การเติมตัวชี้ถ่านในโครงสร้าง

ฉันได้กำหนดโครงสร้าง "รถยนต์" ด้วยโมเดล (char *model) และปีของโมเดล (int year) ฉันมีฟังก์ชั่นที่จะสร้างโครงสร้างรถใหม่ อย่างไรก็ตาม มันเป็นข้อผิดพลาด seg เมื่อคัดลอกตัวชี้ถ่าน นี่ควรจะสร้างโหนดใหม่สำหรับรายการที่เชื่อมโยง

Car *newCar(char *model, int year){
    Car *new = malloc(sizeof(Car));
    new->year = year;
    new->model = malloc(MAX_LENGTH*sizeof(char));
    strcpy(new->model, model);
    new->next = NULL;
    return new;
}

person kyle    schedule 11.03.2013    source แหล่งที่มา
comment
แล้วnew->model = malloc(strlen(model) + 1)ล่ะ?   -  person cnicutar    schedule 11.03.2013
comment
คุณควรตรวจสอบว่า char *model ไม่ใช่ NULL นอกจากนี้ ตามแนวทางปฏิบัติที่ดี ควรตรวจสอบการส่งคืนของ mallocs เสมอ   -  person congusbongus    schedule 11.03.2013
comment
@cnicutar ขอบคุณ; อย่างไรก็ตามปัญหายังคงมีอยู่   -  person kyle    schedule 11.03.2013
comment
@Cong Xu char *model จะไม่เป็น NULL เพราะกำลังสแกนข้อมูลจากไฟล์ในฟังก์ชันอื่น   -  person kyle    schedule 11.03.2013
comment
ก่อนอื่นอย่าใช้ new เป็นชื่อตัวแปรเนื่องจากเป็นคำหลัก   -  person Kinjal Patel    schedule 11.03.2013
comment
@KinjalPatel เขาสามารถใช้ new กับคอมไพเลอร์ c ได้อย่างปลอดภัย   -  person Barath Ravikumar    schedule 11.03.2013
comment
คุณแน่ใจหรือไม่ว่า char *model ถูกยกเลิก NULL?   -  person Kinjal Patel    schedule 11.03.2013
comment
คุณได้ลองสิ่งที่ cnicutar เสนอแล้วหรือยัง.....นั่นคือ new-›model = malloc(strlen(model) + 1)   -  person Kinjal Patel    schedule 11.03.2013


คำตอบ (4)


สำหรับการอ้างอิงในอนาคต ฟังก์ชั่นนี้แก้ไขปัญหาของฉัน...

Car *createCar(char *model, int year){
    Car *new = malloc(sizeof(Car));
    new->year = year;
    new->model = malloc(strlen(model)+1);
    strcpy(new->model, model);
    new->next = NULL;
    return new;
}
person kyle    schedule 02.11.2015
comment
คุณ malloc ใส่พื้นที่ผิดจำนวน มันควรจะเป็น strlen(model)+1 หากสิ่งนี้ดูเหมือนจะแก้ไขปัญหาของคุณได้ แสดงว่าคุณเดินอยู่บนเปลือกไข่แล้ว! - person M.M; 03.11.2015
comment
@MM คุณพูดถูก! ฉันกำลังทบทวนการบ้านเก่าๆ สมัยเรียนปีแรก และพบว่าฉันไม่เคยโพสต์วิธีแก้ปัญหาเลย ฉันได้อัปเดตคำตอบเพื่อสะท้อนถึงข้อผิดพลาดที่คุณพบ - person kyle; 03.11.2015

คุณอาจลองสิ่งนี้:

new->model = model == NULL ? NULL : strdup(model);

สิ่งนี้จะป้องกันคุณจากข้อผิดพลาดหากโมเดลเป็น NULL มิฉะนั้นคุณจะจัดสรรพื้นที่จำนวนที่แน่นอนและ strcopy มัน นอกจากนี้ยังช่วยให้คุณสามารถ free(new->model) ในตอนท้ายได้ในทุกกรณี

person Edouard Thiel    schedule 11.03.2013

ที่นี่โมเดลของคุณคือตัวชี้อักขระ

แต่ strcpy ต้องการสองอาร์กิวเมนต์ - ซึ่งควรเป็น array หรือ character pointer to which memory allocated by malloc or calloc

แต่ strcpy(); ของคุณรับหนึ่งอาร์กิวเมนต์เป็นตัวชี้อักขระซึ่งจะไม่ได้รับการยอมรับ

ทำเลย

new->model = malloc(strlen(model) + 1) จากนั้นเขียน strcpy () ของคุณ มันจะใช้ได้

person Hitesh Menghani    schedule 11.03.2013
comment
หรือ new->model = strdup (model); ซึ่งทำเช่นเดียวกันในคำสั่งเดียว - person Edouard Thiel; 11.03.2013
comment
@EdouardThiel ยกเว้น strdup ไม่ใช่มาตรฐาน (แม้ว่าจะสามารถนำไปใช้งานได้อย่างง่ายดาย) - person cnicutar; 11.03.2013
comment
strdup() สอดคล้องกับ SVr4, 4.3BSD, POSIX.1-2001 - person Edouard Thiel; 16.03.2013

ดูโค้ดด้านล่างแล้วเปรียบเทียบกับโปรแกรมของคุณ แน่ใจว่าคุณจะพบว่ามีอะไรผิดปกติกับโปรแกรมของคุณ

#include <stdio.h>
#include <string.h>

typedef struct car{
char *model;
int year;
}Car;

Car * newCar(char *, int );

int main()
{

Car *benz = newCar("S-class",1990);

printf("\nModel = %s\n",benz->model);
printf("\nYear = %d\n",benz->year);

}

Car * newCar(char *model, int year)
{
    Car *new = malloc(sizeof(Car));
    new->year = year;
    new->model = malloc(strlen(model));
    strcpy(new->model, model);
    return new;
}
person Barath Ravikumar    schedule 11.03.2013