0
votes

If you have a project that utilizes a makefile which compiles multiple files and headers, does this complicate the heap?

Specifically: I have a main.c file which includes a header, say test.h. In test.c which is linked to test.h memory is allocated explicitly with malloc. main.ccalls the functions in test.c. For some reason, when I try to to free memory inside of the functions in test.c I always get an error:

main(65245) malloc: *** error for object 0x106d012f8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

...this error occurs even though I never, not even once free any memory at all in the entire makefile stack. Obviously stdlib.h is included. What could be going on? Are there separate heaps for main.c and test.c and whenever the latter is called and the result is returned, the heap memory allocated is already freed? I'm really stumped. I can allocate and free memory in main.c without any issue. They have the same includes.

3
Please show some code, it's impossible to understand what's going on without it. Also, it's not very clear from your description what happens where. - SurDin
I really don't think the code is important. It is a conceptual question. I'd like to know if the assumptions I have stated make sense. - nick_name
No, it is a code problem. You can alloc in one file and free in another without issue. 1 heap. You must be having a syntax error. - Michael Dorgan
The learning is more important to me than having someone give me the line that fixes my code (if that's even possible). - nick_name
Anything I should look out for that could cause this strangeness? - nick_name

3 Answers

4
votes

there is no such thing as 'different files' at run-time. all files are integrated into one big binary code at linkage. So, therefore, there is obviously only one heap.
your problem must be something else, since you never freed a memory, maybe you are trying to free static allocated memory or something like that

also, note that there is a convention which is usually pretty good to prevent memory leaks, which says: the part of the program that allocated the memory, is also responsible to free it. It is not directly connected to your question, but it will be helpful for future to try and do it, in order to prevent memory leaks.

2
votes

Are you freeing the same addres/pointer in each file? Yes, you get 1 heap unless you specifically try and get multiple heaps. My guess is you are not freeing the same pointer - perhaps a addressing/double pointer error of some sort. You do best to post some source code for us to be sure...

1
votes

All malloc() calls allocate from the same heap, no matter what file you call them from. You are freeing a pointer that did not come from any malloc() call.

Review your code carefully, print allocated and freed pointer values to debug log.