this is the problem area:

FILE* fp = fopen(file_name, "r");
	if (fp == NULL) printf("couldn't open file\n");
	char line[100]; 
			
	while ((fscanf(fp, " %[^\n]", line) != EOF)) 
	{
		printf("%s\n",line);
		clock_t start = clock();
		perform_operation(line, 1);
		clock_t end = clock();
		sprintf(log_msg, "time taken: %ld ms",(end-start));
		log(log_msg);
	}
	
	sprintf(log_msg, "final tree");

	if (fp != NULL)
	{
		fclose(fp);
	}

Now I get some beautifully formatted output:

*** glibc detected *** ./a.out: double free or corruption (out): 0x00000000007310a0 ***
======= Backtrace: =========
/lib/libc.so.6[0x7fd1979e5cb8]
/lib/libc.so.6(cfree+0x76)[0x7fd1979e8276]
/lib/libc.so.6(fclose+0x151)[0x7fd1979d5d21]
./a.out[0x402ea9]
./a.out[0x4031ff]
/lib/libc.so.6(__libc_start_main+0xe6)[0x7fd19798c5a6]
./a.out[0x400bd9]
======= Memory map: ========
00400000-00404000 r-xp 00000000 00:1a 586206                             /amd/ss01/export/homes19/mrkm/workspace/c/misc/a.out
00603000-00604000 r--p 00003000 00:1a 586206                             /amd/ss01/export/homes19/mrkm/workspace/c/misc/a.out
00604000-00605000 rw-p 00004000 00:1a 586206                             /amd/ss01/export/homes19/mrkm/workspace/c/misc/a.out
00731000-007b5000 rw-p 00731000 00:00 0                                  [heap]
7fd190000000-7fd190021000 rw-p 7fd190000000 00:00 0 
7fd190021000-7fd194000000 ---p 7fd190021000 00:00 0 
7fd19796e000-7fd197ad6000 r-xp 00000000 08:01 3597494                    /lib/libc-2.9.so
7fd197ad6000-7fd197cd6000 ---p 00168000 08:01 3597494                    /lib/libc-2.9.so
7fd197cd6000-7fd197cda000 r--p 00168000 08:01 3597494                    /lib/libc-2.9.so
7fd197cda000-7fd197cdb000 rw-p 0016c000 08:01 3597494                    /lib/libc-2.9.so
7fd197cdb000-7fd197ce0000 rw-p 7fd197cdb000 00:00 0 
7fd197ce0000-7fd197cf6000 r-xp 00000000 08:01 3596349                    /lib/libgcc_s.so.1
7fd197cf6000-7fd197ef6000 ---p 00016000 08:01 3596349                    /lib/libgcc_s.so.1
7fd197ef6000-7fd197ef7000 r--p 00016000 08:01 3596349                    /lib/libgcc_s.so.1
7fd197ef7000-7fd197ef8000 rw-p 00017000 08:01 3596349                    /lib/libgcc_s.so.1
7fd197ef8000-7fd197f7c000 r-xp 00000000 08:01 3597505                    /lib/libm-2.9.so
7fd197f7c000-7fd19817b000 ---p 00084000 08:01 3597505                    /lib/libm-2.9.so
7fd19817b000-7fd19817c000 r--p 00083000 08:01 3597505                    /lib/libm-2.9.so
7fd19817c000-7fd19817d000 rw-p 00084000 08:01 3597505                    /lib/libm-2.9.so
7fd19817d000-7fd19826e000 r-xp 00000000 08:01 6799482                    /usr/lib/libstdc++.so.6.0.10
7fd19826e000-7fd19846e000 ---p 000f1000 08:01 6799482                    /usr/lib/libstdc++.so.6.0.10
7fd19846e000-7fd198475000 r--p 000f1000 08:01 6799482                    /usr/lib/libstdc++.so.6.0.10
7fd198475000-7fd198477000 rw-p 000f8000 08:01 6799482                    /usr/lib/libstdc++.so.6.0.10
7fd198477000-7fd19848a000 rw-p 7fd198477000 00:00 0 
7fd19848a000-7fd1984aa000 r-xp 00000000 08:01 3597473                    /lib/ld-2.9.so
7fd19858a000-7fd19868d000 rw-p 7fd19858a000 00:00 0 
7fd1986a5000-7fd1986a9000 rw-p 7fd1986a5000 00:00 0 
7fd1986a9000-7fd1986aa000 r--p 0001f000 08:01 3597473                    /lib/ld-2.9.so
7fd1986aa000-7fd1986ab000 rw-p 00020000 08:01 3597473                    /lib/ld-2.9.so
7fffbb7b4000-7fffbb7c9000 rw-p 7ffffffea000 00:00 0                      [stack]
7fffbb7ff000-7fffbb800000 r-xp 7fffbb7ff000 00:00 0                      [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

If I comment line 18 ie the fclose then there is no problem. As can be seen from above, fp is a local pointer and not passed to any called function as a parameter (so that no possible inadvertent closing of file beforehand). Then why would this cause such a problem? Why the runtime environment started to spew swear words like these even though its just a mere fclose?

I doubt the problem is fclose. Of course it would be a good idea if fp is NULL not to continue.

Do you have long input lines? Is the problem really elsewhere in the code you didn't post? Can you post enough of a trimmed but complete bit of code that demonstrates the problem (sample input file, too)?

Edited 6 Years Ago by Dave Sinkula: n/a

> while ((fscanf(fp, " %[^\n]", line) != EOF))
I agree with Dave, this could so easily overflow your char array.

Try using fgets instead.

Your fscanf() line of code doesn't have a format specifier:

while ((fscanf(fp, " %s[^\n]", line) != EOF))

Your fscanf() line of code doesn't have a format specifier:

while ((fscanf(fp, " %s[^\n]", line) != EOF))

It did before you incorrected it. [edit]Specifying a max width would have been a better attempt.

Edited 6 Years Ago by Dave Sinkula: n/a

#1 post does not now, have a format specifier for fscanf().

You're saying that it used to have this?

This article has been dead for over six months. Start a new discussion instead.