Pointer assignment to dynamic array of structs
Basically all I am trying to do is assign something (a Polynomial) to a dynamic array at a given index, overwriting whatever is there. For simplicity I just made subtract() return a pointer t开发者_StackOverflow中文版o the first element in the polynomialArray. So, this code should copy the contents of the first element and replace another element (I know that a deep copy is necessary, and that is implemented in subtract). I have been spoiled by java (and you kind folks)...
When I go to print it at the index at which it is copied to, there is nothing there.
Usually something like Poly1 = 2x^3 + 4x
would print, but instead it just prints Poly1 =
.
Compiles fine and runs, but does not do what I need it to. EDIT: Runs fine if there wasn't anything at that index. If there is something at the index, seg fault.
//Portion of main from another file
Polynomial* subtracted = subtract(op1_index, op2_index);
insert(subtracted, diff_index);
printPolynomial(diff_index);
//Methods in question (utils file)
void insert(Polynomial* element, int index) {
if(index > num_polynomial) {
polynomialArray = (Polynomial*)realloc(polynomialArray, (index + 1) * sizeof(Polynomial));
}
free(polynomialArray[index].polynomialTerm);
polynomialArray[index] = *element; // Problem here?
}
Polynomial* subtract(int op1_index, int op2_index) {
return &polynomialArray[0];
}
//Typedefs accessible in main and utils file
typedef struct term {
int exponent;
int coefficient;
} Term;
typedef struct polynomial {
Term *polynomialTerm;
} Polynomial;
//variables accessible in utils file
static Polynomial *polynomialArray;
int num_polynomial; // counter to keep track of the number of polynomials
I think the problem is here free(polynomialArray[index].polynomialTerm);
if index > num_polynomial, then the value here polynomialArray[index].polynomialTerm
is garbage. you don't need to free it. instead you should write it something like that:
void insert(Polynomial* element, int index) {
if(index >= num_polynomial) { /* probably should be >= depends on your implementation */
polynomialArray = (Polynomial*)realloc(polynomialArray, (index + 1) * sizeof(Polynomial));
}
else
{
free(polynomialArray[index].polynomialTerm);
}
polynomialArray[index] = *element;
}
also, this is a bit risky. for a two main reasons:
- you don't increment num_polynomial if index is bigger.
- you don't know how bigger index is, so you might allocate a much bigger memory, and later, if you try to reach anything between the previous
num_polynomial
and the new size, you get to unknown area, and probably get segfault again.
I think the issue is that subtract
is returning a pointer into your dynamically allocated array, but then insert
realloc's that array, leaving the pointer returned by subtract (passed into insert as element
) dangling.
So at the line you have marked Problem here?
, element is pointing at the just implicitly freed (by realloc) array, which may well have been overwritten by the memory management system. In any case, accessing memory after its freed gives undefined behavior.
精彩评论