2017-12-30 36 views
-1

는 보내기 struct예외 동안

struct HotelManagement 
{ 
    Hotel_t *hotel; 
    Customer_t *customers; 
    reservation_t *reservations; 
    int physicalSize; 
    int registerdSize; 

    int physicalSizeReserv; 
    int registerdSizeReserv; 

} typedef HotelManagement_t; 

그리고 다음 main 다음

printf("-----DETAILS FOR CUSTOMER-----\n"); 

printf("enter name "); 
gets(tempName);//"cleans" the buffer 
gets(tempName); 
customerName = strdup(tempName); 

printf("nenter credit card "); 
gets(tempCredit); 
creditCardNumber = strdup(tempCredit); 

printf("enter credit card expiration month(mm)\n"); 
printf("enter credit card expiration year(yyyy)\n"); 

scanf("%d",&month); 
scanf("%d",&year); 

addCustomer(customerName,creditCardNumber, month,year,&hm); 

addCustomer 다음의 구현 :

void addCustomer(char *customerName, char *numberOfCreditCard,int month,int year,HotelManagement_t *hotelMang) 
{ 
    int *m = &month; 
    int *y = &year; 
    int i; 

    if (hotelMang->physicalSize == hotelMang->registerdSize) 
    { 
     hotelMang->customers = (Customer_t*)realloc(hotelMang->customers, hotelMang->registerdSize * sizeof(Customer_t)); 
    } 
    hotelMang->customers[hotelMang->registerdSize].id = hotelMang->registerdSize+1;//id starts with '1' 
    hotelMang->customers[hotelMang->registerdSize].cName = (char*)malloc(strlen(customerName)*sizeof(char));//initalize space for the Customers name 
    strcpy(hotelMang->customers[hotelMang->registerdSize].cName ,customerName); 

    if (checkValidCreditCard(&month,&year,numberOfCreditCard) == 1) 
    { 
     hotelMang->customers[hotelMang->registerdSize].credit = (char*)malloc(strlen(numberOfCreditCard)*sizeof(char));//initalize size 
     strcpy(hotelMang->customers[hotelMang->registerdSize].credit, numberOfCreditCard); 
     hotelMang->customers[hotelMang->registerdSize].month = month; 
     hotelMang->customers[hotelMang->registerdSize].year = year; 
    } 
    //increments 
    for (i = 0; i <= hotelMang->registerdSize; i++) 
    { 
     printOneCustomer(hotelMang->customers[i]); 
    } 
    hotelMang->registerdSize= hotelMang->registerdSize +1; 
} 

프로그래머를 배열을 초기화하는 함수 s ...

hotelManagement->customers = (Customer_t*)calloc(1, sizeof(Customer_t)); 
    hotelManagement->hotel->roomsMat = (Room_t**)calloc(floors, sizeof(Room_t*)); 
    hotelManagement->reservations = (reservation_t*)calloc(1, sizeof(reservation_t)); 

점점 고객을 배열에 추가하기 때문에 고객 공간을 다시 할당하려고하지만 예외가 발생합니다. 그 이유가 무엇일까요? 당신이 아주 처음 addCustomer를 호출 할 때

+1

내 *** 추측 ***? 'HotelManagement_t' 구조체'hm'을 초기화하지 않습니다. –

+1

또한, 결코 * 사용 *하여'gets'! 최신 C 표준에서 제거 된 위험한 기능입니다. 예 : ['fgets'] (http://en.cppreference.com/w/c/io/fgets). –

+1

전체 메인입니까? 예인 경우 누락 된 세 줄을 추가하십시오. 그렇지 않으면 우리는 당신이 뭔가를 놓쳤는 지 궁금해합니다. – klutt

답변

0

hotelManagement->registerdSize의 값은 제로입니다. 크기 0을 가진 realloc을 호출하면 구현이으로 정의됩니다. realloc 함수 은 원래 포인터를 반환 할 수 있습니다. 그것은 수 있습니다null 포인터를 반환합니다. 그것도 무료 메모리 수 있습니다.

당신이 Customer_t 구조의 "배열"을 가리 키도록 hotelMang->customers를 초기화 점을 감안하면, 당신은 아마 1hotelManagement->registerdSize를 초기화해야한다. 그리고 재 할당 할 때 (hotelManagement->registerdSize + 1)을 사용하십시오.

또한 오류를 확인하지 않으므로 callocrealloc은 모두 null 포인터를 반환 할 수 있습니다. realloc으로 전화를 걸면 원래 포인터가 없어져 메모리 누수가 발생합니다. realloc의 결과를 얻으려면 항상 임시 변수를 사용하십시오.

+0

그래서 당신이 방금 제안한 것처럼 몇 가지 변경을했습니다. 첫째, 나는 retilnd NULL 포인터를 확인하는 statment 경우 registerdSize을 initilazed 1. 추가. 그리고 마지막으로 hm을 초기화하는 것이 었습니다.고객이 NULL이됩니다. 감사! – OmerMichleviz