Simple question about headers and malloc!
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 26 Dec 2007 21:54:00 -0800
Hello,
This is a C question or if I may call it a situation! Thanking all fellows
in advance for their help.?, 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
.
- Follow-Ups:
- Re: Simple question about headers and malloc!
- From: David Webber
- Prev by Date: Re: is such exception handling approach good?
- Next by Date: Re: is such exception handling approach good?
- Previous by thread: Efficient code maintenance
- Next by thread: Re: Simple question about headers and malloc!
- Index(es):