【问题标题】:reallocating memory doesn't work in c重新分配内存在 c 中不起作用
【发布时间】:2014-03-14 22:47:38
【问题描述】:

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

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

【问题讨论】:

  • 也许temp=(char*)realloc(p,end*sizeof(temp)); 会起作用...因为*temp=(char*)realloc(p,end*sizeof(temp)); 看起来很奇怪。
  • temp = realloc(p, end * sizeof *temp);
  • 两个都试过了,还是不行……

标签: c string stack malloc realloc


【解决方案1】:

其实还是有一些小技巧可以改变的:

  • *temp=realloc(... 应该变成temp=realloc(...
  • temp!=NULLrealloc() 的正常行为。
  • 如果你在realloc操作之后使用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

再见,

弗朗西斯

【讨论】:

  • 有两个事实:1. 您的代码有效。 2. 我必须学习它是如何工作的。谢谢!
  • 我在最后添加了free(p)...以避免将来的内存泄漏...再见,弗朗西斯
【解决方案2】:
*temp=(char*)realloc(p,end*sizeof(temp));

成为

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

指针是temp*temp指的是内容。

【讨论】:

  • temp = realloc(p, end * sizeof *temp);
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多