472,804 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 software developers and data experts.

filling a buffer in C

Hello,
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)
{
perror("malloc");
exit(1);
}

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)
{
perror("malloc");
exit(1);
}
/* 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,
Alex
Dec 21 '07 #1
1 7260
weaknessforcats
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. };
  12.  
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

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

Similar topics

3
by: dh | last post by:
Runtime.getRuntime().exec() ... mad buffering of stdout ... It buffers stdout without limit ... so if your Java program doesn't keep up with the Process output then memory fills up! I wish it...
6
by: Andrey | last post by:
What does the standard say about zero-initializing of static structures/classes, specifically: is it guaranteed by the standard that the alignment fillers between the members will also be...
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
2
by: hennakapoor | last post by:
How do the form filling software determine which text field gets what data i.e. a typical form filling software will ask the user to enter the username / address / password etc. Whenever it goes to...
1
by: jobi.joy | last post by:
I have a requirement of having the capability of offline form filling for my website.So users can some way download the form and can fill out in offline mode. Any standard way to do this. I am...
3
by: crjunk | last post by:
I have a 3 table in my DataSet that I'm filling with data. After I've filled these 3 tables, I'm then trying to run a query that will fill a 4th table in the DataSet with data from the three...
3
by: HONOREDANCESTOR | last post by:
I am modifying some code that uses 'events' to add to a buffer. Suppose I want to write a routine that will read from the buffer at timed intervals. Is there a danger that when I read from the...
4
by: aki | last post by:
Hi all, i am writing codes for implementing network protocols. i have written a method which is receiving a packet from network. i have assumed that the packet i will receive will be of type...
0
by: martinmercy2001 | last post by:
Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.