473,832 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass and cast address of cast

2 New Member
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 2717
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
9975
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 must be an object instead of
5
1492
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 in advance, Vinicius.
12
6359
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 = (int*)i; int result = (*arg + 1); return (void*)result; }
7
1983
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 way I found that workes is to define SendMessage like that:
6
2408
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 C#. One of the problem is passing a function address to a COM function as a parameter with another progress value. My callback function is very simple using the progress value to update my progressbar. Because this COM function usually takes a long...
5
2368
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 the same alignment requirements. (3) sizeof(double) >= sizeof(unsigned) ===================================== We can cast from double to unsigned int as follows:
5
2080
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 message that I have. Here is my simple program. #include <stdio.h> #include <stdlib.h>
9
3568
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 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback function I need to cast this unmnaged pointer that I have received from 3rd party back into the...
7
3209
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, int n);
0
9794
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10780
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5623
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5788
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4420
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.