Evgenii.Balai
Evgenii.Balai

Reputation: 949

malloc function crash

I have a problem with memory allocation using malloc. Here is a fragment from my code:

    printf("DEBUG %d\n",L);
    char *s=(char*)malloc(L+2);
    if(s==0)
    {
      printf("DEBUGO1");
    }
    printf("DEBUGO2\n");

It outputs "DEBUG 3",and then a error msgbox appears with this message:

The instruction at 0x7c9369aa referenced memory at "0x0000000". The memory could not be read

For me such behavior is very strange. What can be wrong here?

The application is single threaded.

I'm using mingw C compiler that is built in code::blocks 10.05

I can provide all the code if it is needed. Thanks.

UPD1: There is more code:

char *concat3(char *str1,char *str2,char *str3)
{
    /*concatenate three strings and frees the memory allocated for substrings before*/
    /* returns a pointer to the new string*/

    int L=strlen(str1)+strlen(str2)+strlen(str3);
    printf("DEBUG %d\n",L);
    char *s=(char*)malloc(L+2);
    if(s==0)
    {
      printf("DEBUGO1");
    }
    printf("DEBUGO2\n");
    sprintf(s,"%s%s%s",str1,str2,str3);
    free(str1);
    free(str2);
    free(str3);
    return s;
}

UPD2: It seems the problem is more complicated than i thought. Just if somebody has enough time for helping me out:

Here is all the code

Proj

(it is code::blocks 10.05 project,but you may compile the sources without an ide ,it is pure C without any libraries):

call the program as "cbproj.exe s.pl" (the s.pl file is in the root of the arhive)

and you may see it crashes when it calls the function "malloc" that is on the 113th line of "parser.tab.c"(where the function concat3 is written).

I do the project in educational purpouses,you may use the source code without any restrictions.

UPD3: The problem was that it was allocated not enough memory for one of the strings in program ,but the it seemed to work until the next malloc.. Oh,I hate C now:) I agree with the comments about bad coding style,need to improve myself in this.

Upvotes: 0

Views: 1157

Answers (3)

mike.dld
mike.dld

Reputation: 3049

The problem with this exact code is that when malloc fails, you don't return from the function but use this NULL-pointer further in sprintf call as a buffer.

I'd also suggest you to free memory allocated for str1, str2 and str3 outside this function, or else you might put yourself into trouble somewhere else.


EDIT: after running your program under valgrind, two real problems revealed (in parser.tab.c):

In yyuserAction,

char *applR=(char*)malloc(strlen(ruleName)+7);
sprintf(applR,"appl(%s).",ruleName);

+7 is insufficient since you also need space for \0 char at the end of string. Making it +8 helped.

In SplitList,

char *curstr=(char*)malloc(leng);

there's a possibility of allocating zero bytes. leng + 1 helps.

After aforementioned changes, everything runs fine (if one could say so, since I'm not going to count memory leaks).

Upvotes: 1

Gandaro
Gandaro

Reputation: 3443

You cannot use free on pointers that were not created by malloc, calloc or realloc. From the Manpage:

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

Upvotes: 0

user268396
user268396

Reputation: 11976

From the error message it actually looks like your if statement is not quite what you have posted here. It suggests that your if statement might be something like this:

if(s=0) {
}

Note the single = (assignment) instead of == (equality).

Upvotes: 1

Related Questions