Partage
  • Partager sur Facebook
  • Partager sur Twitter

Adresse mémoire pas a jour (REALLOC)

20 août 2019 à 10:02:06

Bonjour, j'ai un soucis que je pense avoir localisé ( une erreur de segmentation) je lis une mauvaise adresse mémoire. Je voulais donc savoir pourquoi et j'ai fais des tests sur la fonction realloc. Et il s'avère que je n'arrive pas à récupérer l'adresse mémoire après le realloc. Pouvez vous m'aider ?

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

#define MaxFlOAT 2147483647
#define MinFlOAT -2147483648
#define tailleMinTabBranche 30

typedef struct point3
{
    float x;
    float y;
    float z;
}Point3;

typedef struct point2
{
    float x;
    float y;
    float ID;
}Point2;

typedef struct triangle
{
    Point3 normal;
    Point3 A;
    Point3 B;
    Point3 C;
}Triangle;

typedef struct face
{
    Triangle* T;
    float yMin;
    float yMax;
    Point3 vect_AB; //vecteur AB
    Point3 vect_BC; //vecteur BC
    Point3 vect_AC; //vecteur AC

    Point3 min_AB;
    Point3 max_AB;

    Point3 min_BC;
    Point3 max_BC;

    Point3 min_AC;
    Point3 max_AC;
}Face;

typedef struct segment
{
    Point2 A;
    Point2 B;
}Segment;

typedef struct cellulepoint
{
    Point2 P;
    Segment* reference;
    struct cellulepoint* suivant;
}Cellulepoint;

typedef Cellulepoint* Ptr_Cellulepoint;
typedef Cellulepoint* ListeChaineePoint;

void fonctionRealloc(ListeChaineePoint tabBranche[], int taille)
{
  tabBranche = realloc(tabBranche, taille*tailleMinTabBranche*sizeof(Cellulepoint));
  printf("fonctionRealloc : L'adresse de tabBranche est de %p\n",tabBranche);
}
int main()
{
  ListeChaineePoint* tableauBranche = NULL;

  tableauBranche = malloc(tailleMinTabBranche*sizeof(Cellulepoint));
  if(tableauBranche != NULL)
  {
    printf("MAIN: L'adresse de tableauBranche est de %p %p %p\n",tableauBranche);
    fonctionRealloc(tableauBranche,1);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);
    fonctionRealloc(tableauBranche,5);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);
    fonctionRealloc(tableauBranche,8);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);

    free(tableauBranche);
  }
  else
  {
    printf("Il y a un soucis lors de l'allocation dynamique\n");
  }
  return 0;
}



  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 10:14:56

Et le printf ligne 78, il a un sérieux problème !

-
Edité par rouloude 20 août 2019 à 10:20:40

  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 10:18:36

Ah oui désolé j'ai copier un code avec trop de %p

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

#define MaxFlOAT 2147483647
#define MinFlOAT -2147483648
#define tailleMinTabBranche 30

typedef struct point3
{
    float x;
    float y;
    float z;
}Point3;

typedef struct point2
{
    float x;
    float y;
    float ID;
}Point2;

typedef struct triangle
{
    Point3 normal;
    Point3 A;
    Point3 B;
    Point3 C;
}Triangle;

typedef struct face
{
    Triangle* T;
    float yMin;
    float yMax;
    Point3 vect_AB; //vecteur AB
    Point3 vect_BC; //vecteur BC
    Point3 vect_AC; //vecteur AC

    Point3 min_AB;
    Point3 max_AB;

    Point3 min_BC;
    Point3 max_BC;

    Point3 min_AC;
    Point3 max_AC;
}Face;

typedef struct segment
{
    Point2 A;
    Point2 B;
}Segment;

typedef struct cellulepoint
{
    Point2 P;
    Segment* reference;
    struct cellulepoint* suivant;
}Cellulepoint;

typedef Cellulepoint* Ptr_Cellulepoint;
typedef Cellulepoint* ListeChaineePoint;

void fonctionRealloc(ListeChaineePoint tabBranche[], int taille)
{
  tabBranche = realloc(tabBranche, taille*tailleMinTabBranche*sizeof(Cellulepoint));
  printf("fonctionRealloc : L'adresse de tabBranche est de %p\n",tabBranche);
}
int main()
{
  ListeChaineePoint* tableauBranche = NULL;

  tableauBranche = malloc(tailleMinTabBranche*sizeof(Cellulepoint));
  if(tableauBranche != NULL)
  {
    printf("MAIN: L'adresse de tableauBranche est de %p\n",tableauBranche);
    fonctionRealloc(tableauBranche,1);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);
    fonctionRealloc(tableauBranche,5);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);
    fonctionRealloc(tableauBranche,8);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);

    free(tableauBranche);
  }
  else
  {
    printf("Il y a un soucis lors de l'allocation dynamique\n");
  }
  return 0;
}



  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 10:20:44

tabBranche dans la fonction fonctionRealloc est une copie de la variable passée en paramètre, elle n'influe donc pas sur la variable tableauBranche que tu passes en paramètre.
  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 10:24:47

Comment faire pour que tabBranche ne soit pas une copie mais l'original ? En plus c'est une perte de mémoire importante si c'est une copie ?

