2014-02-15 31 views
1

我在我的程序中試圖做的是將一個字符串的內容以相反的方式複製到另一個字符串中。該計劃的這部分工作。重新分配內存不能在c中工作

但是,我不想限制用戶輸入,所以我想使用malloc和realloc。這是我的代碼:

#include <stdio.h> 
#include <stdlib.h> 
/*copy one string to another, in reverse*/ 
void copyStr(char *p, char *h){ 

    int i=0,j=0; 
    int length=0; 
    length=strlen(p); int l=length; 
    for (i=0; i<length; i++){ 
     h[i]=p[l-1]; 
     l--; 
    } 
    char *temp=&h[0]; 
    for (i=0; i<length; i++){ 
     printf("%c",temp[i]); 
    } 


} 
main(){ 
    printf("please enter a string\n"); 
    char c; int i=0; int end=10; 
    /*allocate initial memory*/ 
    char *p=(char*)malloc(sizeof(end)); char *temp=p; 
    while (c!='\n') 
    { 
     /*reallocate if needed*/ 
     if (i==(end-1)){ 
      end*=2; 
      temp = realloc(p,end*sizeof(temp)); 
      if (temp!=NULL){ 
       /*this is for myself, to see what the error was*/ 
       printf("error allocating\n"); 
       exit(1); 
      } 
      else 
       free(p); 
     } 
     c=getchar(); 
     p[i]=c; 
     i++; 
    } 

    char h [sizeof(p)]; 
    copyStr(p,h); 
} 

我發現realloc函數不起作用,所以我要求您的幫助。

如果輸入非常短(即3個字符),則該程序起作用。 如果超過10個字母,它不會重新分配內存。 如果它長於5,它將反向打印,但會發送一條消息,稱爲「堆棧被搗毀」。 謝謝。

+0

也許'溫度=(字符*)的realloc(P,端的sizeof *(溫度));'將工作...因爲'*溫度= (char *)realloc(p,end * sizeof(temp));'看起來很奇怪。 – francis

+0

'temp = realloc(p,end * sizeof * temp);' – wildplasser

+0

兩者都試過,仍然不起作用... – Alan

回答

1

事實上,有一些小動作改變:

  • *temp=realloc(...應該成爲temp=realloc(...
  • 事實上,temp!=NULLrealloc()正常行爲。
  • 如果您在使用後重新分配操作,請不要忘記更改p
  • sizeof(p)是一個指針的大小,也就是4個或8 ...我把它變成char h [sizeof(char)*(i+1)];
  • 我還添加了\0字符在字符串的結尾。如果您希望打印或使用strlen()#include string.h,這很有用。然後,你可以printf("the result is :\n%s \n",h);

這裏去代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
/*copy one string to another, in reverse*/ 
void copyStr(char *p, char *h){ 

    int i=0,j=0; 
    int length=0; 
    length=strlen(p); int l=length; 
    for (i=0; i<length; i++){ 
     h[i]=p[l-1]; 
     l--; 
    } 
    //keep end-of-string character 
    h[length+1]='\0'; 
    /* char *temp=&h[0]; 
    for (i=0; i<length; i++){ 
     printf("%c",temp[i]); 
    }*/ 
    printf("the result is :\n%s \n",h); 



} 
main(){ 
    printf("please enter a string\n"); 
    char c; int i=0; int end=10; 
    /*allocate initial memory*/ 
    char *p=(char*)malloc(sizeof(end)); char *temp=p; 
    //signaling end of string 
    p[0]='\0'; 
    while (c!='\n' && c!=EOF) 
    { 
     /*reallocate if needed*/ 
     if (i==(end-2)){ 
      end*=2; 
      temp=(char*)realloc(p,end*sizeof(char)); 
      if (temp==NULL){ 
       /*this is for myself, to see what the error was*/ 
       printf("error allocating\n"); 
       exit(1); 
      } 
      else{ 
       p=temp; 
       printf("ok here\n"); 
      } 
     } 
     c=getchar(); 
     p[i]=c; 
     i++; 
    } 
    //signaling end of string 
    p[i+1]='\0'; 

    printf("INVERTING STRING\n"); 
    char h [sizeof(char)*(i+1)]; 
    copyStr(p,h); 

      free(p); 
} 

!危宿三KROW OT smees TI

再見,

弗朗西斯

+0

有兩個事實:1.您的代碼工作。 2.我必須瞭解它是如何工作的。謝謝! – Alan

+1

我在最後添加了'free(p)'...以避免未來的內存泄漏...... Bye,Francis – francis

1

它不是*temptemp。 Realloc返回內存分配的位置的地址,您應該將其存儲在指針中。在由指針指向的地址不存儲已經這將是毫無意義

0
*temp=(char*)realloc(p,end*sizeof(temp)); 

temp = realloc(p,end*sizeof(temp)); 

指針是temp*temp指的是內容。

+0

謝謝,修正了它的問題,但仍然無效。 – Alan

+0

'temp = realloc(p,end * sizeof * temp);' – wildplasser