重新分配内存在c中不起作用

时间:2014-02-15 14:08:39

标签: c string stack malloc realloc

我在程序中尝试做的是将一个字符串的内容复制到另一个字符串中。这部分程序有效。

但是,我不想限制用户输入,所以我想使用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,它将反向打印,但会向我发送一个名为“stack smashed”的消息。 谢谢。

3 个答案:

答案 0 :(得分:1)

它不是*temp temp。 Realloc返回分配内存的位置的地址,您应该将其存储在指针中。不存储在已经没有任何意义的指针指向的地址

答案 1 :(得分: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);
}

! enif krow ot smees ti

再见,

弗朗西斯

答案 2 :(得分:0)

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

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

指针为temp*temp指内容。

相关问题