470,870 Members | 1,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

filling a buffer in C

I'd like to fill a buffer with different type on variables (i.e. integers, strings..).
I have a pointer to pointer data structure (char **array_of_indexed_buffers) that initialized as follows:

array_of_indexed_buffers = malloc(numOfPackets * sizeof(char *));
if(g_array_of_indexed_buffers == NULL)

for packet each entry, I have the following allocation.

for (i = 0; i < g_numOfPackets; i++)
/* allocate some memory for a buffer pointed by an array in index i */
array_of_indexed_buffers[i] = malloc(MAX_RX_TX_BUFFER_LENGTH);
if (array_of_indexed_buffers[i]==NULL)
/* erase content of allocated buffer */
bzero(array_of_indexed_buffers[i], MAX_RX_TX_BUFFER_LENGTH);

/* get piece of data from the big chunk received/created buffer and insert header parameters and bufferSize data into each array_of_indexed_buffers[i]*/
insertHeder((char *)(received_buf+(i * bufferSize)), i);

Now the problem I have is with the filling of each packet first with "index", then with bufferSize, and eventually with a data which i take from received_buf (data size is bufferSize)

void insertHeder
char *src_data_element,
int index
char *p;
bzero(g_array_of_indexed_buffers[index], MAX_RX_TX_BUFFER_LENGTH);

p = (char *)array_of_indexed_buffers[index];

*p = index ;

p = p + (sizeof (int*));

*p = (int)g_bufferSize;
p = p + (sizeof (int*));

memcpy(p, src_data_element, g_bufferSize);

the problem is I can't fill each packet in that way (with p pointer) to make it pointed by array_of_indexed_buffers[index]

Please suggest me how can I do this.

Thank you in advance,
Dec 21 '07 #1
1 6547
9,208 Expert Mod 8TB
I'd like to fill a buffer with different type on variables (i.e. integers, strings..).
If you mean buffer as in an array,then all elements must be the same type and size. Otherwise, the compiler cannot perform pointer arithmetic to locate elements.

I suggest that you use a discriminated union and have an array of that.

Expand|Select|Wrap|Line Numbers
  1. enum DataType {EMPTY, INT, FLOAT, POINTER_TO_CHAR};
  2. struct Data
  3. {
  4.      unsigned int dt;
  5.      union
  6.      {
  7.           int i;
  8.           float f;
  9.           char* ptrc;
  10.      };
  11. };
The value in dt is used to remember what was put inside the union.

Then you work your entire appication using DataType variables rather than use the built-on types.

Check out what Microsoft did with their VARIANT.
Dec 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Andrey | last post: by
1 post views Thread by inkapyrite | last post: by
1 post views Thread by jobi.joy | last post: by
3 posts views Thread by crjunk | last post: by
4 posts views Thread by aki | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.