473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

MEMORY ALLOCATION ORDER.


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[i]);
}
}

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?

Jul 23 '05 #1
5 1188
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.

Jul 23 '05 #2
mufasa wrote:
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
....
Jul 23 '05 #3

Ron Natalie wrote:
mufasa wrote:
printf("\n%u\n",&temp);


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

Jul 23 '05 #4
Default User wrote:
Ron Natalie wrote:
mufasa wrote:
printf("\n%u\n",&temp);


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.


(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.

Jul 23 '05 #5

Old Wolf wrote:
Default User wrote:
Ron Natalie wrote:
mufasa wrote:

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

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.


(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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Maurice | last post by:
Hi there, I'm experiencing big memory problems on my webserver. First on an old RedHat 7.2 system, now on an other fresh installed Suse 8.2 system: Linux version 2.4.20-4GB...
1
by: Alex Vinokur | last post by:
Hi, I have a problem with invoking an instance before main() when using memory allocation for static member. Is the program below valid? =========================================== Windows...
11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
4
by: kk | last post by:
Hi all, i didn't get output in the following code while compiling and executing with g++ version 3.2.3 it doesn't allocate memory to pointer varaible (x) in class B. and it gives correct output...
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
12
by: Grahamo | last post by:
Hey, this is related to absolutely nothing in particular and is of no importance whatsoever except for my curiousity. Does anybody have an example of a particularly tricky memory leak that's...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
8
by: Cuthbert | last post by:
Hi folks, This question is a little deep into memory management of microprocessor. How do we know the memory arrangement using in microprocessors? Top-Bottom or Bottom-Top? For example, the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.