2

Here I come with my new C question, I have an array of char arrays and I want to reserve memory contiguously in memory to helps write into file with a single call.

ok, that's what I have:

#define PACKET_SIZE 40960
#define BUFFER_SIZE 3950
#define N_BUFFERS 4
typedef struct buffer{
 int lock;
 char (*rxbuf)[BUFFER_SIZE][PACKET_SIZE];
}buffer;
/* Buffer list */
buffer bufferlist[N_BUFFER];
[...]
/* I use this to init structs */
void initializeData() {
 for (int i = 0; i < N_BUFFER; i++) {
 bufferlist[i].lock = 0;
 bufferlist[i].rxbuf = malloc(sizeof(???)); /* ??? <- I don't know what to put here */
 }
}
/* After I insert data */
for (int ibuffer = 0; ibuffer < N_BUFFER; ibuffer++) {
 for (int idata = 0; idata < BUFFER_SIZE; idata++) {
 /* Here I want to pass char array[PACKET_SIZE] */
 int bytes_received = insertData(bufferlist[ibuffer].rxbuf[idata], sizeof(bufferlist[ibuffer].rxbuf[idata]));
 [...]
 }
}
[...]
/* Then write with this */
fwrite(bufferlist[i].rxbuf, sizeof(????), 1, outfile);

Please can you help me with this code?

Thanks in advance

asked Dec 19, 2013 at 12:23
2

3 Answers 3

1

Change your definition of char array in structure

char (*rxbuf)[BUFFER_SIZE][PACKET_SIZE];

to

char rxbuf[BUFFER_SIZE][PACKET_SIZE];

With this you already create char array and reserve memory, so no need to malloc().

You can do fwrite() as

fwrite(bufferlist[i].rxbuf, sizeof(bufferlist[i].rxbuf), 1, outfile);
answered Dec 19, 2013 at 12:29
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rohan! One more question, If I pass bufferlist[0].rxbuf[0] as function parameter, I'm passing char array of PACKET_SIZE this way right?
1

Use

bufferlist[i].rxbuf = malloc(sizeof(*bufferlist[i].rxbuf)); 

That means: Allocate to bufferlist[i].rxbuf as much memory as needed by the variable bufferlist[i].rxbuf is pointing to.

Following this, the call to fwrite() would be:

fwrite(bufferlist[i].rxbuf, sizeof(*bufferlist[i].rxbuf), 1, outfile);
answered Dec 19, 2013 at 14:32

Comments

1

char (*rxbuf)[BUFFER_SIZE][PACKET_SIZE]; is only a single pointer to an 2D array of sizes BUFFER_SIZE and PACKET_SIZE.

If you want contiguous memory you can either change it to an 2D array

char rxbuf[BUFFER_SIZE][PACKET_SIZE];

or to a pointer to an array

char (*rxbuff)[BUFFER_SIZE] ;
rxbuff = malloc( sizeof( char ) * BUFFER_SIZE * PACKET_SIZE ) ;

In both ways the array is accessed in the same way rxbuff[0][0] = 'a' ;

answered Dec 19, 2013 at 12:39

3 Comments

The code example is wrong. the type of the pointer must be 2D: char (*rxbuf)[BUFFER_SIZE][PACKET_SIZE]
@egur Fixed, but actually it should be char (*rxbuff)[BUFFER_SIZE].
@self In your example, how you write all array without loop?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.