Reputation: 93
I'm still trying to get the hang of the basics of C, and right now I'm just trying to deal with char pointers.
When I try to free up inputFile
I get a glibc detected which proceeds to then show me a memory map and then aborts.
Why is that?
I provided all the code where inputFile
is used
EDIT: Made the following changes in new code. Still get the same error, and for some reason its telling me the file doesn't exist even though it is there and worked before.
char *inputFile = malloc(100 * sizeof(char));
strcpy(inputFile, "NULL"); //NEW CHANGE
.
.
.
if(optind < argc)
{
strcpy(inputFile, argv[argc -1]); //NEW CHANGE
}
else if(optind == argc)
{
printf("Type the name of the input file\n");
fgets(inputFile, 30, stdin);
printf("Your input file name is: %s", inputFile);
}
if(strcmp(inputFile,"NULL") == 0)
{ //NEW CHANGE
printf("No inputfile detected");
exit(1);
}
if(argc != 1)
{
int rows, cols, newRows, newCols;
PIXEL *b, *nb;
readFile(inputFile, &rows, &cols, &b);
writeFile(fname, rows, cols, b);
free(inputFile);
}
This is the exact error I'm getting:
example.bmp
Your input file name is: example.bmp
Can't open bmp file to read: No such file or directory
*** glibc detected *** ./bmptool: double free or corruption (out): 0x00007ffc4765c400 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3111275e5e]
/lib64/libc.so.6[0x3111278cf0]
./bmptool[0x401e15]
/lib64/libc.so.6(__libc_start_main+0x100)[0x311121ed20]
./bmptool[0x4009c9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 00:1d 35691265 /a/buffalo.cs.fiu.edu./disk/jccl-001/homes/cmanr012/Programming3/bmptool
00602000-00603000 rw-p 00002000 00:1d 35691265 /a/buffalo.cs.fiu.edu./disk/jccl-001/homes/cmanr012/Programming3/bmptool
00c5c000-00c7d000 rw-p 00000000 00:00 0 [heap]
3110e00000-3110e20000 r-xp 00000000 fc:01 279087 /lib64/ld-2.12.so
3111020000-3111021000 r--p 00020000 fc:01 279087 /lib64/ld-2.12.so
3111021000-3111022000 rw-p 00021000 fc:01 279087 /lib64/ld-2.12.so
3111022000-3111023000 rw-p 00000000 00:00 0
3111200000-311138b000 r-xp 00000000 fc:01 279306 /lib64/libc-2.12.so
311138b000-311158a000 ---p 0018b000 fc:01 279306 /lib64/libc-2.12.so
311158a000-311158e000 r--p 0018a000 fc:01 279306 /lib64/libc-2.12.so
311158e000-3111590000 rw-p 0018e000 fc:01 279306 /lib64/libc-2.12.so
3111590000-3111594000 rw-p 00000000 00:00 0
32f6800000-32f6816000 r-xp 00000000 fc:01 279209 /lib64/libgcc_s-4.4.7-20120601.so.1
32f6816000-32f6a15000 ---p 00016000 fc:01 279209 /lib64/libgcc_s-4.4.7-20120601.so.1
32f6a15000-32f6a16000 rw-p 00015000 fc:01 279209 /lib64/libgcc_s-4.4.7-20120601.so.1
7f3710000000-7f3710021000 rw-p 00000000 00:00 0
7f3710021000-7f3714000000 ---p 00000000 00:00 0
7f37164b6000-7f37164b9000 rw-p 00000000 00:00 0
7f37164d6000-7f37164da000 rw-p 00000000 00:00 0
7ffc4764a000-7ffc4765f000 rw-p 00000000 00:00 0 [stack]
7ffc477ff000-7ffc47800000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
Upvotes: 0
Views: 1205
Reputation: 15566
These three are causing major issues:
char *inputFile = malloc(100 * sizeof(char));
...
inputFile = NULL;
...
inputFile = argv[argc -1];
...
free(inputFile);
The first allocates memory (reasonable enough).
Then the second leaks that memory by removing the pointer (not so reasonable).
Then the third assigns the value of a program argument to the char
pointer (ok).
And then the fourth proceeds to free that program argument (Whoa! Undefined Behavior Time!).
What you were probably intending to do was:
char inputFile[100];
...
inputFile[0] = 0;
...
strcpy(inputFile, argv[argc - 1]);
Quoting from C11 draft N1548:
if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to
free
orrealloc
, the behavior is undefined.
EDITS:
strcpy(inputFile, "NULL"; //NEW CHANGE
This is utterly wrong. First of all, NULL
is a symbolic constant, not a string.
if(strcmp(inputFile,"NULL") == 0) { //NEW CHANGE
This is definitely wrong too. It should be if(!*inputfile)
. Again, NULL
is a symbolic constant.
Upvotes: 1