• C

How to build a buffer in C

I've got this:

int      bufsize = 400; /*Size of accepting packet*/
int      BUFFER_PACKET = 10; /*Number of pcakets to buffer before send */
int BUFFER_COUNTER = 0; /*Counter for travesing buffer */
int BUFFER_FULL = 0; /*Sets to 1 if full, and sends packets to app */
char* bufArray;

/* Initiate bufArray */
char* bufArray[BUFFER_PACKET];
for(BUFFER_COUNTER=0; BUFFER_COUNTER < BUFFER_PACKET; BUFFER_COUNTER++ )
{
   //TODO: Need check for enough memory
   bufArray[BUFFER_COUNTER] = (char *) malloc(bufsize);
}
BUFFER_COUNTER = 0;

When I send the packet (which is a custom struct), I do like this:
sendfunc((char *)(message + 1), ntohs(message->xcast_len));    which calls extern DATAGRAM function..

I would like to build a buffer like this:

if (BUFFER_FULL == 0)
{
   debug("Start filling up buffer %i\n", BUFFER_COUNTER);
   memcpy(bufArray[BUFFER_COUNTER],(char *) message, bufsize); //Buffer size is 400,
   BUFFER_COUNTER++;
   if (BUFFER_COUNTER == BUFFER_PACKET)
   {
         BUFFER_FULL = 1;
         BUFFER_COUNTER = 0;
   }
}
else
{
   while(BUFFER_COUNTER != BUFFER_PACKET)
   {
         if (sendfunc ((bufArray[BUFFER_COUNTER] + 1), sizeof(bufArray[BUFFER_COUNTER])) == -1)
         {
               printf ("Failed to send \n");
               return;
         }
         BUFFER_COUNTER++;
   }
   if (sendfunc ((char *)(message + 1), ntohs(message->xcast_len)) == -1)
   {
         printf ("Failed to send \n");
         return;
   }
}

The last function works great if I comment out the "buffer"...

Could anybody please help me???

reagards
mamruocAsked:
Who is Participating?
 
ageraldnaveenConnect With a Mentor Commented:
yes memcpy wont work, becoz you might endup to read beyond the actual length of the message..bcoz you always copy "BUFSIZE" bytes from the "message".. that could be the reason.. so change that to

memcpy(bufArray[BUFFER_COUNTER],(char *) message, ntohs(message->xcast_len));

and ensure that ntohs(message->xcast_len) is less than or equal to BUFSZIE
and also maintain that size array...hope everything would go smooth now.
0
 
mamruocAuthor Commented:
What I don't understand is basically why I can't do the

memcmp??

regards
0
 
mamruocAuthor Commented:
No experts here to help me? Please??
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
PaulCaswellConnect With a Mentor Commented:
First:

/* Initiate bufArray */
char* bufArray[BUFFER_PACKET];

wont work as BUFFER_PACKET has been defined as an integer variable, not a number.

We need MUCH more information to help.

>>What I don't understand is basically why I can't do the memcmp??

I dont see any memcmp.

Give us some more information about the problem such as platform, compiler verfsion etc.

Paul
0
 
ageraldnaveenCommented:
"The last function works great if I comment out the "buffer"... --- this statement is not clear.. which function??? whats that "buffer"???
0
 
mamruocAuthor Commented:
@PAul: Will it work if I've used const or DEFINE, or do I have to use a number instead...

I ment memcpy, not memcmp....

