Take the 2-minute tour ×

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

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


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

share|improve this question
Your code is incomplete; what is L? –  Oli Charlesworth Jan 22 '12 at 16:49
The error is elsewhere. Post the relevant code, please. –  Kerrek SB Jan 22 '12 at 16:50
You should compare with NULL instead of 0 –  BlackBear Jan 22 '12 at 16:50
@BlackBear: actually a zero comparison is perfectly legal. –  StilesCrisis Jan 22 '12 at 16:51
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. –  Jonathan Leffler Jan 22 '12 at 17:10
show 11 more comments

3 Answers

up vote 1 down vote accepted

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

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

share|improve this answer
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. –  iensen Jan 22 '12 at 19:49
add comment

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

share|improve this answer
Yes,it seems like the only possible answer,but everything is exactly as i wrote .I have attached the whole project to my post.. –  iensen Jan 22 '12 at 17:22
add comment

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


share|improve this answer
Thanks, but i use "free" for pointers created by strdup,which uses malloc for allocting memory –  iensen Jan 22 '12 at 18:50
add comment

Your Answer


