[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to build a buffer in C

Posted on 2004-08-04
23
Medium Priority
?
406 Views
Last Modified: 2011-09-20
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
0
Comment
Question by:mamruoc
  • 11
  • 9
  • 3
23 Comments
 

Author Comment

by:mamruoc
ID: 11716326
What I don't understand is basically why I can't do the

memcmp??

regards
0
 

Author Comment

by:mamruoc
ID: 11716462
No experts here to help me? Please??
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 375 total points
ID: 11716644
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:ageraldnaveen
ID: 11716652
"The last function works great if I comment out the "buffer"... --- this statement is not clear.. which function??? whats that "buffer"???
0
 

Author Comment

by:mamruoc
ID: 11716700
@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
 

Author Comment

by:mamruoc
ID: 11716719
@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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11716779
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11716831
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
 

Author Comment

by:mamruoc
ID: 11716854
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
 
LVL 1

Expert Comment

by:ageraldnaveen
ID: 11716914
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
 
LVL 1

Accepted Solution

by:
ageraldnaveen earned 375 total points
ID: 11716958
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11717008
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
 

Author Comment

by:mamruoc
ID: 11717010
ntohs(message->xcast_len) reports 400,

bufArray[0] is initated which size of 400, right? but it don't work... :S
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11717165
Have you checked your:

>>   //TODO: Need check for enough memory

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

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11717208
What platform are you working on?

Paul
0
 

Author Comment

by:mamruoc
ID: 11717233
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
 

Author Comment

by:mamruoc
ID: 11717242
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
 

Author Comment

by:mamruoc
ID: 11717245
To you two... What do you think would be fair to do??

Split the points on you two???


regards...
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11717279
RE: passing arg 1 of `memcpy' makes pointer from integer without a cast

Remove:

>>char* bufArray;

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11717286
>>Split the points on you two???

Have you solved the problem then?

Paul
0
 

Author Comment

by:mamruoc
ID: 11717323
yes.. If you read above, you'll se that I just solved it...

It was (as usual) silly mistakes...

0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11717362
>>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
 

Author Comment

by:mamruoc
ID: 11717400
I splitted...

Thanks alot...

regards!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question