James Jenkinson
James Jenkinson

Reputation: 1660

C: array doesn't acquire new memory address

When running the following, it keeps printing the same memory address.

#include <stdio.h>

int array[5] = {2, 4, 6, 8, 10};
int *pointer;


int main()
{
        pointer = array;
        printf("%p:\t%d\n", pointer, *pointer);
        return 0;
}

I don't know whether this is a problem, but I'm still curious to know why this would be the case, when declaring it within main() would keep returning new addresses.

Upvotes: 0

Views: 138

Answers (4)

Andrew McGuinness
Andrew McGuinness

Reputation: 2147

The reason why you normally get the same address for "array" in the code you posted, but get different addresses if you declare array inside main, is that the array outside main has global scope and so is allocated in a single fixed location, whereas array if declared inside main is allocated on the stack when main is entered (and could conceivably be allocated several times if you called main from elsewhere).

The security feature of randomizing addresses that is described in other answers applies to the stack, not to variables with static duration, because common exploits work by overwriting the stack with hostile code, then jumping to it by overwriting a return address, which is also stored on the stack. Overwriting a non-stack variable with hostile code still leaves the attacker the problem of executing it, and so is a lesser security concern.

Upvotes: 0

unwind
unwind

Reputation: 400159

Note that on most modern desktop (and server, of course) operating systems, addresses handled by processes are virtual. This means that when the OS loads the program to run in your process, it sets up a mapping between the physical pages of RAM and the virtual pages that the process "sees".

This mapping seems to be the same for you every time you run the program, but that's strictly a coincidence. The operating system could just as well apply randomization, or any other method that makes the mapping vary.

Note that because of the virtual addresses, it's perfectly possible for any number of your programs to run at the same time (in parallel) and still see the exact same address for the array.

Upvotes: 1

Pavel Radzivilovsky
Pavel Radzivilovsky

Reputation: 19104

This question was a subject of much debate and has a long history.

Originally, when programs ran in physical memory, addresses of things used to depend on what was in the memory before execution.

Later on and with modern CPUs, each process would get its own address space so there is no diversity.

Then, came security people and said that deliberate randomization of program start address and code location would prevent a range of security attacks. Some of them were lynched by a mob of software engineers who said that improving consistency is important to do more useful software testing. Yet, some compilers (e.g. Visual Studio 2010) would do the randomization of code location by default.

It is still not possible and less needed for the data, in most cases. This is why you are getting the same result.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206666

Purely, a coincedence.
You cannot rely on the address to be same in different runs.

Upvotes: 2

Related Questions