Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,211 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,269 people online right now. Registration is fast and FREE... Join Now!




the magic printf()!

 
Reply to this topicStart new topic

the magic printf()!, one printf() not work but NO error & NO warning

Oscar53
31 May, 2008 - 09:07 AM
Post #1

New D.I.C Head
*

Joined: 15 May, 2008
Posts: 23


My Contributions
Hi today I was doing a program for getting all anagrams of an input string but the printf() for writing "Finished" not work!!!

all the printf() before it work but the magic is that also all the printf() after it work!!!

it's not possibile!!!

Here the code:

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

FILE *fp;

void Anagramma(char A[30],char B[30]);

int main()
{
    char A[30];
    printf("Inserisci la stringa: ");
    gets(A);          
    fp=fopen("Result.txt","w");  //open the file
        Anagramma(A,"");  //call anagramma
    fclose(fp);          //close the file

    printf("I'M A MAGIC PRINTF()"); // this printf NOT WORK

    printf("I'M NOT MAGIC BUT I WORK");
    getch();
    return 0;
}

void Anagramma(char A[30],char B[30])
{
    char temp[2];
    int i,k;
    if(strlen(A)==0)    //se la stringa A Š vuota
        fprintf(fp,"%s\n",B);  //scrivo nel file la stringa B
    else
    {
        for(i=0;i<strlen(A);i++)
        {
            temp[0]=A[i];   //in temp salvo il carattere i-esimo di A
            temp[1]=0;

            for(k=i;k<strlen(A);k++)
                    A[k]=A[k+1];    

            Anagramma(A,strcat(B,temp));

            B[strlen(B)-1]=0;  //elimino temp da B

            for(k=strlen(A);k>=i;k--)
                    A[k+1]=A[k];      
            A[i]=temp[0];    
                
        }
    }
}


Thanks biggrin.gif biggrin.gif

This post has been edited by Oscar53: 1 Jun, 2008 - 12:14 AM
User is offlineProfile CardPM
+Quote Post

mikeblas
RE: The Magic Printf()!
31 May, 2008 - 02:19 PM
Post #2

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions
As far as I can tell, neither of your print statements will ever execute because you end up infinitely recursing and blowing out of stack space.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: The Magic Printf()!
31 May, 2008 - 04:11 PM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
I'm thinking that the Anagramma function will be called N levels deep, where N is the length of string A. Each level chops 1 character off the string, due to the loop where k maxes out at strlen(A) - 1 (assigning A[k+1] would then copy the null terminator). Since A is being modified each level deep, for each return, the for loop will terminate as strlen is being directly called in the continuation/termination expression. I think one issue here is the strcat(B, temp) though. I'm surprised that the compiler even allows this (in GCC, string literals are const), as the initial parameter is "". The 30 in "char B[30]" is not meaningful, so there's a buffer overflow here...

Perhaps the string literal "I'M A MAGIC PRINTF()" is being overwritten in memory by this overflow of B in such a way that causes B to become contaminated with spaces and/or nulls?

There are other issues with this algorithm... but replacing the "" with a variable B (declared char B[30]; and initialized of course) would test whether or not this is true?

This post has been edited by perfectly.insane: 31 May, 2008 - 04:13 PM
User is offlineProfile CardPM
+Quote Post

Oscar53
RE: The Magic Printf()!
1 Jun, 2008 - 01:34 AM
Post #4

New D.I.C Head
*

Joined: 15 May, 2008
Posts: 23


My Contributions
I solved the magic problem, thanks to your helps!

My program never went in stack overflown but as said perfectly.insane the bug was in the parameter char B[30] where I passed "".

But why a wrong parameter did not work the printf() after the call of Anagramma???

bye icon_up.gif


User is offlineProfile CardPM
+Quote Post

mikeblas
RE: The Magic Printf()!
1 Jun, 2008 - 07:26 AM
Post #5

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions
QUOTE(Oscar53 @ 1 Jun, 2008 - 02:34 AM) *

My program never went in stack overflown but as said perfectly.insane the bug was in the parameter char B[30] where I passed "".
Oops! He's right; I didn't look deeply enough into it. Sorry about that.


QUOTE(Oscar53 @ 1 Jun, 2008 - 02:34 AM) *
But why a wrong parameter did not work the printf() after the call of Anagramma???
I can't tell. On my system, the strcat() call in question does cause an exception, since it's trying to write to read-only memory. Which compiler are you using, on which platform? If it turns out that you're writing over one string and successfully doing it, you're overwriting the next string in memory, and if you're lucky, that next string is the other string literal for the printf() call.
User is offlineProfile CardPM
+Quote Post

Oscar53
RE: The Magic Printf()!
1 Jun, 2008 - 09:18 AM
Post #6

New D.I.C Head
*

Joined: 15 May, 2008
Posts: 23


My Contributions
I use WinXp and TurboC as compiler.

QUOTE

On my system, the strcat() call in question does cause an exception, since it's trying to write to read-only memory


blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif blink.gif

still a question:

when I write in my code for example printf("Hello World"); during the running the string "Hello World" is write in RAM just opened the program or just before write on the video "Hello world" ???

Bye icon_up.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: The Magic Printf()!
1 Jun, 2008 - 11:16 AM
Post #7

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
When you have a program such as this:

CODE
int main() {
    printf("Hello World");
    return 0;
}


This will create a static string in memory containing "Hello World", When the program executes a reference to that static string is passed to the function printf.

The printf function will more or less copy that string into the video buffer.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: The Magic Printf()!
1 Jun, 2008 - 01:48 PM
Post #8

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(Oscar53 @ 1 Jun, 2008 - 10:18 AM) *

I use WinXp and TurboC as compiler.


Does that compiler even generate 32-bit code? That's Turbo C, not Turbo C++? I don't think any compiler back then (almost 20 years ago) protected one against this.
User is offlineProfile CardPM
+Quote Post

Oscar53
RE: The Magic Printf()!
2 Jun, 2008 - 12:19 AM
Post #9

New D.I.C Head
*

Joined: 15 May, 2008
Posts: 23


My Contributions
I use Turbo C++ but I program in C. It generate 16-bit code...do you think that would be better use a 32-bit compiler??? what are the benefits???

Sorry for my english but What the meaning of "will more or less" in the NickDMax's reply???


Thank you again biggrin.gif
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: The Magic Printf()!
2 Jun, 2008 - 02:11 AM
Post #10

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(Oscar53 @ 2 Jun, 2008 - 01:19 AM) *

I use Turbo C++ but I program in C. It generate 16-bit code...do you think that would be better use a 32-bit compiler??? what are the benefits???


There wasn't any memory protection on MS-DOS systems, so there was no concept of putting const data into a read-only data page/segment. Protected mode operation made this possible, so some 32-bit protected mode compilers (with cooperation with the OS) support read-only data sections. In GCC (as of lately), string constants are of the type const char* by default, and depending on the system, in the internal sense, not simply by contract. On such systems, applications would be terminated more than likely for writing to read-only areas of memory, with an access violation or similar exception.
User is offlineProfile CardPM
+Quote Post

Oscar53
RE: The Magic Printf()!
2 Jun, 2008 - 08:22 AM
Post #11

New D.I.C Head
*

Joined: 15 May, 2008
Posts: 23


My Contributions
wink2.gif Thanks!!! biggrin.gif

now I try to use code blocks (under Win) and gcc under Linux.

if do you know a better compiler, suggest me it wink2.gif

Bye biggrin.gif biggrin.gif biggrin.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:13PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month