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

  • SEARCH
  • Home
  • 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 6800353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:57:32+00:00 2026-05-26T18:57:32+00:00

I am struggling to wrap my head around malloc in c – specifically when

  • 0

I am struggling to wrap my head around malloc in c – specifically when it needs to be free()’d. I am getting weird errors in gcc such as:

... free(): invalid next size (fast): ...

when I try to free a char pointer. For example, when reading from an input file, it will crash on certain lines when doing the following:

FILE *f = fopen(file,"r");
char x[256];
while(1) {
    if(fgets(x,sizeof x,f)==NULL) break;
    char *tmp = some_function_return_char_pointer(x); //OR malloc(nbytes);
    // do some stuff
    free(tmp); // this is where I get the error, but only sometimes
}

I checked for obvious things, such as x being NULL, but it’s not; it just crashes on random lines.

But my REAL question is – when do I need to use free()? Or, probably more correctly, when should I NOT use free? What if malloc is in a function, and I return the var that used malloc()? What about in a for or while loop? Does malloc-ing for an array of struct have the same rules as for a string/char pointer?

I gather from the errors I’m getting in gcc on program crash that I’m just not understanding malloc and free. I’ve spent my quality time with Google and I’m still hitting brick walls. Are there any good resources you’ve found? Everything I see says that whenever I use malloc I need to use free. But then I try that and my program crashes. So maybe it’s different based on a variable’s scope? Does C free the memory at the end of a loop when a variable is declared inside of it? At the end of a function?

So:

for(i=0;i<100;i++) char *x=malloc(n); // no need to use free(x)?

but:

char *x;
for(i=0;i<100;i++) {
    x=malloc(n);
    free(x); //must do this, since scope of x greater than loop?
}

Is that right?

Hopefully I’m making sense…

  • 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-26T18:57:33+00:00Added an answer on May 26, 2026 at 6:57 pm

    malloc() is C’s dynamic allocator. You have to understand the difference between automatic (scoped) and dynamic (manual) variables.

    Automatic variables live for the duration of their scope. They’re the ones you declare without any decoration: int x;

    Most variables in a C program should be automatic, since they are local to some piece of code (e.g. a function, or a loop), and they communicate via function calls and return values.

    The only time you need dynamic allocation is when you have some data that needs to outlive any given scope. Such data must be allocated dynamically, and eventually freed when it is no longer necessary.

    The prime usage example for this is your typical linked list. The list nodes cannot possibly be local to any scope if you are going to have generic “insert/erase/find” list manipulation functions. Thus, each node must be allocated dynamically, and the list manipulation functions must ensure that they free those nodes that are no longer part of the list.

    In summary, variable allocation is fundamentally and primarily a question of scope. If possible keep everything automatic and you don’t have to do anything. If necessary, use dynamic allocation and take care to deallocate manually whenever appropriate.

    (Edit: As @Oli says, you may also want to use dynamic allocation in a strictly local context at times, because most platforms limit the size of automatic variables to a much smaller limit than the size of dynamic memory. Think “huge array”. Exceeding the available space for automatic variables usually has a colourful name such as “pile overrun” or something similar.)

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

Sidebar

Related Questions

I've been struggling to wrap my head around this for some reason. I have
I'm really struggling to wrap my head around some of this stuff. Let me
I've been struggling with this for hours and can't wrap my head around where/how
I am struggling to wrap my head around xslt... trying to convert the following
I'm really struggling to wrap my head around this: I have a UserModel and
Have been struggling with Javascript closure for a while trying to wrap brain around
I'm trying to wrap my head around the OpenGL object model on iPhone OS.
I am making a concerted effort to wrap my head around Rspec in order
I have been trying to wrap my head around mvvm for the last week
Ok, so I've been trying to wrap my head around recursion in Java and

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.