I'm new to c and I have the following problem:
after executing this part of the code
TypeDefStruct tdss[10];
for(...) {
TypeDefStruct *tds = (TypeDefStruct*)malloc(sizeof(TypeDefStruct));
strcpy(&tds->data, charPointerArray[index]) // works (not original code)
memcpy((void*) &tdss[index], (void*) &tds, sizeof(TypeDefStruct)); // new
free(tds); // new
}
an error occurs here
TypeDefStruct *tds = &tdss[0]; // worked before
printf("\twith input: \"%s\"\n", tds->data); // worked before
the TypeDefStruct:
typedef struct TypeDefStruct{
char* data;
} TypeDefStruct;
also the error sigsagv occurs at some point (don't know where, may not here at all...)
what I'm trying to do
I am trying to copy this struct into an array and manage my memory correctly.
please, don't mind asking for more information!
valgrindis the best available tool for diagnosing what has actually gone wrong in cases of memory corruption. It is very common for the true cause of such bugs to be nowhere near the point where the program crashes. 2) For us to help you, we need a minimal, complete, verifiable example -- a complete program, that we can run for ourselves and watch crash, containing no more code than is strictly necessary to reproduce the problem. You may discover the bug yourself while writing this.TypeDefStructis a terrible name for a data type. Think of something more meaningful.memcpy((void*) &tdss[index], (void*) &tds, sizeof(TypeDefStruct)); // newhas a syntax error! Since tds is a type defined struct we don't need the&