Re: Simple question about headers and malloc!

Tech-Archive recommends: Fix windows errors by optimizing your registry




"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


.



Relevant Pages

  • Re: Array of pointers to structs
    ... > my library routines to know when to stop processing the array. ... > typedef struct screen_disp ... > various ways of declaring 'display_scr' as well and nothing seems to be ... not an array of pointers to the struct. ...
    (comp.lang.c)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)
  • Re: porting problems encountered
    ... Tru64 compiles long variables to size 8 bytes while VMS and HP-UX ... platform, the size of the structure would be 12 bytes. ... when it got to the AS/400 with 128 bit pointers. ... Using the struct from before: ...
    (comp.os.vms)
  • Re: strict aliasing rules in ISO C, someone understands them ?
    ... I understand now that my interpretation of the standard was totally ... an aggregate is a struct or an array. ... struct s1 {int i; double d;}; ... Pointers are just a means of accessing the objects, ...
    (comp.lang.c)
  • Re: Array of pointers in a struct
    ... >typedef struct trieNode ... This is an array of 27 pointers. ... >And this is the code I make a new trie node, initialize and return it. ...
    (comp.lang.c)