2013-12-18 104 views
0

我有一个任务,我必须接受来自用户的输入。我不能使用链表,所以我的计划是:int数组的动态内存分配

  1. Alloc一些内存。

  2. 如果我们需要的realloc,这意味着我达到了分配的小区数量:

    1. 尝试realloc的。如果成功,那很好。

    2. 如果我们不能重新分配,打印输入,空闲内存和realloc然后。

我真的不能决定有关,告诉我我怎么达到的分配内存的结束,这就是为什么我需要你的帮助的命令。我写道:

if (i==(MAX_CHARS_INPUT-1)) 

但我不确定。

代码:

#include <stdio.h> 
#include <stdlib.h> 

#define MAX_CHARS_INPUT 200 
#define D_SIZE 2 

void printWithMalloc(){ 
    int charSize=1; 
    int *ptr=malloc(MAX_CHARS_INPUT*sizeof(charSize)); 

    int i=0, j=0, c; 
    printf("please enter a string\n"); 

    while ((c=getchar())!=EOF && c!='\n') 
    { 
     ptr[i++]=c; 
     if (i==(MAX_CHARS_INPUT-1)) /*if we need to realloc*/ 
     { 
      int *temp=realloc(ptr,D_SIZE*MAX_CHARS_INPUT*sizeof(charSize)); 
      if (temp==NULL) /*realloc failed*/ 
      { 
       printf("you wrote:\n"); 
       while(j<=i) 
        putchar(ptr[j++]); 

       free(ptr); 
       ptr=(int*)malloc(MAX_CHARS_INPUT*sizeof(charSize)); 
      } 
      else 
       ptr=temp; 
     } 
    } 
} 

int main(){ 
    printWithMalloc(); 
    return 0; 
} 

谢谢!

+1

'的sizeof(charSize)'会给你一个int的大小,因为harSize是一个int。相反,只是做'的sizeof(char)的' –

+3

更妙的是,完全忽略它,因为'的sizeof(char)的'是定义1 – glglgl

+0

你可能要重新考虑做一个realloc的一个额外的2个字节,因为它不是很有效,如果你的环境不是嵌入式的(每个字节都是计数的),只需在空间不够的情况下添加一个100字节的块就可以减少realloc的数量。有一个变量保持缓冲区大小的轨道,当你重新分配时增加这个。 –

回答

1

的问题确实是与你的条件:

if (i==(MAX_CHARS_INPUT-1)) 

这工作,但仅限于第一次达到此限制。当你的realloc你的缓冲区变大,但你不检查是否用完的空间。所以想象我输入500个字符。当第199个字符被读取时,缓冲区被重新分配成400个字符大。但是,i只有检查在第199个字符,所以当到达第400个时它将用完缓冲区。

第二个问题是,当您重新分配缓冲区时,它只会增长到400个字符(D_SIZE * MAX_CHARS_INPUT)并且不会更大。

第三个问题是,当您重新编号为malloc(即当realloc失败时),您不会重置i,因此它会立即写入缓冲区末尾。

正如现在删除的答案中所建议的。跟踪您的缓冲区大小:

size_t buffSize = MAX_CHARS_INPUT; 

当你重新分配,更新buffSize第一,然后用其作为参数传递给realloc

buffSize *= D_SIZE; // double the buffer-size 
temp = realloc(ptr, buffSize * sizeof(*temp)); // using sizeof *temp is less confusing and less error-prone 
当然

:也更新您的条件:

if(i == buffSize - 1) 

和当您重新malloc重置ibuffSize

buffSize = MAX_CHARS_INPUT; 
ptr = malloc(buffSize*sizeof(*ptr)); 
i = 0; 

虽然不是很明智,因为如果分配失败,通常会有更大的问题(除非内存非常有限)。并且(特别是因为您不检查malloc的结果)可能有问题,因为malloc也可能会失败。在alloc-fail之后退出程序并不罕见。

+0

谢谢,我会在几个小时内调查一下。 – Alan

0

有几件事情在你的代码获得错误的,新的代码是:

#include <stdio.h> 
    #include <stdlib.h> 

    #define MAX_CHARS_INPUT 200 
    #define D_SIZE 2 

    void printWithMalloc(){ 
     //int charSize=1; you don't need this. 
     //int *ptr=malloc(MAX_CHARS_INPUT*sizeof(charSize)); 
     char *ptr=malloc(MAX_CHARS_INPUT*sizeof(char));//sizeof(char) will give you the block size, MAX_CHARS_INPUT: gives you the number of blocks to be allocated and pointer type is char, since you want to save char(s), right? 

     int i=0, j=0, c; 
     printf("please enter a string\n"); 

     //while ((c=getchar())!=EOF && c!='\n') 
     while ((c=getchar())!='\r') //'\r' is for enter key... since the user input is coming from console not form a file, right? 
     { 
      ptr[i++]=c; 
      if (i==(MAX_CHARS_INPUT-1)) /*if we need to realloc*/ 
      if (i==MAX_CHARS_INPUT) // i is already incremented in i++ 
      { 
       //int *temp=realloc(ptr,D_SIZE*MAX_CHARS_INPUT*sizeof(charSize)); 
       char *temp=realloc(ptr,D_SIZE*MAX_CHARS_INPUT*sizeof(char)); 
       if (temp==NULL) /*realloc failed*/ 
       { 
        printf("you wrote:\n"); 
        while(j<=i) 
         putchar(ptr[j++]); 

        free(ptr); 
        ptr=(char*)malloc(MAX_CHARS_INPUT*sizeof(char)); 
       } 
       else 
        ptr=temp; 
      } 
     } 
    } 

    int main(){ 
     printWithMalloc(); 
     return 0; 
    } 
+0

谢谢,我会在几个小时内调查一下。 – Alan