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......

4
  • 1
    it's int main(). Also, Don't cast the result of malloc (and friends). Don't forget that scanf and malloc can fail. Commented Jan 16, 2015 at 16:28
  • 1
    And don't forget that the user could enter a negative value. Commented Jan 16, 2015 at 16:37
  • Expanding on @Kevin's note, or that signed overflow is undefined. Commented Jan 16, 2015 at 16:40
  • @Deduplicator No sign overflow possible with sizeof(int) * n unless the unlikely sizeof(int) > sizeof(size_t). Commented Jan 16, 2015 at 20:34

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

9 Comments

The values are undefined, yes, but I don't think you get full-blown nasal demon undefined behavior just by reading uninitialized memory.
@Kevin The allocated memory is not initialized and accessing uninitialized locations lead to UB
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 ...
@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.
@Gopi; I changed my mind. Behavior is not undefined but the value is indeterminate.
|
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

6 Comments

Hooray, after several executions I got a full pack of 30 zeroes. ;-)
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.
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)
Indeed, in English "arbitrary" is probably better than "random" in this context. I am not a native speaker :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.