2013-03-14 2 views
2

사실 이것은 Xcode로 컴파일 할 때 순수한 C 프로그램입니다. 오류 메시지가 컴파일 엑스 코드는 신호 SIGABRT와 라인 temp=(int*)realloc(p, lengA*sizeof(int))에서 중단 점을 제공realloc 된 포인터가 할당되지 않았습니다.

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
int LocateElem(int *p1,int e,int leng1); 
void Display(int max, int array[]); 
int GetElem(int * p, int pass); 
int Union(int *p1,int *p2, int leng1, int leng2); 
int ListInsert(int *p, int e, int lengA); 
int* GetData(int* pArray, int Array_size); 
void Show(int *p, int leng); 

void InitList_Sq(int *L); 
int *p_A,*p_B; 
int m,n; 

int main() 
{ 
    clock_t begin, end; 
    double cost; 
    begin = clock(); 


    printf("How many elements of A u want:"); 
    scanf("%d",&m); 
    if (m<0) { 
     printf("Error!"); 
     return 0; 
    } 
    printf("How many elements of B u want:"); 
    scanf("%d",&n); 
    if (n<0) { 
     printf("Error!"); 
     return 0; 
    } 

    p_A=(int *)malloc(m*sizeof(int)); 
    p_B=(int *)malloc(n*sizeof(int)); 
    if (p_A==NULL) { 
     printf("Error allocating memory!\n"); //print an error message 
     return 0; //return with failure 
    } 
    if (p_B==NULL) { 
     printf("Error allocating memory!\n"); //print an error message 
     return 0; //return with failure 
    } 

    int *pLast_A, * pLast_B; 
    printf("Array A is :\n"); 
    pLast_A=GetData(p_A, m); 
    printf("\nArray B is :\n"); 
    pLast_B=GetData(p_B, n); 

    int newLeng; 
    newLeng=Union(p_A,p_B,m,n); 

    printf("\nThe Union set is :\n"); 
    Show(p_A, newLeng); 

    free(p_A); 
    free(p_B); 
    end = clock(); 
    cost = (double)(end - begin)/CLOCKS_PER_SEC; 
    printf("\n%lf seconds", cost); 
    return 1; 


} 


int* GetData(int* pArray, int Array_size){ 
    int* pFill= pArray; 
    int count; 
    srand((unsigned) time(NULL)); 
    for (count=0; count< Array_size; count++) { 
     *(pFill+count)=rand()%1000; 
     printf("%d\t", * (pFill+count)); 
    } 
    return pFill+count; 
} 


int Union(int *p1,int *p2, int leng1, int leng2){ 
    for (int count=0; count<leng2; count++) { 
     int e=GetElem(p2, count); 
     while(LocateElem(p1, e, leng1)==0){ 
      leng1=ListInsert(p1, e, leng1); 
     } 
    } 
    return leng1; 
} 

int GetElem(int *p, int pass){ 
    return *(p+pass); 
} 

int LocateElem(int *p1,int e,int leng1){ 
    for (int count=0; count<leng1; count++) 
     if (e==*(p1+count)) 
      return 1; 
     else 
      return 0; 

} 

int ListInsert(int *p, int e, int lengA){ 
    lengA+=1; 
    int* temp; 
    temp=(int*)realloc(p, lengA*sizeof(int)); 
    if (temp==NULL) { 
     printf("Error allocating memory!\n"); //print an error message 
     free(temp); 
     return 0; //return with failure 
    } 
    else{ 
     p=temp; 
     *(p+lengA-1)=e; 

    } 
    return lengA; 

} 

void Show(int *p, int leng){ 
    for (int count=0; count<leng; count++) { 
     printf("%d\t", *(p+leng)); 
    } 
} 

후 "포인터 인 realloc'd이 할당되지 않았다"말했다.

int ListInsert(int *p, int e, int lengA){ 
    int* temp; 
    temp=(int*)realloc(p, lengA*sizeof(int)); 
    ... 
    else { 
     p=temp; // <<<<< THIS 

p의 새로운 값이 다시 ListInsert의 호출자에게 전파되지 않습니다

+1

좁힐 수 있습니까? –

+1

C에서'malloc()'또는'realloc()'의 반환 값 (http://stackoverflow.com/a/605858/28169)을 던져서는 안됩니다. – unwind

답변

7

문제는 여기에 있다는 것입니다. 이는 p이 값으로 전달 되었기 때문에 발생합니다.

int *pint **p으로 설정해야합니다.

+0

+1 나를 입력하는 것이 더 빠를 수도 있습니다. :). 좋은 대답. – phoxis

+0

바로 남자입니다. U는 맞습니다. 고마워요! –

+0

하지만 여전히 같은 문제가 발생합니다. –

관련 문제