Je pense que je dois envoyer un pointeur mais vu que c'est un tableau pour moi c'est deja un pointeur.

Merci d'avance

  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 10:47:13

Pour modifier une variable passée en paramètre à une fonction dans la fonction, il faut passer son adresse à la fonction. (Le cas le plus connu est la fonction scanf).
  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 10:54:47

Alors j'ai fais quelque chose mais j'ai toujours des warnings :

ReallocResolution.c:79:21: warning: incompatible pointer types passing 'ListeChaineePoint **' (aka 'struct cellulepoint ***') to parameter of type 'ListeChaineePoint *' (aka 'struct cellulepoint **');

remove & [-Wincompatible-pointer-types]

fonctionRealloc(&tableauBranche,1);

^~~~~~~~~~~~~~~

ReallocResolution.c:66:41: note: passing argument to parameter 'tabBranche' here

void fonctionRealloc(ListeChaineePoint* tabBranche, int taille)

^

ReallocResolution.c:81:21: warning: incompatible pointer types passing 'ListeChaineePoint **' (aka 'struct cellulepoint ***') to parameter of type 'ListeChaineePoint *' (aka 'struct cellulepoint **');

remove & [-Wincompatible-pointer-types]

fonctionRealloc(&tableauBranche,5);

^~~~~~~~~~~~~~~

ReallocResolution.c:66:41: note: passing argument to parameter 'tabBranche' here

void fonctionRealloc(ListeChaineePoint* tabBranche, int taille)

^

ReallocResolution.c:83:21: warning: incompatible pointer types passing 'ListeChaineePoint **' (aka 'struct cellulepoint ***') to parameter of type 'ListeChaineePoint *' (aka 'struct cellulepoint **');

remove & [-Wincompatible-pointer-types]

fonctionRealloc(&tableauBranche,8);

^~~~~~~~~~~~~~~

ReallocResolution.c:66:41: note: passing argument to parameter 'tabBranche' here

void fonctionRealloc(ListeChaineePoint* tabBranche, int taille)

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

#define MaxFlOAT 2147483647
#define MinFlOAT -2147483648
#define tailleMinTabBranche 30

typedef struct point3
{
    float x;
    float y;
    float z;
}Point3;

typedef struct point2
{
    float x;
    float y;
    float ID;
}Point2;

typedef struct triangle
{
    Point3 normal;
    Point3 A;
    Point3 B;
    Point3 C;
}Triangle;

typedef struct face
{
    Triangle* T;
    float yMin;
    float yMax;
    Point3 vect_AB; //vecteur AB
    Point3 vect_BC; //vecteur BC
    Point3 vect_AC; //vecteur AC

    Point3 min_AB;
    Point3 max_AB;

    Point3 min_BC;
    Point3 max_BC;

    Point3 min_AC;
    Point3 max_AC;
}Face;

typedef struct segment
{
    Point2 A;
    Point2 B;
}Segment;

typedef struct cellulepoint
{
    Point2 P;
    Segment* reference;
    struct cellulepoint* suivant;
}Cellulepoint;

typedef Cellulepoint* Ptr_Cellulepoint;
typedef Cellulepoint* ListeChaineePoint;

void fonctionRealloc(ListeChaineePoint* tabBranche, int taille)
{
  *tabBranche = realloc(*tabBranche, taille*tailleMinTabBranche*sizeof(Cellulepoint));
  printf("fonctionRealloc : L'adresse de tabBranche est de %p\n",*tabBranche);
}
int main()
{
  ListeChaineePoint* tableauBranche = NULL;

  tableauBranche = malloc(tailleMinTabBranche*sizeof(Cellulepoint));
  if(tableauBranche != NULL)
  {
    printf("MAIN: L'adresse de tableauBranche est de %p\n",tableauBranche);
    fonctionRealloc(&tableauBranche,1);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);
    fonctionRealloc(&tableauBranche,5);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);
    fonctionRealloc(&tableauBranche,8);
    printf("MAIN: L'adresse de tableauBranche apres fonctionRealloc() est de %p\n",tableauBranche);

    free(tableauBranche);
  }
  else
  {
    printf("Il y a un soucis lors de l'allocation dynamique\n");
  }
  return 0;
}

Merci

  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 11:00:49

C'est juste le prototype de ta fonction qui n'est pas correcte :

void fonctionRealloc(ListeChaineePoint **tabBranche, int taille);

Il faut un pointeur de pointeur.

  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 11:19:56

Merci, j'ai une fonction qui utiliser ça : &tabBranche[(*index)] quand le prototype était égal à tabBranche[] mais maintenant que le prototype est **tabBranche que vaut le paramètre ? 

Est-ce : &(*tabBranche[(*index)]) ? Merci

  • Partager sur Facebook
  • Partager sur Twitter
20 août 2019 à 11:56:41

Je n'ai pas compris ta question !? mais un prototype de fonction à toujours un type de retour et un type pour chacun de ses paramètres, donc tabBranche ne peut pas être un prototype !

Pour info les tableaux sont toujours passé par adresse aux fonctions. donc fct(type *tab) est strictement équivalent à fct(type tab[]) 

Si tu as un cours sur ce sujet, il serait bien de le relire.

  • Partager sur Facebook
  • Partager sur Twitter