Re: Simple question about headers and malloc!
- From: "David Webber" <dave@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 28 Dec 2007 13:07:54 -0000
"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:EC58C260-5573-415E-9E0C-92AC9A37EB7A@xxxxxxxxxxxxxxxx
....
Robby, this is realy horribly confused. As two guide lines:
1. Do not include .c files in anything; only include .h files in .c files (and if absolutely necessary in other .h files).
2. In your .h files define structure types, define function prototypes, but do not declare any variables.
If you design your program to conform with these you stand a much better chance.
There is much too much wrong (for me and I suspect others) to be able to sort it all out, but let me just annotate some of the problem - one or two functions - to give you an idea.
void * SizeMCB_X(int m)
{
switch(m)
{case 0:
pMCB1 = malloc(11*(sizeof(struct MCB)));
return pMCB1;
break;
case 1:
pMCB2 = malloc(11*(sizeof(struct MCB)));
return pMCB2;
break;
//and so forth!
}
Get rid of your pointers defined in the header file. Get rid of this function.
Just have
MCB *pMCB;
pMCB = malloc(11*(sizeof(struct MCB)));
where you need it.
==================================yyy.c
function1()
// Make this
struct MCB * function1()
{
struct MCB* px;
int i;
px = (struct MCB *)SizeMCB_X(0);
// You have px pointing to an array of structures.
px = malloc(11*(sizeof(struct MCB)));
// will do nicely instead.
//pCL_B = malloc(15*(sizeof(struct CL_B))); //If 2nd malloc here, then okay!
Don't define pointers in header files!!!
//Fill the arrays of structures!
for(i=0;i<11;i++){
px[i].A0 = 10;
px[i].A0 = 10;
px[i].A0 = 10;
// You have the same assignment 3 times.
// Was it supposed to be A0, A1, A2 ?
}
// Now instead of having a global pointer just
return px;
}
You see, the mallocs have to be called *conditinally* within yyy.c because
its not everytime I need to allocate memory. Some may suggest to just
allocate them all at the begining and thats it.
I am not suggesting that.
....its just that what
I am doing gives me junk readings and I try and try and feel discouraged.
Whatever is happening is probably more to do with the fact that you're declaring pointers in header files.
Which takes me back to:
Passing it back to the function which "owns it" is the way this is done.
In my case, function1 and function2 owns the pointers to the mallocs,
right?. So is David suggesting I pass these pointers back to new pointers in
xxx.c or main and then pass them back in when I fetch function2 ???
If I understand you correctly, then yes. Something like:
struct CL_B * function2( struct MCB *px )
{
int xxx[3]; // You need a type!!!!!!!! I am guessing int.
struct CL_B *pCL_B; // Get itout of the header file.
pCL_B = malloc(15*(sizeof(struct CL_B)));
xxx[0]= px[0].A0;
xxx[1]= px[0].A1;
xxx[2]= px[0].A2;
// You don't say what you're doing with pCL_B or xxx
// but in case you don't free pCLB, return it to a function
// which can then do so:
return pCLB;
}
I'd also advise always declaring types, even if your compiler defaults to int when you don't.
Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
.
- References:
- Simple question about headers and malloc!
- From: Robby
- Re: Simple question about headers and malloc!
- From: David Webber
- Re: Simple question about headers and malloc!
- From: Ben Voigt [C++ MVP]
- Re: Simple question about headers and malloc!
- From: Robby
- Simple question about headers and malloc!
- Prev by Date: Re: MSDN volatile sample
- Next by Date: Re: MSDN volatile sample
- Previous by thread: Re: Simple question about headers and malloc!
- Next by thread: Challenging GotW 66's moral
- Index(es):
Relevant Pages
|