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

// using malloc
void main(){

    int n,i;
    printf("Enter size of n \n");
    scanf("%d",&n);

    int *A = (int*)malloc( sizeof(int) * n );

/*  for(i=0;i<n;i++){
        A[i] = i+1;
    }*/

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

}

in this code i have to get output as a garbage values but i'm getting all zeros why? (why i am expecting garbage values is malloc default allocation is garbage values)

sorry for poor english......

user3815361
  • 23
  • 1
  • 10

4 Answers4

5

There is no rule that the memory returned by malloc() is set to anything in particular, it's simply undefined. "All zeroes" is certainly within the confines of "anything". You can't depend on the memory being zeroed, but you calso cannot depend on it not being zeroed.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

malloc do not initialize allocated space to 0 but that doesn't mean that you will not get 0. Accessing uninitialized memory will give you indeterminate value (either an unspecified value or a trap representation).

Quoting from this answer:

[...] using an unitialized value is by itself not undefined behavior, but the value is simply indeterminate. Accessing this then is UB if the value happens to be a trap representation for the type.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
1

What malloc() does is it allocated some memeory on heap and returns the pointer to the allocated memory. So if you try to read the values in the allocated memory before writing something to it the behavior is undefined.

So if you ask why 0 then I say I can't explain undefined behavior. On some systems you might not get 0. malloc() doesn't guarantee initializing the allocated memory to 0 so people use something like memset() to initialize the allocated space.

Gopi
  • 19,784
  • 4
  • 24
  • 36
  • The values are undefined, yes, but I don't *think* you get full-blown nasal demon undefined behavior just by reading uninitialized memory. – Kevin Jan 16 '15 at 16:18
  • 3
    @Kevin The allocated memory is not initialized and accessing uninitialized locations lead to UB – Gopi Jan 16 '15 at 16:20
  • i read that if you not store any values into array it will take garbage values but when im compiling this program im getting zero as output ... – user3815361 Jan 16 '15 at 16:23
  • 2
    @Gopi: It's slightly more complicated... but good enough as a first approximation. Iff the type has trap-representations (implementation-defined), or it's a memoryless automatic variable (coud have been defined with `register`), it's UB. First is unlikely for `int` on any current system, second is no consideration here. – Deduplicator Jan 16 '15 at 16:35
  • @Gopi; I changed my mind. Behavior is not undefined but the value is indeterminate. – haccks Jan 16 '15 at 17:03
  • @haccks Don't you think using indeterminate values lead to undefined behavior. – Gopi Jan 16 '15 at 17:14
  • @Gopi; I added a link to my answer. Read the linked answer for detailed explanation. – haccks Jan 16 '15 at 17:17
  • @haccks Just taking OP's case expecting garbage or `0` with `malloc()` is not defined. Can't we look at it that way? I get that the values are undeterminate and I myself have mentioned in few answers – Gopi Jan 16 '15 at 17:22
  • Value is not defined doesn't mean that the behavior is undefined. Program will not gonna crash or something. – haccks Jan 16 '15 at 17:47
1

This is the output of your program on my computer:

Enter size of n
30
Values in Array
0 -268435456 0 -268435456 2100953104 32767 -1897535813 32767 2100998344 32767 -1897535812 32767 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

It's not all zeroes.

I ran it again:

Enter size of n
30
Values in Array
0 -1610612736 0 -1610612736 2100953104 32767 -1897535813 32767 2100998344 32767 -1897535812 32767 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

and again:

Enter size of n
30
Values in Array
0 268435456 0 268435456 2100953104 32767 -1897535813 32767 2100998344 32767 -1897535812 32767 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

This is why it's called "undefined behaviour" (UB for friends) :-D

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Hooray, after several executions I got a full pack of 30 zeroes. ;-) – axiac Jan 16 '15 at 16:31
  • Not quite UB, see https://stackoverflow.com/questions/27988171/error-in-malloc-program#comment44368042_27988231 – Deduplicator Jan 16 '15 at 16:41
  • Ok, then. It's not undefined behaviour, it's just random data. And random means *anything*. Even a bunch of zeroes is random data if you cannot tell in advance that you'll get it. I think it's less important how we name it. `malloc()` does not guarantee anything about what you will get in the memory block it returns. It can be zeroes, it can be some piece of a program you ran before, it can even be the letter you wrote to your mom two hours ago in an email program, it can be anything. Don't make assumptions about what you'll get and **always** initialize that memory before reading from it. – axiac Jan 16 '15 at 16:53
  • 1
    Ah, we are programmers, being pedantic is thus a neccessity ;-) Which is the reason I commented in addition to upvoting the trial-run showing you can get different results without changing anything. (As an aside, I prefer arbitrary in this context) – Deduplicator Jan 16 '15 at 16:57
  • Indeed, in English "arbitrary" is probably better than "random" in this context. I am not a native speaker :-) – axiac Jan 16 '15 at 17:13
  • Me neither. Welcome to the club. – Deduplicator Jan 16 '15 at 17:14