0

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.

16
  • Your code is incomplete; what is L? Commented Jan 22, 2012 at 16:49
  • 4
    The error is elsewhere. Post the relevant code, please. Commented Jan 22, 2012 at 16:50
  • You should compare with NULL instead of 0 Commented Jan 22, 2012 at 16:50
  • 5
    @BlackBear: actually a zero comparison is perfectly legal. Commented Jan 22, 2012 at 16:51
  • 1
    Debugging print statements should at least end with a newline. Arguably, they should be written to stderr, not stdout, or you should fflush() either stdout or 0 after writing. Otherwise, the information may be buffered until later. Commented Jan 22, 2012 at 17:10

3 Answers 3

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you,the first problem with applR was the actual problem. After fixing it everything works fine. You are also right about memory leaks,i need to learn some tool (like valgrind?) to fix all of them.
1

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).

1 Comment

Yes,it seems like the only possible answer,but everything is exactly as i wrote .I have attached the whole project to my post..
0

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.

1 Comment

Thanks, but i use "free" for pointers created by strdup,which uses malloc for allocting memory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.