2013-03-14 86 views
2

實際上,這是一個純粹的C prog.when我編譯Xcode。錯誤 消息說,「指針是realloc'd沒有被分配」正在重新分配的指針未被分配

#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)); 
    } 
} 

編譯後給出的Xcode在與信號SIGABRT行temp=(int*)realloc(p, lengA*sizeof(int))斷點。

+1

你可以縮小它嗎? – 2013-03-14 12:35:59

+1

你不應該在C中使用'malloc()'或'realloc()'](http://stackoverflow.com/a/605858/28169)的返回值。 – unwind 2013-03-14 12:44:16

回答

7

的問題是,在這裏:

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

p新值不會傳播回ListInsert的調用者。發生這種情況是因爲p按值傳遞。您需要將int *p轉成int **p

+0

+1可能你輸入的比我快:)。很好的答案。 – phoxis 2013-03-14 12:39:43

+0

正是男人。你是對的。!非常感謝! – 2013-03-14 12:40:24

+0

但是兄弟仍然會發生同樣的問題。 – 2013-03-14 12:49:37