-3

I am using a char * array to make a buffer to move information from multiple mapping threads to a reducing thread. I need to make the array circular, however, i keep getting segmentation faults when the array runs out of room. How do I make the array circular? I currently have

for(j = 0; j < i; j++){
    int next = mr->nextIndex + j;
    if(next > 1023){
        next = 0;
    }
    mr->buffer[next] = temp[j];
}

The array is set up as,

new_mr->buffer = malloc(sizeof(char *) * MR_BUFFER_SIZE);

with the macro being 1024. Any help is appreciated.

temp is

char *temp = malloc(sizeof(char *));

and it gets its value from

memcpy(temp, kv, i);

and kv is passed into the function from the main.

2 Answers2

0

This is wrong:

char *temp = malloc(sizeof(char *));

You're storing some data into there using memcpy() but the storage space is only sizeof(char*) which is 4 or 8 bytes. You probably meant to use some other size there, e.g. the value of i you pass to memcpy().

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0
char *temp = malloc(sizeof (char *));

should be

char *temp = malloc(sizeof (char) * i); // sizeof (char) can be omitted

because temp is expected to point to an array of char.


mr->buffer[next] = temp[j];

should be

mr->buffer[next] = &temp[j];

because mr->buffer[next] is of type char *.

nalzok
  • 14,965
  • 21
  • 72
  • 139