0

Here's the snippet

main(){
//
  while(){
  int b;
  scanf("%d",&b);
  int arr[b];
  }
}

Ok now, Since I don't know the exact bound of array, I have to take in as a variable*using scanf*. And the array created contains garbage value. And for that purpose I have to initialize each element to 0, because that is what I want.

Is there a way I can get this thing to work, without having to initialize every time inside the loop?

Thanks

EDIT I guess I can use calloc to allocate the memory. Will there be a different in memory used?

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • @PHIfounder gcc accepts it without error. – Kraken Jul 14 '13 at 08:04
  • The array size should be defined . – 0decimal0 Jul 14 '13 at 08:04
  • Compile with -Wall... like `gcc -Wall prog.c -pedantic -o prog` . – 0decimal0 Jul 14 '13 at 08:05
  • 2
    Of course you can use `calloc()`. – meaning-matters Jul 14 '13 at 08:06
  • @meaning-matters Then instead of asking he should have tried it first ? what say? – 0decimal0 Jul 14 '13 at 08:07
  • 1
    What about using a macro for size? – haccks Jul 14 '13 at 08:08
  • @meaning-matters Yeah, I did try it before editing it, but will there be any difference in the memory usage in two cases? – Kraken Jul 14 '13 at 08:10
  • @haccks Can't do that, I will have to run the same process multiple times, within the while loop, and each time I'll have different boundary index. and also the index is user input. Is there a way of incorporating this using macro? – Kraken Jul 14 '13 at 08:11
  • If you use `calloc`, it will exist on the heap, while what you have will exist on the stack. The stack has limited memory, so if `b` is several million, you are going to have problems. – Mark Lakata Jul 14 '13 at 08:11
  • @Kraken; Is your compiler supports C99 features? – haccks Jul 14 '13 at 08:12
  • @MarkLakata how does the memory allocation differ in two ways, when the value of `b` is not yet taken from the user? Is the memory allocated at run time? – Kraken Jul 14 '13 at 08:12
  • 1
    `memset(arr,0,sizeof(int)*b)` – Mark Lakata Jul 14 '13 at 08:12
  • 1
    How about `memset(arr, 0, sizeof(arr));`? – johnchen902 Jul 14 '13 at 08:13
  • @haccks I am using gcc. – Kraken Jul 14 '13 at 08:13
  • Yes, the memory is allocated (from the stack) at run-time, but this is true even if `b` was a constant. – Mark Lakata Jul 14 '13 at 08:15
  • Then it supports c99. You can use VLA(variable length array). Hope [this](http://stackoverflow.com/questions/17332360/initializing-variable-length-array) will help you. – haccks Jul 14 '13 at 08:16
  • 1
    @Kraken You can do it like this `scanf("%d",b); arr[b];`.As if you just want to decide the size of array at run time. Look you got your answer below :) – 0decimal0 Jul 14 '13 at 08:19
  • @MarkLakata If b was constant and I used `int arr[5]` then the memory would have been run time or compile time? Then why do we call malloc allocation dynamic if both are the same and occur at runtime? – Kraken Jul 14 '13 at 08:19
  • It depends on the compiler. Most compilers running on a normal operating system (Linux, Windows, etc) will take memory from the stack at run-time for local variables, also known as `auto` variables. The amount that they take is either known (`int arr[5]`) or unknown (`int arr[b]`). But the memory left in the stack is generally unknown to the application, so there is no guarantee that there will be enough memory on the stack at run-time. Some embedded compilers do NOT use a stack. In that case, all allocation happens at compile time, and `int arr[b]` is not allowed. – Mark Lakata Jul 15 '13 at 16:44

2 Answers2

4

If you need an array of run-time size, then the proper approach depends on how large your array is going to be.

If the value of b can be large, then declaring it as a local array can lead to stack overflow. In that case the better idea would be to dynamically allocate your array using calloc. calloc will properly zero-initialize an int array.

If the array is relatively small and safe to allocate on the stack, the you can simply declare it as a local array (which is what you did in your original code) and then set it to all-zeros by using memset(arr, 0, sizeof arr).

Of course, in both cases you might end up with a hidden memory-zeroing cycle implemented inside the library function. In general case it is not possible to set a large region of memory to zero without using a cycle.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

If your compiler supports C99 then you can easily use VLA's;

int a, b; 
printf("Enter size of array: ");
scanf("%d",&b);

int arr[b];

and to initialize its all elements to zero in your case use an inner loop or standard library function memset.

memset(b, 0, sizeof(b));
haccks
  • 104,019
  • 25
  • 176
  • 264