Re: Malloc code



Robby wrote:

Replies inline:

Hello David Wilk.

David, excuse my ignorance, but what do you mean by top-post? :-(

If you look it up in Google, Google Groups or Wikipedia you will find all you need to know. These Microsoft newsgroups are very tolerant of different posting styles (a bad thing, IMHO), but no matter which style you use you should take care to trim previous posts.

2. I did notice that there were several hard coded constants in your code (11, 12, 15, 16, 176) and wondered what their purpose was.

David: can you let me know where you see this? And its in the clearer code sample that you see this, right? I know that I assign bogus numbers in the TCP_LOAD_MCB_X() function. Is this what you mean?

In your code I see the following lines:

int xxx[12];
pMCB1 = malloc(176*(sizeof(struct MCB)));
for(i=0;i<11;i++)
int xxx[16];
pCL_B = malloc(15*(sizeof(struct CL_B)));

There is no indication what these hard-coded numbers signify, and at least some of them are surely typos.

You could have gotten rid of the particular problem you had by doing #define NMAX 11 /* or 12 ? */ pMCB1 = malloc(NMAX*(sizeof(struct MCB))); for (i=0; i<NMAX; i++)

Yes indeed I could of done this, but actually do it in the full blown project code!

Then why not do it in the example code? You should make your example code as clear as you can possibly make it. Your posted code had so many things wrong that it was very difficult to see what the real problem was.

Although I did guess that xxx was being used only for debugging purposes, it would have been nice if this had been indicated in the code: int xxx[12]; /* for debugging only */

You bet I will indicate this next time! I think I confused alot of people with that !

I didin't know that we can insert a file in our posts! I will need to figure out how to do this.

I was not suggesting that you attach a file (this is generally frowned on in usenet). Rather you should combine all your code into a single file, check that it still compiles and illustrates your problem, and then paste the whole file into your post. As it was, if a reader wanted to test your code he/she had to pick different parts of your post out into separate files, and add them all to a vc project. Too much trouble.

6. I understand that you are using some embedded compiler, but this is a vc group. It would be god if you were to try your code in vc, and see if the problem is still there. If not, it is unlikely that anybody here can help you with it; but if so, this may be a big clue.

I thought of doing that, but you see, its 2 years I have not done any vc++ because, I really had to get the embeded stuff of my project done first (man, I didn't think it would take me so long). So, when I posted this post a couple of days ago, I did try it in vc++ by reproducing the three files and copying the code, but it gave me over 150 errors. And then with all of the discouragment I was going through with this post, I just gave up and closed vc++. But, I really can't wait to pick it up again in the near future... theres so much I want to learn.

If you combined all your code into a single file, then you would just create an empty console project and add this file to it. Remember, someone who is really going to test your code is going to have to do this, so shouldn't you do it also?

Thanks for the clue... But this site is very good and there is alot of resourcefull help. You guys also helped me alot with vc++. I apologize if this post was so unclear but this was a nasty one.

Also, since David Web. has reccomended to not declare pointers in my header, this sort of complicated things where I must now assign all the pointers returned from malloc to an array of pointers, and then return this array of pointers back to main, which I have never done before. I did start coding it. I know how to pass in an array of pointers. But don't know how to return an array of pointers. So I just returned a void pointer and assign the value to another pointer declared in main. I read up on some samples which showed some examples but didn't show how to do this exactly the way I needed it. This is what I came up with and it compiles but I did not test it yet. This will be done tomorrow. I was also curious, is there another way where I can pass back the actual array of pointers from the function instead of a void pointer? Anyways here is what I have done.

void *FX()
{
struct MCB *MCBptrs[3]; //Array of pointers
MCBptrs[0] = malloc(10*(sizeof(struct MCB)));
MCBptrs[1] = malloc(20*(sizeof(struct MCB)));
MCBptrs[2] = malloc(30*(sizeof(struct MCB)));
return MCBptrs;
}

The reason that I suggested to you (some time back) to return void* was that you were returning pointers to three different structs from the same function (a bad design, IMHO). You are not doing that any more. If you want to assign a whole array of pointers, you can do

void FX(struct MCB* MCBptrs[3], int flag)
{
if(flag == 0)
{
MCBptrs[0] = malloc(10*(sizeof(struct MCB)));
MCBptrs[1] = malloc(20*(sizeof(struct MCB)));
MCBptrs[2] = malloc(30*(sizeof(struct MCB)));
}
else
{
free (MCBptrs[0]);
free (MCBptrs[1]);
free (MCBptrs[2]);
}
}


void main()
{
int *gptr;
struct MCB *r1;
struct MCB *r2;
struct MCB *r3;

gptr = (int *) FX();

r1 = *gptr[0];
r2 = *gptr[1];
r3 = *gptr[2];

r1[0].LEADING_PIX;
r2[2].LEADING_PIX;
r3[7].LEADING_PIX;

free(*gptr[0]);
free(*gptr[1]);
free(*gptr[2]);
}

It's int main(), not void main().

int main()
{
struct MCB* MCBptrs[3];
FX(MCBptrs, 0); /* allocate */
/* do work here */
FX(MCBptrs, 1); /* free */
return 0;
}

I never really coded in C, but in C++ I never use global variables. It is (almost) always possible to pass required information in function arguments.

Thankyou for your post, hope you had a nice new year's eve!

A good year to you also, Robbie.

--
David Wilkinson
Visual C++ MVP
.



Relevant Pages