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

Pass and cast address of cast

Can we assume member variables reside in a class as they would in a structure? It is legitimate to assume the memory layout of the field of a structure. Can we assume the same for a class?

e.g.
struct
{
long x;
long y;
} s;

char* p = (char*)&s.x;
long* q = p+4;

In memory both s.x and s.y are 4-bytes and assuming compiler packs memory then the address of s.y is the address of s.x plus 4 bytes. p points to first byte of s.x and q points to first byte of s.y.


class MyClass
{
public:
long x;
long y;
};

MyClass t;

char* p = &t.x;
char* q = p+4;

Does q point to first byte of t.y? Always?


};
Jun 2 '07 #1
1 2700
weaknessforcats
9,208 Expert Mod 8TB
Can we assume member variables reside in a class as they would in a structure? It is legitimate to assume the memory layout of the field of a structure. Can we assume the same for a class?
You may. C only has structs. C++ is a replacement for C. Therefore, C++ onlky has structs. The C++ class is an appeasment to the object technology folks who speak of classes and unless C++ has one, the impression would be that C++ can't do objects. However, the underlying implentation of a C++ class is a struct.

Rephrasing, if you declare each of your struct members as public/private/protected, there is no difference between a class and a struct.

The one difference between the two is the default access specifier. For a struct the default is public: for a class the default is private.

char* p = (char*)&s.x;
What!!! First, this is a bad cast. Second, it's a C cast and not a C++ cast. s.x is a long and no amount of fiddling will make a char.

Worse, there is a presumption that the low 8 bits of a the long is a signed char-including the sign bit.

In memory both s.x and s.y are 4-bytes and assuming compiler packs memory then
You said it yourself: assuming compiler packs... Generally, variables are laid out on int boundaries. That means there can be slack bytes. The compiler will adjust the pointer arithmetic to take this into account ---->>>>>Right up until you pull a cast and make assuptions about memory.

Therefore:
Does q point to first byte of t.y? Always?
is a definite could be.

Please do not make assumptions in C++ and especially do not cast. A cast in C++ means one of two things a) you are calling a relic C function and you have to cast to a void* or somesuch thing, or b) your C++ design is screwed up.
Jun 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
5
by: CViniciusM | last post by:
Hello, The output of the code below is: number = 10 number = 0 (or other number except 15) Why the output of the second 'number' is not 15? Why the 'number' address is not changed? Thanks...
12
by: Andreas Schmidt | last post by:
I am trying to understand the behavior of void pointers. Can someone explain to me why I get a segfault for the following program? #include <stdio.h> void* plusone(void* i){ int* arg =...
7
by: Yoramo | last post by:
hello I'm calling Win32 API methods like "SendMessage" in this method there are the lParam & wParam parameters, sometimes I need to pass a structure address in the lParam or wParam. the only...
6
by: Minfu Lu | last post by:
I have a problem dealing with passing a function address to a COM callback. I use this COM function for communicating to a hardware. My original project was written in VB. I have converted it to...
5
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
5
by: doni | last post by:
Hi, I am a beginner to C and I wrote a program that takes arguments and prints it in hex. I am getting a segmentation fault if I dont pass any arguments instead I want it to display the Usage...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
7
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p,...
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...
1
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.