473,394 Members | 1,721 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,394 software developers and data experts.

C memory properties

According to:
http://groups.google.co.in/group/com...5?dmode=source

This is valid C code:

#include <stdio.h>
#include <stddef.h>
struct xyz { int x; int y; int z; };
int main(void)
{
struct xyz x_y_z = { 0 };
struct xyz *xyz_ptr = &x_y_z;
int *ip = &x_y_z.z;
ptrdiff_t diff = (char *)ip - (char *)xyz_ptr;
printf("%d\n", (int)diff);
return 0;
}

Yes, it is. But it is not semantically valid, right? Because as far as
I know,
this assumes a particular kind of memory model. Who is to say
that the address stored in 'ip' is a value that is /greater/ than that
stored in 'xyz_ptr'?

It could just as well be likely that 'xyz_ptr' contains a value that
could be regarded as greater than that stored in 'ip', yes?

In which case, the above invokes implementation defined behavior?

Or am I looking at subtraction on two pointer values incorrectly?
Instead of thinking about it in normal terms, regardless of
the values(such as memory addresses that may be assigned to
objects incrementally or decrementally), it should produce the proper
offset under a conforming virtual C machine?

--
aegis

Dec 9 '05 #1
2 1119
In article <11**********************@g47g2000cwa.googlegroups .com>,
aegis <ae***@mad.scientist.com> wrote:
struct xyz { int x; int y; int z; }; int *ip = &x_y_z.z;
ptrdiff_t diff = (char *)ip - (char *)xyz_ptr; Yes, it is. But it is not semantically valid, right? Because as far as
I know,
this assumes a particular kind of memory model. Who is to say
that the address stored in 'ip' is a value that is /greater/ than that
stored in 'xyz_ptr'?


The C standard says so. It says that within structures, items are
stored at increasing addresses.

The standard does not define the order of bytes within an arithmetic
type.

The standard allows for internal padding, so the difference
you get out of the program you showed, will not necessarily be the same
as 2 * sizeof(int) .
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Dec 9 '05 #2

"aegis" <ae***@mad.scientist.com> wrote
This is valid C code:

#include <stdio.h>
#include <stddef.h>
struct xyz { int x; int y; int z; };
int main(void)
{
struct xyz x_y_z = { 0 };
struct xyz *xyz_ptr = &x_y_z;
int *ip = &x_y_z.z;
ptrdiff_t diff = (char *)ip - (char *)xyz_ptr;
printf("%d\n", (int)diff);
return 0;
}

Yes, it is. But it is not semantically valid, right? Because as far as
I know,
this assumes a particular kind of memory model. Who is to say
that the address stored in 'ip' is a value that is /greater/ than that
stored in 'xyz_ptr'?

It could just as well be likely that 'xyz_ptr' contains a value that
could be regarded as greater than that stored in 'ip', yes?

In which case, the above invokes implementation defined behavior?

Or am I looking at subtraction on two pointer values incorrectly?
Instead of thinking about it in normal terms, regardless of
the values(such as memory addresses that may be assigned to
objects incrementally or decrementally), it should produce the proper
offset under a conforming virtual C machine?

Your attitude is right, but the detail is wrong.

C does require that the address of the first element of the structure be the
same as the address of the structure, and that members be arranged in
increasing order. This is to make it easier to interface with other
languages, and to join up code which uses different structures.

However as a general rule a structure should be treated as a black box, with
the members at arbitrary locations within it.
Dec 10 '05 #3

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

Similar topics

0
by: Brian Piscopo | last post by:
Memory leak and .NET -- like oil and water :- I've written a distributed system for parallel processing of large tasks. Each machine has an agent on it that sends processor usage information back...
0
by: Bill Burwell | last post by:
Which memory properties, or what combinations of memory properties, provide useful information about a program's memory usage when that program has just started leaking memory? While I have...
9
by: Jimmy Cerra | last post by:
I am a little confused how the memory for objects is allocated in JavaScript. David Flanagan, in "JavaScript: The Definitive Guide," states that each property of a class takes up memory space when...
10
by: Markus.Elfring | last post by:
Some APIs/SDKs are available to modify settings like "readable", "writeable" or "executeable". Examples: - http://msdn.microsoft.com/library/en-us/memory/base/memory_protection_constants.asp -...
5
by: Ron Mexico | last post by:
I have written a graphing engine (very similar to what BigCharts.com offers). Basically, it's an ASPX page that accepts parameters and calls back-end business objects (dlls) to create a graph. ...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
2
by: Peter Hartlén | last post by:
Hi! The basic questions here is how an object is stored in memory. I have an list of a custom object, where the object has a couple of fields, properties and methods. If we pretend that he...
0
by: jeffrijo | last post by:
One of my SQL Server is confiured as follow: IBM Hardware 16 proc and 32GB of memory OS = Windows 2003 Data Center Ed /pae switch enabled SQL Server 2000 EE with AWE enabled Max SQL Server...
3
by: =?Utf-8?B?VG9tKys7?= | last post by:
When I create an object in VB.NET I define both properties (data) and methods (code). If I have 1,000 instances of this object stored in a Dictionary as Dictionary(of int32, object) then I am...
1
by: Jonathan Wilson | last post by:
I have a closed source app. I have a .dll plugin for this app (which I am writing). This plugin contains a bug somewhere which seems to clobber memory in a "random" fashion (as in, its not...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.