passing pointer to function and using realloc

ryuzakinho

I want to pass a pointer to a function which will call a second function that will use realloc. The issue is that realloc is returning NULL.

I don't know if the mistake is in the numbers of * in the function call or something else.

Could you please help me ?

The code:

int main(){

// some code.
clause_t* ptr; //clause_t is a structure i declared.

//Some work including the initial allocation of ptr (which is working).

assignLonely(matSAT, ic.nbClause, ic.nbVar, ptr); //the issue is here.

//Some other work
}

void assignLonely(int** matSAT, int nbClause, int nbVar, clause_t* ptr)
 {

   int i = 0, j = 0;
   int cpt = 0;
   int indice = -1; 

for (i = 0; i < nbClause ; ++i)
{
    j = 0;
    cpt = 0;
    while((j < nbVar) && (cpt < 2))
    {
        if (matSAT[i][j] != 0)
        {
            cpt++;
        }
        else
        {
            indice = j;
        }

        if (cpt < 2)
        {
            deleteClause(indice, &ptr);
        }

        j++;
    }
}
}

void deleteClause(int indiceClause, clause_t** ptr)
{
    int i = indiceClause;
    int nbElt = sizeof((*ptr))/sizeof((*ptr)[0]);
    int tailleElt = sizeof((*ptr)[0]);

while(i+1 < nbElt)
{
    (*ptr)[i] = (*ptr)[i+1];
    i++;
}

*ptr = (clause_t*)realloc(*ptr, (nbElt-1)*tailleElt);
if (*ptr == NULL)
{
    fprintf(stderr, "Erreur reallocation\n");
    exit(EXIT_FAILURE);
}

}
Vlad from Moscow

You have to declarae function assignLonely similarly to function deleteClause like

void assignLonely(int** matSAT, int nbClause, int nbVar, clause_t** ptr);

if you want that changes of ptr in the function would be stored in the original object in main.

Also take into account that this statement

int nbElt = sizeof((*ptr))/sizeof((*ptr)[0]);

is wrong. Expression sizeof((*ptr)) will return the size of the pointer. Pointers do not keep information about how many elements in arrays they point to.

So expression

(nbElt-1)

can be equal to zero or even be negative.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing a pointer as a function argument

From

passing function pointer to the C code using cgo

From

Passing capturing lambda as function pointer

From Dev

Passing function pointer in Dlang

From Dev

C++ passing function pointer

From Dev

Passing Function Pointer in C#

From Dev

Passing a typedef method as pointer function

From Dev

Passing state to function pointer

From Dev

Passing pointer to a function callback

From Dev

Problems updating pointer doing realloc using a structure

From Dev

Passing a pointer to an array in a function in C

From Dev

Passing pointer or array to function

From Dev

Python -passing imgdata pointer to a function in C using ctypes

From Dev

Passing pointer to function

From Dev

Realloc pointer to a char array in function

From Dev

Passing global pointer to function

From Dev

Passing char pointer as argument to function

From Dev

Passing a pointer to a function

From Dev

Passing a pointer to a function expecting a vector

From Dev

Passing a pointer (string) to a C function

From Dev

After using realloc the next pointer in array is lost

From Dev

Passing a vector, queue, and pointer to a function

From Dev

Troubles using realloc function

From Dev

Passing default string pointer to function

From Dev

Passing a pointer to a list, to a function

From Dev

C - Passing pointer to function

From Dev

SegFault while using Malloc/Realloc on an uninitialized pointer

From Dev

Javascript addEventListener passing function pointer

From Dev

Comparison - passing array and pointer to a function