-1

Whats wrong with this function, which is expected to add a row and a column to given 2D array? Matrix is symmetric.

void updateMatrix(double ***mat, int size, double *vec)
{ // mat is sizeXsize matrix, length of vec is size+1
    *mat = (double**)realloc(*mat, (size + 1)*sizeof(double*));
    (*mat)[size] = (double*)malloc((size + 1)*sizeof(double));

    for(int i = 0; i < size + 1; i++) {
        (*mat)[size][i] = vec[i];
    }

    for(int i = 0; i < size; i++) {
        (*mat)[i] = (double*)realloc((*mat)[i], (size + 1)*sizeof(double));
        (*mat)[i][size] = vec[i];
    }
}
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Palash Kumar
  • 429
  • 6
  • 18
  • 1
    It's hard to tell, what problems are you experiencing? Do you have build errors? Run-time errors or crashes? Unexpected results? Something else? – Some programmer dude Apr 30 '15 at 08:10
  • 2
    Also, don't reassign the pointer you're reallocating to itself, think about what would happen if `realloc` fails and returns `NULL`. – Some programmer dude Apr 30 '15 at 08:10
  • 1
    Then run in a debugger to locate the crash in your code. And step through the code you think is faulty to see that it does exactly what you think it is. – Some programmer dude Apr 30 '15 at 08:11
  • @JoachimPileborg I have tried that.. still after few calls, this particular function segmentation fault. – Palash Kumar Apr 30 '15 at 08:12
  • `vec` is of `size` or `size+1` ? Your for loops use both. – Rohan Apr 30 '15 at 08:14
  • 1
    *Where* does it crash? On which line? – Some programmer dude Apr 30 '15 at 08:14
  • 1
    The problem could be in the calling function. If size increases by more 1 on successive calls you will end up with rows containing uninitialized pointers. – samgak Apr 30 '15 at 08:15
  • This isn't a 2D array, it is a lookup table. Arrays are allocated in adjacent memory cells. Is there any particular reason why you like to have your data fragmented all over the heap? If not, [read this](http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c). – Lundin Apr 30 '15 at 08:16
  • fails at first line... valgrind says - Invalid free() / delete / delete[] / realloc() – Palash Kumar Apr 30 '15 at 08:59
  • This part of the code seems right...except that the advice of @JoachimPileborg should be followed. See [the documentation](http://www.cplusplus.com/reference/cstdlib/realloc/) for a correct way to use `realloc()` and check if it returns `NULL`. Moreover, it is stated that the first argument of `realloc()` should be a `Pointer to a memory block previously allocated with malloc, calloc or realloc. Alternatively, this can be a null pointer, in which case a new block is allocated (as if malloc was called).`. Is `*mat` set to `NULL`, or a return of `malloc` the first time a row/col is created ? – francis Apr 30 '15 at 10:59
  • Thanks everyone. The above code is correct (at least logically). Issue was in the calling part, where I was sending the copy of A (2d pointer) to the caller of this function instead of &A. – Palash Kumar Apr 30 '15 at 12:04
  • @PalashKumar Please answer the question( you can answer your own questions ), delete it, or flag it for deletion. – 2501 Apr 30 '15 at 13:09

1 Answers1

0

Your realloc is returning NULL in the second for loop.. I'm trying to figure out why.

Have you allocated everything before hand? Because chances are you might be passing a non NULL and non malloced pointer to realloc. And that is forbidden/will cause errors

Or, as says @MichaelDorgan , you could just be passing a gigantic size to your function but i sincereley hope you are not trying to allocate a few Go for an array. Otherwise i'm curious as to its use.

Khaldor
  • 294
  • 1
  • 16
  • Or he just might be passing in a gigantic size. Proper if / handling code around allocations would answer some of his questions for sure. – Michael Dorgan May 01 '15 at 18:44