stack allocated memory versus dynamically allocated memory
gcc 4.6.2 c89
I am just wondering what could cause more memory issues. For example, if you allocate a structure array on stack or if you was to allocate it dynamically on the heap.
Normally, I follow a simple rule. If the structure is only to be used in that function, then I allocate on the stack. However, if I need to refer to it somewhere else by keeping it in memory I would allocate dynamically.
The reason I asked this question as some of my memory was getting corrupted and I was told that I should allocate dynamically rather than on the stack for structure arrays. As stack memory has more chance of getting corrupted. However, if memory that is allocated dynamcially gets corrupted you can easily free it.
Does the above make sense to anyone?
Many thanks for any suggestions,
I wouldn't say that any one way of allocating a structure has "more chance of corruption". Whatever is causing the corruption could happen just as easily however it's allocated.
I'd say you'd be better off fixing the source of the corruption: You can use gdb to put a breakpoint on write to the corrupted variable with watch <varname>
. Alternatively, you can also put a breakpoint when you detect corruption, and then use reverse debugging to find where the corruption occurred.
Edit: There seems to be some confusion about the meaning of stack and static
allocation:
void foo() {
int a[10]; // stack
static int b[10]; // static
int *c = malloc(sizeof(int) * 10); // dynamic on the heap
}
Stack variables are valid only during the lifetime of the function - once this function has returned, you can't expect the data at the location of a
to still be valid. Sometimes these are called local or automatic variables.
Static variables inside a function are valid also outside the function - the data is kept around between function invocations. This means if you do this:
void foo() {
static int a = 0;
a++;
printf("%dn",a);
}
The number printed will increase by one on each invocation of foo()
. Generally, functions don't have many variables declared as static, because static variables remember their last value from the last time the function was called - which is usually not the behaviour you want. It's possible this is what was meant by the person who told you that static variables have "more chance of being corrupted".
Heap variables are valid from when they're created to when they're free()
d. This is also called dynamic allocation and is what you want if you're planning to return data to another part of the program (or if you want to specify the length of an array at runtime in C89).
Aside: static
is confusing in that when applied to a function name or variable in global scope:
static void foo() { ... }
static int x;
it means "this function or variable is only visible inside this translation unit". I'm not sure why the static
keyword has these two different meanings.
As a rule, never allocate statically, instead allocate on the stack when a data structure is local to a function. That makes it much easier to create re-entrant functions and avoid all sorts of issues.
(As an exception, allocating const
data statically is no problem.)
Edit : ok, you meant stack allocation. In that case the rule you follow is sound practice. But neither stack nor heap allocation is any guarantee against memory corruption.
If you're worried about memory corruption, you should worry in both dynamic and static allocations. If you have a corruption, the difference is only what will be corrupted, but the result would be bad either way.
If you're worried about leaks, then static allocation makes life simple - you don't need to free it, so it can't leak.
But I think the main criterion should be whether or not static allocation makes you allocate too much.
With dynamic allocation, you can size your allocation based on your actual requirements, while with static allocations you may be force to allocate much more, up to some theoretical maximum. For example, if you want a structure per client, and support up to 1000 clients, you have to allocate 1000 structures in advance, even if in practice there are only 3 clients. With dynamic allocation, you allocate what you need, when you need it.
上一篇: 从私有堆分配内存会导致死锁?
下一篇: 堆栈分配的内存与动态分配的内存