0
#include<stdio.h>

int main(){
    int *i;
    *i=1;
    int a[5]={10,20,30,40,50};
    printf("%d\n",&a[4]);
    printf("%d\n",&a[3]);
    printf("%d\n",&a[2]);
    printf("%d\n",&a[1]);
    printf("%d\n",&a[0]);
    printf("%d\n",i);

    return 0;
}

printf("%d\n",i); This print statement prints different values every time it is ran, which is understandable as the program is loading on RAM(am I right?).
But,
Then why does printf("%d\n",&a[0]);(or any other a[i]) prints the same value each time the program is run, does that mean the program is saving the array on harddisk, if so why?
Please answer
Please do share this question if you want the answer

VifterGR
  • 11
  • 1
  • 3
    pointer `i` isn't initialized, dangerously when you try to assign it `*i = 1` – long.kl Jan 27 '22 at 07:15
  • most modern OSes have a separate address space for each process. the code will load at the same address every time it loads, unless the program or one of the libraries it loads changes and moves things around – Garr Godfrey Jan 27 '22 at 07:17
  • 1
    but, it is surprising your code even executes that far. writing to random memory location is likely to cause a crash – Garr Godfrey Jan 27 '22 at 07:19
  • @GarrGodfrey, I think writing to a random memory location is undefine behavior – long.kl Jan 27 '22 at 07:20
  • 2
    Using format specifier `%d` when providing a pointer is undefined behaviour. Use `%p` and cast the pointer to `void*`. Otherwise you might just print half of your pointer. – Gerhardh Jan 27 '22 at 07:23
  • 1
    @long.kl yes, "undefined", which in practice means "likely to crash" – Garr Godfrey Jan 27 '22 at 07:26
  • @GarrGodfrey Likely to crash but even more likely to not do anything and introduce a nasty bug. – Shambhav Jan 27 '22 at 07:40

3 Answers3

4

printf("%d\n",i); This print statement prints different values every time it is ran, which is understandable as the program is loading on RAM(am I right?).

No, you're not actually initialising i so its value is random garbage: if you compile using address sanitizer (-fsanitize=address), the program will immediately explode when reaching *i = 1, and compiling with -Wuninitialized will also point to this issue.

But there's no requirement that i is stable either, when properly initialised: it depends how the allocator in use works, and where it decides to put the value in the heap.

Then why does printf("%d\n",&a[0]); (or any other a[i]) prints the same value each time the program is run, does that mean the program is saving the array on harddisk, if so why?

That's just a peculiarity of your machine, it doesn't occur on my system (that is the addres of a moves around): it depends where the runtime decides to put the stack and what it decides to do (or not) before running main (even more so if you enable stack ASLR), since a lives on the stack.

FWIW on my system, using clang, i always has the same value (when properly initialised using int *i = malloc(sizeof *i);, but the value of a moves around.

Masklinn
  • 34,759
  • 3
  • 38
  • 57
  • This helped, Thanks – VifterGR Jan 27 '22 at 07:25
  • FWIW since you seem like a C beginner here are a few things I would recommend: 1. enable "all" warnings (it's not actually all of them but it's a good start) using `-Wall`, if you get a warning go and understand it, then fix it. 2. enable address sanitizer (`-fsanitize=address`) at least in development, asan is quite expensive but has an *extremely* high signal to noise ratio; 3. enable ubsan (`-fsanitize=undefined`), same as asan. – Masklinn Jan 27 '22 at 07:34
0

*i points to a random address anywhere The array a[] is on stack whose (start) address may be the same each time

Tomb
  • 11
  • 2
  • 1
    `*i` points nowhere, because it's not even a pointer. – Jabberwocky Jan 27 '22 at 07:33
  • You mean to say `i` points nowhere? – Shambhav Jan 27 '22 at 07:38
  • @ShambhavGautam no that's not what they wrote, `i` points to some random address because it is not initialized to any specific address. But `*i` is an integer and hence does not point anywhere. – Gerhardh Jan 27 '22 at 07:54
  • @Gerhardh I firstly messed up "nowhere" and "anywhere". And I don't get what you're saying. You are right but I don't think they meant what you mean here. – Shambhav Jan 27 '22 at 08:00
  • Maybe you clarify if you mean the answer from Tomb or the comment from Jabberwocky. – Gerhardh Jan 27 '22 at 08:02
  • clarification: The variable i is an un-initialized pointer to int. The assignment of value 1 to that random pointer address is not allowed and may lead to curious results or exception. – Tomb Jan 27 '22 at 08:53
-1
#include<stdio.h>

int main(){
    int *i;
    int a[5]={10,20,30,40,50};
    //%p denotes we are printing address that a pointer points to 
    printf("%p\n",&a[4]);
    printf("%p\n",&a[3]);
    printf("%p\n",&a[2]);
    printf("%p\n",&a[1]);
    printf("%p\n",&a[0]);
    *i=1;
    printf("%p\n",i);

    return 0;
}
$gcc memory_address.c && ./a.out
0x7ffe282734d0
0x7ffe282734cc
0x7ffe282734c8
0x7ffe282734c4
0x7ffe282734c0
Segmentation fault (core dumped)

$ gcc memory_address.c && ./a.out
0x7fffb1b8b670
0x7fffb1b8b66c
0x7fffb1b8b668
0x7fffb1b8b664
0x7fffb1b8b660
Segmentation fault (core dumped)

We are getting Segmentation fault because i pointer is not assigned a valid reference and array a is assigned new memory on ram every time you run your program.

Udesh
  • 2,415
  • 2
  • 22
  • 32