Monday, June 9, 2008

Memory allocation in C

Had a nice discussion with Ilpo today.

Write down some experience.

char SendBuf[10][SIZE];

actually allocate a continuous memory.
At first I thought it's like

char *pt[10];

so that I could manipulate on the pointer pt[0] pt[1] ...
and pass it to another function.

No, that doesn't work.

To solve my program, I did a small trick:
int i;
char *pt[10];
char SendBuf[10][SIZE];

for(i=0;i<10;i++ )

after that, I can use "pt" to do my tricks :)

Another note is that when use the
definition of struct

like: struct A example
we need to memset the 'example' before use it.
Because the stack in memory may leave some garbage information in the struct which can lead to unexpected failure. Like the one I encounter in the PSE project.

The msgsend struct always fail in sending msg because there is a garbage bit in the stack.

thank u Ilpo.

No comments: