Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7086121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:30:38+00:00 2026-05-28T07:30:38+00:00

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:30:38+00:00Added an answer on May 28, 2026 at 7:30 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with a memory leak in a .NET CF application. Using
I’m trying to debug a memory leak problem. I’m using mtrace() to get a
I have trouble deallocating memory that I allocated using malloc. The program runs fine
I have a problem with out of memory when I'm trying load a few
We have a memory overwrite problem. At some point, during the course of our
My problem is: I have a perl script which uses lot of memory (expected
This is a C# problem. I have a big object in memory at a
it looks like I'm facing a typical memory outage problem when using a PHP
I am having a weird problem .. I have written a CUDA code which
I followed Jonathan's code from here ( MPI_Bcast a dynamic 2d array ) to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.