MEMORY ALLOCATION ORDER.

M

mufasa

Ok! Very riddling to me. I have the following code:

#include<iostream.h>

int main(void)
{
char q[3];
char temp='a';

printf("\n%u\n",&temp);

for(int i=0;i <3;i++)
{
printf("\n%u\n",&q);
}
}

and as an output, I get a sequence of consecutive integers (addresses,
like 3122897889/90/91/92).

However, when I swap the following two lines :
char q[3];
char temp='a';

and make them

char temp='a';
char q[3];

I get an arbitrary memory location followed by 3 consecutive location
addresses. And this happens everytime I run both the versions of my
code.

How exactly is the memory allocated to the datatypes? In what order? Is
it a question worth asking?
 
M

mufasa

As an after thought, I assume that the output by the second version of
the code is an always-expected output. That is, any array would always
be in a sequence while we just can't say anything about other
individual variables.

Also, the completely sequential allottment in the first case could
happen because I have used small sized instances in a simple program,
but such behaviour is not guaranteed everytime. Only the arrays would
be allocated sequential memory.

I am really sorry to bother anyone here.
 
R

Ron Natalie

mufasa said:
printf("\n%u\n",&temp);

Undefined behavior. %u is not compatible with pointers. Use %p to
print pointers. You were lucky here.

How exactly is the memory allocated to the datatypes? In what order? Is
it a question worth asking?

You can't rely on any ordering between different objects. The only
thing you can guarantee is that pointers to elements of the same
array (or one past the end) compare such as if the earlier elements
had smaller values. There's actually no indication that the addresses
are smaller. Machines such as the Cray which are word addressed
actually encoded the byte offset in the word for char* in the high
bits so sequential locations would actually be (for an array starting
at 0x1000):
0x0000000000001000
0x1000000000001000
0x2000000000001000
....
 
D

Default User

Ron said:
Undefined behavior. %u is not compatible with pointers. Use %p to
print pointers. You were lucky here.

Additionally, printf() is a variadic function expecting a void* for %p.
This is one of those places where a cast is needed.




Brian
 
O

Old Wolf

Default said:
Additionally, printf() is a variadic function expecting a void*
for %p. This is one of those places where a cast is needed.

(void *) and (char *) must have the same size, representation etc.
The standard could easily have said that %p could take a char *,
and it would not have required any extra work by compiler writers,
but it didn't for some reason.
 
D

Default User

Old said:
(void *) and (char *) must have the same size, representation etc.

That's correct. I didn't have the original post "in scope" at the time,
I was just responding to Ron's reply.
The standard could easily have said that %p could take a char *,
and it would not have required any extra work by compiler writers,
but it didn't for some reason.

And in practice, most object pointers will be fine. It's just not
guaranteed. For that matter, there's a good chance they'd print fine
with %u. But why gamble on what undefined behavior will do?


Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top