It compiles with this notice:
passing arg 1 of `memcpy' makes pointer from integer without a cast

Platform, Linux, Debian, kernel 2.4.26 precompiled. gcc version 3.3.4 (Debian 1:3.3.4-7)...

regards...
0
 
mamruocAuthor Commented:
@ageraldnaveen

The whole thing I pasted in is the "buffer"....

What I ment is if I only used the last if sentance:

   if (sendfunc ((char *)(message + 1), ntohs(message->xcast_len)) == -1)
   {
        printf ("Failed to send \n");
        return;
   }

It works great, then I only send packets instead for buffering 10 packets before I push them out on net....

regards
0
 
PaulCaswellCommented:
The problem is probably tyhat you are not sending anything until all buffers are full.

A double-buffered system usually needs some form of dual-threading where one thread fills the buffer while the other empties it. This would be quite a significant amount of reengineering. What was the problem that made you believe that double-buffering was the best way out.

Paul
0
 
PaulCaswellCommented:
I just noticed another problem:

>>       if (sendfunc ((bufArray[BUFFER_COUNTER] + 1), sizeof(bufArray[BUFFER_COUNTER])) == -1)

'bufArray[BUFFER_COUNTER]' is a 'char *' so sizeof(bufArray[BUFFER_COUNTER]) will probably not be what you want. Try:

>>       if (sendfunc ((bufArray[BUFFER_COUNTER] + 1), bufsize ) == -1)

incidentally, why are you using 'bufArray[BUFFER_COUNTER] + 1'? What is the '+ 1' for?

Paul

0
 
mamruocAuthor Commented:
Actually it's the memcpy that don't work, I get

4058 Segmentation fault.

The reason why I need to build a buffer is I'm sending audio packets on a ad-hoc network.. and using a protocol which is sending udp packets only. when it rece

a. The capacity on the wireless net isn't too good..
b. packets can be dropped without any reaction from either sender nor receiver...

I want to buffer 10 packets before I send it to a extern application (not on the net), So it can start stream immediately when it recieves datapackets....

Which buffer built in like this, I can plug (hopefully) any applications on the C  code and don't nedd to do the buddering at application level...
0
 
ageraldnaveenCommented:
what Paul set is right... that sizeof would return sizeof(char*) and not the size of the array.

even then, I dont think u shall pass the "bufsize" as arg to sendfunc,
i think u should also parallelly maintain the size(s) in another array containing..

ntohs(message->xcast_len));   // for every corresponding msg in bufArray

and ur call shall be like

sendfunc((bufArray[BUFFER_COUNTER]+1), sizeArray[BUFFER_COUNTER]))

which would send in the actual size...not the buffer size.

also, Are u sure that, your buffersize is sufficiently large enough to hold all the required data.???
0
 
PaulCaswellCommented:
ageraldnaveen is right but you will need to store the size of the message in the buffer along with the message itself. Is that where the '+1' came from? Is the first byte of the message a length?

Paul
0
 
mamruocAuthor Commented:
ntohs(message->xcast_len) reports 400,

bufArray[0] is initated which size of 400, right? but it don't work... :S
0
 
PaulCaswellCommented:
Have you checked your:

>>   //TODO: Need check for enough memory

This seems an unlikely cause but lets rule it out first.

Paul
0
 
PaulCaswellCommented:
What platform are you working on?

Paul
0
 
mamruocAuthor Commented:
hehe... jepp...

I just added that-...

Now.. everything work....

the '+1' was just from a code I used above...

What seems to be the problem was:

int     bufsize = 400; /*Size of accepting packet*/
int     BUFFER_PACKET = 10; /*Number of pcakets to buffer before send */
int BUFFER_COUNTER = 0; /*Counter for travesing buffer */
int BUFFER_FULL = 0; /*Sets to 1 if full, and sends packets to app */
char* bufArray;   <----------------- PROBLEM!!!!

I changed to char* bufArray[10];

and deleted the char* bufArray[BUFFER_PACKET];

and changed the send (and memcpy) to reflect the real size, not (char *)...

:D

Thanks alot folks!
0
 
mamruocAuthor Commented:
As mentioned before:

 @PAul: Will it work if I've used const or DEFINE, or do I have to use a number instead...

I ment memcpy, not memcmp....

It compiles with this notice:
passing arg 1 of `memcpy' makes pointer from integer without a cast

Platform, Linux, Debian, kernel 2.4.26 precompiled. gcc version 3.3.4 (Debian 1:3.3.4-7)...

regards...
0
 
mamruocAuthor Commented:
To you two... What do you think would be fair to do??

Split the points on you two???


regards...
0
 
PaulCaswellCommented:
RE: passing arg 1 of `memcpy' makes pointer from integer without a cast

Remove:

>>char* bufArray;

Paul
0
 
PaulCaswellCommented:
>>Split the points on you two???

Have you solved the problem then?

Paul
0
 
mamruocAuthor Commented:
yes.. If you read above, you'll se that I just solved it...

It was (as usual) silly mistakes...

0
 
PaulCaswellCommented:
>>yes.. If you read above, you'll se that I just solved it...

I'd suggest you decide if either of us guided your mind to the problem or the solution. If not, just split equally. Does that seem fair to you ageraldnaveen?

Paul
0
 
mamruocAuthor Commented:
I splitted...

Thanks alot...

regards!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.