-4

je dois mettre en œuvre ces fonctions dans internal.h ci-dessous:C allocation de mémoire dans la structure étudiante ne fonctionne pas

#ifndef STUDENT_INTERNAL_H 
#define STUDENT_INTERNAL_H 

#include "student.h" 

/** Allocate memory for a new @ref student object. */ 
struct student *student_alloc(void); 

/*** Initialize an already-allocated @ref student object. 
* @returns NULL on failure but does not free the memory 
*/ 

struct student *student_init(struct student *, const char *name, size_t namelen, 
          struct student_id, student_complete); 

#endif /* STUDENT_INTERNAL_H */ 

C'est ce que j'ai à ce jour mais je suis confus et il ne fonctionne pas:

#include <sys/types.h> 
#include "common.h" 
#include "student.h" 
#include "transcript.h" 
#include "internal.h" 

/** 
* Initialize an already-allocated @ref student object. 
* @returns NULL on failure but does not free the memory 
*/ 
struct student *student_init(struct student *student, const char *name, size_t namelen, 
          struct student_id stud_id, student_complete stud_complete) { 

    struct student *initialized_student; 

    name = malloc(sizeof(namelen)); 
    initialized_student = malloc(sizeof (student)); 

    if (student_alloc()) { 
     initialized_student->s_name = name; 
     initialized_student->s_id = stud_id; 
     initialized_student->s_complete = stud_complete; 

     return initialized_student; 
    } else { 
     return 0; 
    } 
} 

stud_complete est ici un pointeur de fonction qui est déclarée dans un fichier d'en-tête déférent de student.h comme typedef int (*student_complete)(struct student *);

+1

Peut-être que ce serait une bonne idée d'apprendre à utiliser le débogueur –

+1

'initialized_student = malloc (sizeof (étudiant))' est faux - >> ' initialized_student = malloc (sizeof (struct étudiant)); 'ou encore mieux:' initialized_student = malloc (sizeof * initialized_sudent); 'et name = malloc (sizeof (namelen));' est également faux. – wildplasser

Répondre

1

L'API est source de confusion:

Qu'est-ce que namelen?

  • Est-ce la longueur du préfixe initial de name qui est le nom de l'étudiant? Dans ce cas, vous devez allouer un octet supplémentaire pour le terminateur null et copier le nom de manière appropriée.
  • est-ce la longueur maximale pour le nom de l'étudiant? Encore une fois si c'est la longueur, pas la taille, vous avez besoin d'un octet supplémentaire pour le terminateur null. Dans tous les cas, sizeof(namelen) est inapproprié.

Que fait student_alloc()?

De même, la taille passée à malloc() d'affecter la structure student doit être celle d'struct student ou de façon plus fiable que du type pointé par le pointeur de destination.

Voici une version corrigée:

struct student *student_init(struct student *student, const char *name, size_t namelen, 
          struct student_id stud_id, student_complete stud_complete) { 

    struct student *initialized_student = malloc(sizeof(*initialized_student)); 
    char *s_name = calloc(namelen + 1); 

    if (initialized_student && s_name && student_alloc()) { 
     strncat(s_name, name, namelen); 
     initialized_student->s_name = s_name; 
     initialized_student->s_id = stud_id; 
     initialized_student->s_complete = stud_complete; 
     return initialized_student; 
    } else { 
     free(initialized_student); 
     free(s_name); 
     return 0; 
    } 
}