Simple question about headers and malloc!



Hello,

This is a C question or if I may call it a situation! Thanking all fellows
in advance for their help.

I find myself in a bind where I *must* specifically include the yyy header
file (see code sample below) in main even though all its declarations are
specifically used for yyy.c. Therefore I am making all of its declarations
global to the whole program! and I don't like it !

The reason for this is because when I am in yyy.c file I am allocating
memory (using malloc) and then exit back to main. I later call again
functions in yyy.c and allocate another block of memory (using malloc) and
further continue some logic in yyy.c. As I I read data from first memory
allocation, I get data strored from the second allocation... even though I
didn't free the first malloc allocation! I don't free it because I need to
use both data in the memory allocated by both mallocs. But each allocation is
done at different times!!!!!

Therefore, I think that when you exit a function where you used malloc, its
pointer is lost right?, but the allocation of the memory and its data is not.
And since it is not freed, the data in this memory should still stay intact
after leaving the function that allocated, right?

At a later instance if I go back in yyy.c and do another malloc , since the
pointer to the previous memory allocation is lost, I think that the previous
memory block is up for grabs by the next malloc command and this is where
data gets mixed up. So this is why I am including the yyy.h file in main so
all pointers returned from mallocs always stay valid and all allocations are
reserved at all times.

There are other files between xxx.c and yyy.c and would be quite lenghty to
explain it all, but I hope its clear enough to get a general idea. Making all
pointer declarations that will be used with malloc commands in a global
header file is the only way I see that preserves memory allocations when
mallocs are done at different times since the pointers returned by malloc are
never lost. I don't really think I would like to return pointers returned by
malloc back to main in order to preserve them.

I could be wrong about all this, and if I am, I would appreciate your help.
I just keep testing and get the same results... data overlapping!

So as I was saying, basically if you have a .c file with its header file, we
typically include the header file from within the .c file. But this is not
what I am doing.... please view the sample below. This obviously compiles but
not sure if this is a good C programming practice. Any feedback appreciated.

#include <yyy.h> //Is this okay?
#include <xxx.c>

int main()
{
//...some code !
//Calls functions in xxx.c
}

===========xxx.h
....some declarations
==============
===========xxx.c
#include <xxx.h>
#include <yyy.c>
....some code
//Calls functions in yyy.c
==============

===========yyy.h
....some declarations
==============

===========yyy.c
//Should yyy.h be included here instead?
....some code
==============


--
Best regards
Robert
.



Relevant Pages

  • Re: Simple question about headers and malloc!
    ... Therefore I am making all of its declarations ... memory (using malloc) and then exit back to main. ... allocation, I get data strored from the second allocation... ...
    (microsoft.public.vc.language)
  • Re: Simple question about headers and malloc!
    ... Therefore I am making all of its declarations ... memory (using malloc) and then exit back to main. ... allocation, I get data strored from the second allocation... ...
    (microsoft.public.vc.language)
  • Re: svn - but smaller?
    ... rather than just having a gigantic portion of memory allocated ... limits of the underlying application. ... larger than 2048 might warrant use of malloc. ... of static vs. dynamic allocation, ...
    (freebsd-stable)
  • Re: xmalloc string functions
    ... than a NULL return from malloc(). ... Memory is quite a different kind of resource. ... I try to write my code so any allocation failure is handled ... its not likely that one will be more susceptible to failure than the other. ...
    (comp.lang.c)
  • Re: A solution for the allocation failures problem
    ... reduce the effort involved in managing memory. ... Releasing such buffer may not effect the ability of malloc() to return ... says it releases the memory for subsequent allocation, ... Dealing with the failure "at point of failure" isn't inherently evil. ...
    (comp.lang.c)