学生结构中的 C 内存分配不起作用
C memory allocation in student structure not working
我必须在下面的 internal.h 中实现这些功能:
#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 */
这是我目前所拥有的,但我很困惑并且它不起作用:
#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
这是一个在不同的头文件中声明的函数指针 student.h 就像 typedef int (*student_complete)(struct student *);
API令人困惑:
什么是 namelen
?
- 是不是
name
首字母的长度就是学生的名字?在这种情况下,您应该为空终止符分配一个额外的字节并适当地复制名称。
- 这是学生姓名的最大长度吗?同样,如果这是长度而不是大小,则您需要一个额外的字节作为空终止符。
- 无论如何
sizeof(namelen)
都是不合适的。
student_alloc()
是做什么的?
类似地,传递给malloc()
以分配student
结构的大小应该是struct student
的大小,或者更可靠地是目标指针指向的类型的大小。
这是更正后的版本:
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;
}
}
我必须在下面的 internal.h 中实现这些功能:
#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 */
这是我目前所拥有的,但我很困惑并且它不起作用:
#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
这是一个在不同的头文件中声明的函数指针 student.h 就像 typedef int (*student_complete)(struct student *);
API令人困惑:
什么是 namelen
?
- 是不是
name
首字母的长度就是学生的名字?在这种情况下,您应该为空终止符分配一个额外的字节并适当地复制名称。 - 这是学生姓名的最大长度吗?同样,如果这是长度而不是大小,则您需要一个额外的字节作为空终止符。
- 无论如何
sizeof(namelen)
都是不合适的。
student_alloc()
是做什么的?
类似地,传递给malloc()
以分配student
结构的大小应该是struct student
的大小,或者更可靠地是目标指针指向的类型的大小。
这是更正后的版本:
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;
}
}