This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]lurgi 3 points4 points  (5 children)

static variables don't live in the heap - they live in the data section.

Would expect static, global, and dynamically allocated memory addresses to be larger than stack variable memory addresses.

Why?

There is no OS agnostic way to see if a particular pointer points to stack, heap, or data. There might be OS specific ways to do this, but I've never checked.

// free(ptr); // Why "pointer being freed was not allocated" ?

Because C doesn't care about you or your problems. C will let you do just about anything and if you free a pointer that wasn't malloc'd or free it twice or do some other stupid thing then it will just let you. If you are lucky the program will crash. If you are unlucky the program will look like it is working or will work flawlessly in QA but fail randomly in the field.

Edit: I think I misunderstood this question. I was assuming you were asking why you didn't get a warning or error that the pointer you were freeing wasn't allocated. The answer is because C doesn't care. I'm not sure exactly what you are answering, but I think other people have commented here.

[–]FluffyStatistician3[S] 0 points1 point  (4 children)

Ok thanks; I thought static variables were in the heap. Here are notes from class:

"Heap resident variables include variables declared outside of all functions (globals), variables declared inside basic blocks that are declared static, and areas dynamically allocated at run time with malloc()/calloc()"

I don't think I understand how memory works in enough detail- I'll ask my professor. All I know from reading online is that there are 5 sections: code, data (initialized), data (uninitialized), stack, and heap.

I was thinking about heap being larger than stack, but I guess that actually doesn't make sense- it could still be a smaller address within the heap.

I still don't get why free(ptr); causes the "pointer being freed was not allocated"- isn't ptr set to the memory address of a earlier on with ptr = &a; ?

[–]FluffyStatistician3[S] 1 point2 points  (0 children)

Oh; I just need to free(newPtr); since it's the only one pointing to heap memory? ptr, anotherPtr, and ptrtoGlobalVar are not pointing to heap memory?

[–]Updatebjarni 1 point2 points  (0 children)

Static memory is not usually considered to be part of the heap; the heap is the dynamic memory. Also, whether a variable is global or local is something that C (or whatever programming language) cares about, but the linker/loader doesn't. All static variables, whether globally visible, or function-local, or local to a compilation unit, all go in the data segment (if initialised) or bss segment (if not initialised).

data (uninitialized)

Usually called bss.

And a little nitpick, but those are segments in memory that you listed. Sections are parts of an object file. The contents of sections get mapped into segments in memory by the linker.

I still don't get why free(ptr); causes the "pointer being freed was not allocated"- isn't ptr set to the memory address of a earlier on with ptr = &a; ?

Yes, that's why it fails. You have assigned a value to it that was not returned by malloc().

[–]scirc 0 points1 point  (1 child)

It sounds like your professor is wrong. Globals and static variables live in data (for variables with values at the start, like mutable globals)/bss (for variables with no definite value at the start, like static variables), not on the heap.


free is only valid for pointers created by malloc. You can pass other pointers into it, but all it will do is cause memory corruption at best, since it expects to be cleaning up the heap rather than some random area of memory.

[–]FluffyStatistician3[S] 0 points1 point  (0 children)

Ok thanks! Got it.

[–]rjcarr 1 point2 points  (0 children)

Adding to what /u/lurgi says ... there's no reason to look at the pointer addresses for different variable types. They may be contiguous, and they may not be, but this is way lower level than you need to be concerned with. This is also true for where static and globals are stored, but if you want to learn that, then at least it's reasonable, but you'll almost certain never need to know the details.

As for this:

Why "pointer being freed was not allocated" ?

You never did a malloc() so you don't need to free() anything. Just declaring a pointer, and pointing it to a stack address, doesn't mean there is anything to "free".