tags:

views:

249

answers:

8

Very much related to my previous question, but I found this to be a separate issue and am unable to find a solid answer to this.

Is the memory used by a (character) array freed by going out of scope?

An example:

void method1()
{
  char str[10];
  // manipulate str
}

So after the method1 call, is the memory used by str (10 bytes) freed, or do I need to explicitly call free on this as well?

My intuition tells me this is just a simple array of primitive types, so it's automatically freed. I'm in doubt because in C you can't assume anything to be automatically freed.

+12  A: 

In this case no you do not need to call free. The value "str" is a stack based value which will be cleaned up when that particular method / scope is exited.

You only need to call free on values which are explicitly created via malloc.

JaredPar
(Where "no" means - yes, it's free'd by going out of scope, and no you don't need to call free explicitly)
jmanning2k
@jmanning2k i re-read my post as well and editted it to be a bit more clear about what "no" meant
JaredPar
A: 

No, local variables of this sort are allocated on the stack, so when you return from the procedure the memory is available for the next function call, which will use the memory for its stack frame.

If you use malloc() the space is allocated on the heap, which must be explicitly freed.

ConcernedOfTunbridgeWells
+9  A: 

It is automatically freed. If you didn't malloc it, you don't need to free it. But this has nothing to do with it being a "simple array of primitive types" - it would be freed if it was an array of structures. It is freed because it is a local variable.

Given that you are asking these very basic questions, I have to ask which C textbook are you using. Personally, I don't believe that you can usefully learn C without reading Kernighan & Ritchie's The C Programming Language, which explains all this stuff very clearly.

anon
Excellent advice!
Lazarus
pbean
A: 

I think it's freed not because it's primitives but that it's a local variable and that will be allocated on the stack not the heap. If you don't malloc it then you can't free it as far as I remember.

Lazarus
A: 

Yes, it is "freed" when it goes out of scope.
No, you don't have to explicitly free it.

The char array is allocated on the stack, so when you return from the function, that stack space is re-usable. You do not need to explicitly free the memory.

Good rule of thumb: if you malloc, you must free.

jeffamaphone
The question is about C, not C++
anon
Ah, yes, indeed. Let me fix that.
jeffamaphone
+3  A: 

Yes, it is "freed." (Not free()'ed, though.)

Since str is an automatic variable, it will only last as long as its scope, which is until the end of the function block.

Note that you only free() what you malloc().

aib
+2  A: 

Yes, the memory is freed automatically once method1 returns. The memory for str is allocated on the stack and is freed once the method's stack frame is cleaned up. Compare this to memory allocated on the heap (via malloc) which you must explicitly free.

Andrew Keeton
A: 

I'm a bit rusty in C/C++ lately, but I think you're right. As long as you didn't dynamically allocate that memory, you should be fine.

ManicMailman