-2

If you guys see the below code, no memory is allocated( only 0 bytes is allocated) to ptr. But how ptr[0],ptr[1].....ptr[n] can be initialized if there is no space for them.

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

void main(){
    int *ptr;
    ptr = malloc(0);
    ptr[0] = 0;
    ptr[1] = 4;
    ptr[2] = 3;

    printf("%d %d %d",ptr[0],ptr[1],ptr[2]);
}
Lee Taylor
  • 6,091
  • 14
  • 26
  • 43
Allan
  • 43
  • 7
  • `ptr=malloc(0);` what your system `malloc()` implementation says about how much memory it has allocated ? Usually on x86 `malloc(0)` doesn't allocate exactly `0` bytes, it allocates some extra bytes to maintains metadata information. – Achal Jun 29 '19 at 17:48
  • i don't know how to check that? – Allan Jun 29 '19 at 17:49
  • C does not care. It will overwrite whatever is in those memory locations. You just got lucky with no segfault error. – OldProgrammer Jun 29 '19 at 17:49
  • @OldProgrammer Really? – Allan Jun 29 '19 at 17:52
  • @Achal If I allocate 4 or 5 bytes instead of 0 bytes then that also works .....But how ? – Allan Jun 29 '19 at 17:53
  • @OldProgrammer But how it is possible to overwrite if I haven't even allocated memory – Allan Jun 29 '19 at 17:53
  • 2
    Note that one of the correct declarations for `main()` is `int main(void)` (the primary alternative is `int main(int argc, char **argv)`) — see [What should `main()` return in C and C++](https://stackoverflow.com/questions/204476/). The return value from `malloc(0)` is implementation defined. It may be a null pointer or a valid pointer to memory that cannot be dereferenced (assigned to) legitimately. The code shown isn't guaranteed to fail, but neither is it guaranteed to work. The chances are that the `malloc()` you're using allocates as much as 16 bytes of space for your zero byte request. – Jonathan Leffler Jun 29 '19 at 17:54
  • @Allanwe we don't know how much`malloc(0)` allocates, it varies from one environment/platfrom to another. – Achal Jun 29 '19 at 17:55
  • You should use gdb or get the asm code to better understand what happend. I saw some fun stuff with array also that overwrited later variable. – Sami Tahri Jun 29 '19 at 17:56
  • @Achal So if I specify some bytes to get allocated then will it change for one environment to another or will the specified bytes get allocated .. – Allan Jun 29 '19 at 17:57
  • 1
    Possible duplicate of [How can I get the size of an array from a pointer in C?](https://stackoverflow.com/questions/232691/how-can-i-get-the-size-of-an-array-from-a-pointer-in-c) – Achal Jun 29 '19 at 17:59
  • 1
    @Allan: the number of bytes you request will be made available for use; there may be more, but you cannot legitimately access them. (Linux has an odd view about over committing memory; even if you ask for preposterous amounts of memory, the allocation won't fail when you call `malloc()`, but might fail later when you try to use it. I've never understood why this is beneficial, but that's the chosen implementation. Yes, the results vary by platform, but if you request N bytes, you should be able to use N bytes, but you should not try to use N+1 bytes. – Jonathan Leffler Jun 29 '19 at 17:59
  • 6
    Welcome to the world of the magic **UNDEFINED BAHAVOIUR**. Anything may happen, it may work or not, it may buy you a pizza or send your bank account details to me. Anything is possible. Sane programmers avoid UBs, or at least use them if they really know the implementation (very liked by the uC bare metal ones like me :) ) – 0___________ Jun 29 '19 at 18:00
  • 1
    Similar one [Why does malloc allocate a different number of bytes than requested?](https://stackoverflow.com/questions/430163/why-does-malloc-allocate-a-different-number-of-bytes-than-requested/32677300#32677300) – Achal Jun 29 '19 at 18:00
  • It was using malloc(0) that brought zombo.com into existence. Undefined behavior is undefined. – Dave S Jun 29 '19 at 18:01
  • #include #include #include int main(void){ float *ptr; ptr=malloc(2); float j=0; for(int i=0;i<1000;i++,j++){ ptr[i]=j; printf("%f \n",ptr[i]); } } – Allan Jun 29 '19 at 18:09
  • The output for the above code(in the above comment) is printing upto 679 . – Allan Jun 29 '19 at 18:09
  • Thats means malloc is allocating extra 650 bytes – Allan Jun 29 '19 at 18:11
  • Am i correct guys ? Thanks for helping out . – Allan Jun 29 '19 at 18:11
  • 2
    Nobody cares how much "extra" memory you get. Accessing memory past what you requested is undefined behavior. Your code is fundamentally broken and is not useful or interesting. – Blastfurnace Jun 29 '19 at 21:24

1 Answers1

-3
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main(void){
float *ptr;
ptr=malloc(2);
float j=0;
for(int i=0;i<1000;i++,j++){
ptr[i]=j;
printf("%f \n",ptr[i]);
}
}

Output to be shown: Segmentation error;

Possible Output to be shown: 0 1 2......999

But the Actual Output is: 0 1 2.........679

From this we can conclude that malloc is allocating extra 660 bytes.

In this case,malloc allocates extra bytes from what you have specified. But this doesn't apply for all cases. This is just one example. malloc will show UNDEFINED BEHAVIOUR sometime's depending on the environment.

Allan
  • 43
  • 7
  • 1
    "malloc always allocate extra bytes from what you have specified". Not true. This is still Undefined Behavior, and anything can happen, including the results you got. – FredK Jun 29 '19 at 18:37