2015-06-01 158 views
2

我希望能够重新分配我的程序在数字中读取的空间量。 例如在运行时,它应该能够读取任何数量的整数,然后打印出来作为“int数组:(所有输入)” 这是我到目前为止已经试过:malloc和整数数组的realloc大小

int main(int argc, char **argv) 
{ 
    int i = 0, n, num; 
    int *A; 

    A = malloc(sizeof(A)); 

    while(scanf("%d",&num) == 1) 
    { 
     A[i] = num; 
     i++; 
    } 

    n = sizeof(A)/sizeof(A[0]); 

    printf("Int Array: "); 
    for (i = 0; i < n; i++) 
    { 
     printf("%d ", A[i]); 
    } 

    return 0; 
} 
+0

'INT A *'是无效的! –

+1

另外,'sizeof A'通常给你4或8,即指针的大小,而不是它指向的数据的大小。 – vsoftco

回答

2

你也可以先预留内存即特定量的:如果用户输入10余项你的realloc的内存另一块说20个整数 最后10个项目复制到新的块等上。

size_t block_length = 10; 
size_t current_length = 0; 
int *A = malloc(sizeof(int) * block_length); // to store 10 integer 
if (A == NULL) 
    doNotContinue_AllocationFailure(); 
current_length += block_length; 
// ... input the first ten numbers, check if count numbers is lesser 
// than block_length 
void *ptr; 
ptr = realloc(A, sizeof(int) * (block_length + current_length) 
if (ptr == NULL) 
{ 
    free(A); /* Otherwise a memory leak occurs */ 
    doNotContinue_AllocationFailure(); 
} 
A = ptr; 

// A can now hold up to 20 numbers, and you can input the next 10 numbers 
4

您的代码一些问题

  1. 语法int A*;是无效的,你的意思是int *A;
  2. 如果你想分配一个元素正确的语法是

    A = malloc(sizeof(*A)); 
    
  3. 在这一行

    n = sizeof(A)/sizeof(A[0]); 
    

    sizeof()操作者给出其是poitner类型的大小,在这种情况下它是一样

    n = sizeof(void *)/sizeof(int); 
    

    其可以是21

可以静态设置数组的大小,在这种情况下,我会建议避免malloc(),或者您可以要求用户在任何情况下,你不能从指针获得大小,所以你必须存储,例如

if (scanf("%d", &size) != 1) 
    return -1; 

A = malloc(size * sizeof(*A)); 
if (A == NULL) 
    return -1; 
/* proceed to work with `A' and keep `size' somewhere, you need it */ 
free(A); 
+0

哦,那么它不可能改变用户输入的大小?所以你必须事先询问用户或设定一个尺寸? – Kyuu

+0

以及如果我要事先设置大小,我将如何使用realloc将其限制为用户输入的元素数量。所以我可以打印出每个int,例如(for(i = 0; i Kyuu

0

一招是在每个新输入中重新分配内存。

int indefiniteInput() { 

    int num; 
    int index = 0; 
    int *inputs = malloc(sizeof (int)); 

    if (inputs == NULL) { 
     perror("malloc failed!"); 
     return -1; 
    } 

    printf("Input numbers: "); 

    while(scanf("%d", &num) == 1) { 

     // stops getting inputs when -1 is entered, 
     // you can change it to any value you want 
     if (num == -1) 
      break; 

     // reallocates memory for the input 
     int *temp = realloc(inputs, sizeof (int) * (index + 1)); 

     if (temp == NULL) { 
      free(inputs); 
      perror("realloc failed!"); 
      return -1; 
     } 

     inputs = temp; 

     // adds the last input to reallocated memory 
     inputs[index++] = num; 
    } 

    printf("Stored inputs: "); 

    int i = 0; 
    for (; i < index; i++) 
     printf("%d ", inputs[i]); 

    free(inputs); 
    return 0; 
} 

输出:

Input numbers: 5 6 7 8 9 -1 
Stored inputs: 5 6 7 8 9 -1 

Input numbers: 1 2 3 5 -5 -6 89 256 2001 45600 96 33 369 -1 
Stored inputs: 1 2 3 5 -5 -6 89 256 2001 45600 96 33 369 
+0

@rpattiso谢谢你指出。 – raymelfrancisco

+0

不要使用'realloc()'!并且总是认为会发生更糟糕的情况,所以你可以防止不被阻止,就像'malloc()'可能返回NULL一样。 –