Home > Software engineering >  Realloc a Pointer, where to say 'it's a pointer'?
Realloc a Pointer, where to say 'it's a pointer'?

Time:05-29

I have one problem using realloc. I am unable to realloc an array of int.

Now that the realloc is fixed, my only problem is that I am not able to trigger the for. Here is it at the moment:

int insereAIndice(int* Tab, int index, int insertion, int taille) {
    Tab = realloc(Tab, taille * sizeof (*Tab));
    int i;
    for (i = taille-1; i <= index 1 ; i--) {
        printf("%d", Tab[i]);
        Tab[i] = Tab[i-1];
    }
    return *Tab;
}

Here is the "important" line:

*Tab = realloc(*Tab, taille * sizeof (*Tab));

And here is the rest of the code:

Main.c

#include "functions.h"
#include "functions.c"

/*       TP 3 - ESIEE-IT Rémy JARDIN        */

int main() {
    int saisie, index, insertion, taille;


    // Création du tableau avec des valeurs aléatoires.
    printf("Creation du Tableau. \nNombre de caractere du tableau : ");
    scanf("%d", &saisie);

    int *Tab = ArrayCreate(saisie);

    printf("Voici le tableau nouvellement creer : \n");
    Affichage(Tab, saisie);


    // Insertion d'une valeur 'insertion' à l'index 'index'. Avec décallage.
    taille = saisie;

    printf("\n ! Mode Insertion ! \nNouvel Endroit souhaiter a partir de 0 : ");
    scanf("%d", &index);

    printf("Nouvelle valeur souhaiter : ");
    scanf("%d", &insertion);

    insereAIndice(Tab, index, insertion, taille);
    Affichage(Tab, saisie);
 
    free(Tab);
    return 0;
}

functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <stdio.h>
#include <stdlib.h>

int *ArrayCreate(int saisie);
int randomizer(void);
int insereAIndice(int* Tab, int index, int insertion, int taille);
int Affichage(int* Tab, int max);

#endif

functions.c

#include <stdio.h>
#include <stdlib.h>
#include "functions.h"


int *ArrayCreate(int saisie) {
    int i;
    int *Tab = malloc( saisie * sizeof (*Tab));
    if (Tab==NULL) {
        printf("Not enough Memory");
        exit (1);
    }
    for (i = 0; i < saisie; i  ) {
        Tab[i] = randomizer();
    }
    return Tab;
}


int randomizer() {
    return 1   rand() % 100;
}


int insereAIndice(int* Tab, int index, int insertion, int taille) {
    *Tab = realloc(*Tab, taille * sizeof (*Tab));
    int i;
    for (i = taille; i < index ; i--) {
        printf("\n - %d", Tab[i]);
    }
    return *Tab;
}


int Affichage(int* Tab, int max) {
    // Affichage
    int i;
    printf("\n Resultats : ");
    for (i = 0; i < max; i  ) {
        printf("%d - ", Tab[i]);
    }
    return *Tab;
}

And this is what GCC gives me:

functions.c: In function 'insereAIndice':
functions.c:26:20: warning: passing argument 1 of 'realloc' makes pointer from integer without a cast [-Wint-conversion]
     *Tab = realloc(*Tab, taille * sizeof (*Tab));
                    ^
In file included from functions.h:5:0,
                 from main.c:1:
f:\logiciel\mingw\include\stdlib.h:486:40: note: expected 'void *' but argument is of type 'int'
 _CRTIMP __cdecl __MINGW_NOTHROW  void *realloc (void *, size_t);
                                        ^~~~~~~
In file included from main.c:2:0:
functions.c:26:10: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
     *Tab = realloc(*Tab, taille * sizeof (*Tab));
          ^

Thank you !

CodePudding user response:

The first argument to realloc is a pointer. Tab is declared as an int *, so that means *Tab is an int which is why it doesn't match the type of the parameter. This also means the return type doesn't match what you're assigning it to.

So pass the pointer instead of what it points to.

Tab = realloc(Tab, taille * sizeof (*Tab));

But on the following lines another problem becomes apparent:

for (i = taille; i < index ; i--) {
    printf("\n - %d", Tab[i]);
}

The array has been resized to have taille elements, so this loop reads past the end of allocated memory. You presumably want this instead for the allocation:

Tab = realloc(Tab, index * sizeof (*Tab));

So then you're not longer reading past allocated memory (assuming the array was resized to be larger), but now you're reading uninitialized values.

  • Related