0

I'm using the realloc function in my program but it is not working properly.

Indeed, the call to realloc work 2 times out of 3 during the last call of realloc i get the following message: "* Error in `./sat': realloc(): invalid next size: 0x0000000000647520 * Aborted (core dumped)"

here is the part of my code that contains the call to realloc:

void insertInAssignedLitArray(int indiceVar, int indiceClause, int nbDeletedLiterrals, lit_t** delLit, clause_t* ptr, int signe, int nbClauseNonSat)
{
*delLit = (lit_t*)realloc(*delLit, nbDeletedLiterrals+1);
if (*delLit == NULL)
{
    fprintf(stderr, "Erreur reallocation lors de la suppression du litteral\n");
    exit(EXIT_FAILURE);
}
//some tasks.
}

I have already used some printfs and the values of nbDeletedLitterals are ok.

This function is called in another function, but i don't use delLit anywhere else.

Thanks in advance.

ryuzakinho
  • 1,891
  • 3
  • 21
  • 35
  • That isn't much to go on. – Scott Hunter Dec 26 '14 at 01:48
  • can you show the code where you originally malloc'd? – softwarenewbie7331 Dec 26 '14 at 02:08
  • It's not the fall that kills you, but it *is* the reason you're in trouble. You're corrupting the heap *before* the `realloc()` that raises the error. – EOF Dec 26 '14 at 02:14
  • I never used malloc because i needed to realloc dynamically. Why would it work twice and not the third time. Am i making a mistake (corrupting the heap) between the 2 and 3 realloc ? – ryuzakinho Dec 26 '14 at 02:17
  • 1
    Heap corruption is hard to debug. Try running your program in `valgrind`. – user253751 Dec 26 '14 at 02:20
  • 1
    At a guess though... is `lit_t` a typedef of `char`? Because if not, you're allocating `nbDeletedLiterrals+1` *bytes*, not `nbDeletedLiterrals+1` literals. – user253751 Dec 26 '14 at 02:21
  • Here is lit_t typedef struct { int varNum; int litValue; int nbOccurences; int* litOccurences; }lit_t; – ryuzakinho Dec 26 '14 at 02:22
  • @immibis, i just installed valgrind and used memcheck. Well, this looks ugly :p . I thnik it will take some time to go through all the errors. – ryuzakinho Dec 26 '14 at 02:36
  • 1
    @ryuzakinho: How many `lit_t`s does `realloc(*delLit, 1)` allocate? (Answer: Zero. It allocates one *byte*). If that's the only problem in your code then it should be easy to fix. – user253751 Dec 26 '14 at 02:44
  • @immibis you rock mate. I should have used nbDeletedLiterrals+1*sizeof(lit_t) It works now Thanks a lot (y) – ryuzakinho Dec 26 '14 at 03:04
  • 1
    @ryuzakinho Still wrong - that's the same as `nbDeletedLiterrals + (1 * sizeof(lit_t))` but you want `(nbDeletedLiteralls + 1) * sizeof(lit_t)` – user253751 Dec 26 '14 at 03:07
  • Indeed, that's what i used in my code. Thanks again mate. (for realloc and for Valgrind, it looks to be useful). – ryuzakinho Dec 26 '14 at 03:14
  • Just a tip, you should assign the return value of realloc to a temp pointer. If allocation fails, you'll lose the pointer to the original memory. Once you know that allocation is successful, assign *delLit to the value of the temp pointer. – superultranova Dec 27 '14 at 00:14
  • @superultranova Thank you very much mate, I believe this might be useful in some situations.especially when I need to perform other tasks even if my realloc call don't work well. – ryuzakinho Dec 27 '14 at 12:44
  • @ryuzakinho you're welcome. The typical case where that comes in handy is to free the original block of memory if the realloc fails. If realloc succeeds, of course it frees the original memory. However, if it fails, it will leave the memory allocated, and without a temp point, set your pointer to the memory to null. If you use a temp pointer, you can free the old block upon failure. – superultranova Dec 27 '14 at 18:57

1 Answers1

0

How many lit_ts does realloc(*delLit, 1) allocate? (Answer: Zero. It allocates one byte). If that's the only problem in your code then it should be easy to fix. – immibis

Armali
  • 18,255
  • 14
  • 57
  • 171