the magic printf()!

one printf() not work but NO error & NO warning

Page 1 of 1

10 Replies - 1660 Views - Last Post: 02 June 2008 - 09:22 AM Rate Topic: -----

#1 Oscar53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-May 08

the magic printf()!

Posted 31 May 2008 - 10:07 AM

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:

#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 :D :D

This post has been edited by Oscar53: 01 June 2008 - 01:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: the magic printf()!

#2 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: the magic printf()!

Posted 31 May 2008 - 03:19 PM

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.
Was This Post Helpful? 0
  • +
  • -

#3 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: the magic printf()!

Posted 31 May 2008 - 05:11 PM

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 - 05:13 PM

Was This Post Helpful? 1
  • +
  • -

#4 Oscar53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-May 08

Re: the magic printf()!

Posted 01 June 2008 - 02:34 AM

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 :^:
Was This Post Helpful? 0
  • +
  • -

#5 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: the magic printf()!

Posted 01 June 2008 - 08:26 AM

View PostOscar53, on 1 Jun, 2008 - 02:34 AM, said:

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.


View PostOscar53, on 1 Jun, 2008 - 02:34 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#6 Oscar53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-May 08

Re: the magic printf()!

Posted 01 June 2008 - 10:18 AM

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: :blink: :blink: :blink: :blink: :blink: :blink: :blink: :blink: :blink:

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 :^: :^:
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: the magic printf()!

Posted 01 June 2008 - 12:16 PM

When you have a program such as this:

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.
Was This Post Helpful? 0
  • +
  • -

#8 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: the magic printf()!

Posted 01 June 2008 - 02:48 PM

View PostOscar53, on 1 Jun, 2008 - 10:18 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#9 Oscar53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-May 08

Re: the magic printf()!

Posted 02 June 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???

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


Thank you again :D
Was This Post Helpful? 0
  • +
  • -

#10 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: the magic printf()!

Posted 02 June 2008 - 03:11 AM

View PostOscar53, on 2 Jun, 2008 - 01:19 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#11 Oscar53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 15-May 08

Re: the magic printf()!

Posted 02 June 2008 - 09:22 AM

;) Thanks!!! :D

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

if do you know a better compiler, suggest me it ;)

Bye :D :D :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1