Solved

How to build a buffer in C

Posted on 2004-08-04
23
393 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
Comment Utility
What I don't understand is basically why I can't do the

memcmp??

regards
0
 

Author Comment

by:mamruoc
Comment Utility
No experts here to help me? Please??
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 125 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:ageraldnaveen
Comment Utility
"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
Comment Utility
@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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 125 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
What platform are you working on?

Paul
0
 

Author Comment

by:mamruoc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
RE: passing arg 1 of `memcpy' makes pointer from integer without a cast

Remove:

>>char* bufArray;

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
Comment Utility
>>Split the points on you two???

Have you solved the problem then?

Paul
0
 

Author Comment

by:mamruoc
Comment Utility
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
Comment Utility
>>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
Comment Utility
I splitted...

Thanks alot...

regards!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now