473,480 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

filling a buffer in C

1 New Member
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 7388
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
2730
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
1629
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
6224
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
1773
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
2082
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
1991
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
1130
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
3935
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
3836
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...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
574
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.