Strange SegFault

Hi everybody,

Can anyone tell why there should be a segfault in this portion of my code?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
107	char *tmp=strtok(tempo,"/");
108	char *fileName=tmp;
109	while(tmp!=NULL){
110		tmp=strtok(NULL,"/");
111		if(tmp==NULL)
112			break;
113		fileName=tmp;
114	}
115
116	char * logo;
117	memcpy(logo,fileName,strlen(fileName)+1);
118	strcat(logo,"log");
119	logo[strlen(logo)-4]='.';
120	cout<<logo<<endl;
121	cout<<fileName<<endl;
122
123	cout<<"---------------------Reaches Here---------------------"<<endl;
124	ofstream myfile ("example.txt");
125
126	return 0;

The entire code is about 300 lines and its purpose is to be a download manager. Compiling the code with line 124 commented out doesn't produce errors. But when I let line 124 be included a segfault rises, preventing the program from ever reaching line 123! I don't know why this line affects the previous lines, since using cout after line 116 in the latter case, I mention that the segfault is risen from usage of memcpy.

I use g++ in Ubuntu 9.10.

I can't figure this mess out! It really confuses me.

Thanks for any comments in advance :)
1
2
3
char * logo;
memcpy(logo,fileName,strlen(fileName)+1);
strcat(logo,"log");


you did not initialize your pointer. it points anywhere, this could be very dangerous.
use something like

 
char *logo = new char[BUFSIZE]


or

 
char logo[BUFSIZE]


I didnt look properly if there is more, just try it out.
Do you understand why it is wrong what you typed? If not i can try to explain, but this is one of the main meaning and must-knows of pointers..

Greatings,
Maikel
Last edited on
Hey thanks a lot. Actually this was a friend's code and I didn't look into the variables he has used!

But still I can't figure out why removing that line (124) in the code makes the program work?

Thanks.
But still I can't figure out why removing that line (124) in the code makes the program work?


It didn't make it work, it just made the screw up harder to find.

Heap corruption causes all sorts of weird, unpredictable behavior. You were really lucky that the program crashed and exposed the problem for you. Otherwise these kinds of bugs can just make your program behave incredibly strange and are very hard to find and fix.

Pretty much, when you have heap corruption, all bets are off, and the program can pretty much do whatever it feels like doing =P
That is because segmentation faults can result in just very strange behaviour. Sure logo is a pointer. But where does it point? Where is the string placed? We can't say.
Because logo wasn't initialized, it could have any value. Whatever that value is, the program interprets it as the address at which to store the string. If logo happens to have the value 1200, then the computer attempts to place the data at address 1200, even if that happens to be an address in the middle of your program code. Chances are that wherever logo points, that is not where you want to put your string. This kind of error can produce some of the most hard-to-trace bugs. And you just observed this. I think it have to be related to the fact that it crushed just in the moment you wanted to write new data to memory. Maybe some error was in queue and in the moment you wanted to use the memory again (by initializing ofstream myfile) the error come up.

But really, I dont know.
I even think, that this is dependend of the kernel / OS you use.
We should ask some experts, if you really want to know. I am no expert ;-)

Yours, Maikel


EDIT:

Gosh, i am too slow with keyboards. Sorry for another post ;-)
Last edited on
Thanks to all replies :) I think you are almost right.

Cheers,
Topic archived. No new replies allowed.