Printing a malloc'd pointer always gives the same address

Am I typing this wrong?

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

int
main( void )
{
    int *       p = malloc(100000);
    int *       q;

    printf("%p\n%p\n", (void *)p, (void *)q);

    (void)getchar();            /* to run several instances at same time */

    free(p);
    return 0;
}

I execute it sequentially or on several terminals at the same time, it always prints "0x60aa00000800" for p( qdiffers, however).

EDIT: Thanks for the answers, one of the reasons I was confused was that he printed different addresses each time. It turns out that the new version of the compiler that I started using -fsanitize=address, caused this change. Oops

+4
source share
4 answers

Value qis uninitialized garbage, because you never assign value to it.

, , , p. , ().

0x60aa00000800, 0x60aa00000800, , - . , . ( , .)

, . , , . , , , , .

https://en.wikipedia.org/wiki/Virtual_memory

+5

. malloc + . ,

q . q no value, , . , undefined ( , undefined)

+3

.

malloc() , , .

  • malloc , .
  • . q , , . , ( ).
+2

/ . , , malloc() .

, , , ( ) . . , , .

, , , . , , . . , .

+1

Source: https://habr.com/ru/post/1527583/


All Articles