mardi 23 mai 2017

Dynamic Char Allocation

I have a loop that brings me a buffer each time, i want to concatenate the buffers in a char named data and after i will write this data in a file, but the problem is that i can't concatenate the char without seg-fault/core-dumped. Here's the code (also tried new char[sizeof] out malloc):

int size = getFrames() * 4;
char * buffer = (char *) malloc(size);

char * data;
char * tmp;

for (loops = 100; loops > 0; loops--) {
    buffer = getBuffer();

    tmp = (char *) malloc(sizeof(data));
    tmp = data;

    data = (char *) malloc(sizeof(tmp) + sizeof(buffer));
    data = strcat(tmp, buffer);
}

free(tmp);
free(data);
free(buffer);

Aucun commentaire:

Enregistrer un commentaire