我在C中有一个问题,发生了大约3次,我找不到答案。我正在 create 函数中分配内存,当我试图在 destrot destrot 中释放它时,程序崩溃了。
这是程序:
typedef struct Order_t* Order;
struct Order_t {
char* email;
TechnionFaculty faculty;
int id;
char* time;
int num_ppl;
};
Order createOrder(char* email, TechnionFaculty faculty, int id, char* time,
int num_ppl)
{
if (email == NULL) {
return NULL;
}
if (checkEmail(email) != OK)
return NULL;
if (checkFaculty(faculty) != OK)
return NULL;
Order order = malloc(sizeof(Order));
if (order == NULL)
return NULL;
(order->email) = malloc(strlen(email) + 1);
if (order->email == NULL) {
free(order);
return NULL;
}
strcpy(order->email, email);
order->faculty = faculty;
order->id = id;
order->num_ppl = num_ppl;
order->time = malloc(strlen(time) + 1);
if (order->time == NULL) {
free(order->email);
free(order);
return NULL;
}
return order;
}
void orderDestroy(Order order)
{
assert(order != NULL);
free(order->email);
free(order->time);
free(order);
}
我对您的代码做了一些小更改,以免崩溃。使用结构时,您应同时分配结构及其成员。我希望它能为您提供帮助。
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Order_t *Order;
struct Order_t{
char* email;
//TechnionFaculty faculty;
int id;
char* time;
int num_ppl;
};
Order createOrder (char* email, int id, char* time, int num_ppl){
if (email == NULL) {
return NULL;
}
Order order = malloc(sizeof(struct Order_t));
if (order == NULL)
return NULL;
(order->email) = malloc(strlen(email)+1);
if (order->email == NULL){
free(order);
return NULL;
}
strcpy(order->email, email);
order->id = id;
order->num_ppl = num_ppl;
(order->time) = malloc(strlen(time)+1);
if (order->time == NULL){
free(order->email);
free(order);
return NULL;
}
return order;
}
void orderDestroy (Order order){
assert(order != NULL);
free(order->email);
free(order->time);
free(order);
}
int main() {
printf("Hello, World!n");
Order o1 = createOrder("foo", 42, "24", 420);
orderDestroy(o1);
return 0;
}
我们使用Valgrind进行检查。
$ ./a.out
Hello, World!
developer@1604:~/CLionProjects/untitled4$ valgrind ./a.out
==16288== Memcheck, a memory error detector
==16288== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16288== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==16288== Command: ./a.out
==16288==
Hello, World!
==16288==
==16288== HEAP SUMMARY:
==16288== in use at exit: 0 bytes in 0 blocks
==16288== total heap usage: 4 allocs, 4 frees, 1,063 bytes allocated
==16288==
==16288== All heap blocks were freed -- no leaks are possible
==16288==
==16288== For counts of detected and suppressed errors, rerun with: -v
==16288== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)