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

时间:2012-11-22 16:33:37

标签: c unix

我有简单的应用程序,我有服务器,我先运行然后尝试执行我的c程序,我得到这个错误分段故障核心转储。我认为问题可能来自我的分配记忆,但不知道是否从那里如何重新编写它:贝娄是我的代码:

    /* 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;
 }}}

1 个答案:

答案 0 :(得分:2)

您尚未发布完整的计划,因此无法说明您的问题所在。代码中可能导致seg错误的一个错误是alloc_blank_customer为各种char数组分配内存但不初始化它。如果您以后在这些未初始化的数组上使用字符串处理函数,则效果将不会定义,并且可能发生崩溃。

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

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

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

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