I've been pouring over this for over a day now and still have no solution, so hopefully I'll get one here
I have a struct
Code:
struct MemoryStruct {
char *memory;
size_t size;
};
which contains a large string with several newlines and the size of that string in bytes.
The program can read the string fine, and it works with printf no problem. However when I try to alter it (eg. using strcat) or use strtok to parse it (which is what i need to do), I get a segmentation fault at runtime. This also happens if I copy the data to a new string and try to perform the same operations on it.
I've pasted the relavent bits of code below:
Code:
char tmp[4],url[255]="",fileline[255]="",*thedata,tmpstr1[2048];
struct MemoryStruct chunk;
chunk.memory=NULL;
chunk.size = 0; // no data at this point
//...
// read data into chunk...
// ...
chunk.memory=&chunk.memory[1]; // strip initial newline char.
strncpy(tmpstr1, chunk.memory, strlen(chunk.memory) - 713); // strip adverts
strcpy(chunk.memory,tmpstr1);
// then either I get a segfault from any kind of write access.
// I have tried appending '\0' to the end of the string after chopping the last 713
// chars off, but that doesn't make any difference.
Thanks in advance
- Mitch