gpt4 book ai didi

c - C 编程中的段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 18:59:09 25 4
gpt4 key购买 nike

我有一个简单的c应用程序,其中有我首先运行的服务器,然后尝试执行我的c程序,并且我得到了此错误段错误核心转储。我认为问题可能来 self 的分配内存,但不知道它是否来自那里如何修复它:波纹管是我的代码:

    /* This is student.c file which as a part of MAD assignment 1 is referenced and used in main.c */

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

#include <string.h>

#include "config.h" // auto generated
#include "customer.h"


Customer *make_customer(unsigned id)
{

Customer *cust;

if ((cust = (Customer *)malloc(sizeof(Customer))) == NULL) {
fprintf(stderr, "Failed to allocate Customer structure!\n");
exit(EXIT_FAILURE);
}
cust->active = 0;
cust->id = id;
cust->old = NULL;
cust->address = NULL;
cust->name = NULL;

return cust;
}

void free_customer(Customer *cust)
{
free(cust->old);
free(cust->address);
free(cust->name);
free(cust);
}

void make_customer_active(Customer *cust)
{
cust->active = 1;
}


void set_customer_old(Customer *cust, char *old)
{
cust->old = strdup(old);
}

void set_customer_address(Customer *cust, char *address)
{
cust->address = strdup(address);
}

void set_customer_name(Customer *cust, char *name)
{
cust->name = strdup(name);
}

int is_customer_active(Customer *cust)
{
return cust->active;
}

int serialize_customer(char *buffer, Customer *cust)
{
size_t offset = 0;

memcpy(buffer, &cust->id, sizeof(cust->id));
offset = sizeof(cust->id);
memcpy(buffer+offset, &cust->active, sizeof(cust->active));
offset = offset + sizeof(cust->active);
memcpy(buffer+offset, cust->old, strlen(cust->old)+1);
offset = offset + strlen(cust->old)+1;
memcpy(buffer+offset, cust->address, strlen(cust->address)+1);
offset = offset + strlen(cust->address)+1;
memcpy(buffer+offset, cust->name, strlen(cust->name)+1);
offset = offset + strlen(cust->name)+1;


return offset;
}

int deserialize_customer(char *buffer, Customer *cust)
{
size_t offset = 0;

memcpy(&cust->id, buffer, sizeof(cust->id));
offset = sizeof(cust->id);
memcpy(&cust->active, buffer+offset, sizeof(cust->active));
offset = offset + sizeof(cust->active);
memcpy(cust->name, buffer+offset, strlen(buffer+offset)+1);
offset = offset + strlen(buffer+offset)+1;


return offset;
}

void print_customer(Customer *cust)
{
printf("Customer id:%d\n", cust->id);
printf("Cutomer age:%s\n", cust->old);
printf("Cutomer name:%s\n", cust->name);
printf("Cutomer address:%s\n", cust->address);
}

Customer *alloc_blank_customer()
{

Customer *cust;

if ((cust = (Customer *)malloc(sizeof(Customer))) == NULL) {
fprintf(stderr, "Failed to allocate Customer structure!\n");
exit(EXIT_FAILURE);
}
cust->active = 0;
cust->id = 0;
if ((cust->name = malloc(MAX_NAME)) == NULL) {
fprintf(stderr, "Failed to allocate name!\n");
exit(EXIT_FAILURE);
if ((cust->old = malloc(MAX_OLD)) == NULL) {
fprintf(stderr, "Failed to allocate age!\n");
exit(EXIT_FAILURE);
if ((cust->address = malloc(MAX_ADDRESS)) == NULL) {
fprintf(stderr, "Failed to allocate address!\n");
exit(EXIT_FAILURE);
}

return cust;
}}}

最佳答案

您还没有发布完整的程序,因此无法说出您的问题所在。代码中可能导致段错误的一个错误是 alloc_blank_customer 为各种 char 数组分配内存但不对其进行初始化。如果您稍后在这些未初始化的数组上使用字符串处理函数,则效果将是未定义的,并可能导致崩溃。

您可以通过使用 calloc 分配 char 数组来解决此问题:

cust->name = calloc(1, MAX_NAME);

或者在数组的开头添加一个 nul 终止符:

cust->name = malloc(MAX_NAME);
cust->name[0] = '\0';

关于c - C 编程中的段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13516878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com