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.
you did not initialize your pointer. it points anywhere, this could be very dangerous.
use something like
char *logo = newchar[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..
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 ;-)