Reputation: 51
New to C and getting errors when running my program on Linux, the program ran and worked fine in windows compiled using MinGW
I get double free or corruption (out): when running the program.
here is where I think the error is:
TokenizerT *TKCreate( char * ts ) {
if(ts==NULL){
return NULL;
}
char * tempstr;
tempstr = ts;
//Allocating memory for Tokenizer struct
TokenizerT *Tokenizer= (TokenizerT *) malloc(sizeof(TokenizerT));
//Allocating memory for token stream
Tokenizer->string = strdup(tempstr);
//strcpy(Tokenizer->string, ts);
//Check field to see wether or not strtok has went through its original call
Tokenizer->stepped = 0;
return Tokenizer;
}
I also have a simple function that always works in windows, but returns wrong output in Linux:
int isOctal(const char * str){
const char *curr = str;
int hasValue = 0;
//Checking if token begins with 0
if(*curr!='0'){
return 0;
}
++curr;
//Ensuring 0-7 are only other digits used
while(*curr!=0){
if ((*curr >='0')&&(*curr<='7')){
++curr;
}else{
return 0;
}
if(*curr>'0'){
hasValue = 1;
}
}
if(hasValue==0){
return 0;
}
return 1;
}
This is what I get when using gdb:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400ca5 in main (argc=1, argv=0x7fffffffe378) at tokenizer.c:365
365 yoken = * TKCreate(argv[1]);
Really scratching my head here, thought I finally ironed out all the problems until I tried to compile on Linux
Edit 1: Added a check for the correct amount of arguments and it still gives the error. If i comment out my call to this function, the code works but I'm worried its not freeing memory then.
void TKDestroy( TokenizerT * tk ) {
free(tk->string);
free(tk);
}
Edit 2: Here is some information from valgrind
==12431== Invalid free() / delete / delete[] / realloc()
==12431== at 0x4C2AD17: free (in /usr/lib64/valgrind/vgpreload_memcheck- amd64-linux.so)
==12431== by 0x400837: TKDestroy (tokenizer.c:76)
==12431== by 0x400E11: main (tokenizer.c:403)
==12431== Address 0xfff000250 is on thread 1's stack
==12431== in frame #2, created by main (tokenizer.c:355)
When I call the program with the incorrect amount of arguments it does not cause an error. The error only occurs when I use the program correctly
Upvotes: 0
Views: 1239
Reputation: 30587
The problem you are seeing in gdb is different to the one you are seeing when you run the program normally.
In the gdb output you can see argc=1
. This means argv only has one element, and argv[1]
is out of bounds.
You probably invoked the program with no arguments. Note that when you run a program under gdb, the arguments are supplied after the 'run' command inside gdb, not when you invoke gdb itself.
I suggest you add a check that you have received the expected number of arguments and display an error message if not.
As for the 'double free or corruption', this information in the valgrind output is probably important:
==12431== Address 0xfff000250 is on thread 1's stack
It implies you are trying to free a local variable instead of a heap variable. Perhaps you are passing the wrong argument to TKDestroy?
Upvotes: 3
Reputation: 1746
int main(int argc, char * argv[])
{
if(argc<2)
{
printf("Usage : < command line execution format>\n");
return 0;
}
//your code
return 0;
}
If argc = 1
means command line argument is 1
and i.e the file of the execution.
argv[0]
contains the file name of the file executed.
if you try to access argv[1]
which does not have any memory allocated which results in segmentation fault.
To over come this you need to check the argc
before accessing any element argv[]
.
Upvotes: 0