473,320 Members | 1,952 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.

sizeof(struct::member)

Suppose we have the following structure:

struct foo {
int x, y;
};

Is there a possibility to get sizeof(foo::x) when
no variable of type foo is available ?

I'm not interested with sizeof(int). I want to
express association of sizeof with the variable.
Rafal
--
sed -e s/s/a/g <my_address >my_right_address
Jul 23 '05 #1
3 7424
* Rafal Dabrowa:
Suppose we have the following structure:

struct foo {
int x, y;
};

Is there a possibility to get sizeof(foo::x) when
no variable of type foo is available ?


A common hack is

foo fooObject(); // Not implemented.

std::size_t const s = sizeof( fooObject().x );

If you like to live dangerously you can alternatively just
cast 0 to a pointer to foo and dereference it, but although
it should be practically safe (sizeof arguments aren't evaluated
at run time) I'm not sure about its formal legality: if it were
all OK then presumably the hack above would be less used.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
* Alf P. Steinbach:
* Rafal Dabrowa:
Suppose we have the following structure:

struct foo {
int x, y;
};

Is there a possibility to get sizeof(foo::x) when
no variable of type foo is available ?


A common hack is

foo fooObject(); // Not implemented.

std::size_t const s = sizeof( fooObject().x );

If you like to live dangerously you can alternatively just
cast 0 to a pointer to foo and dereference it, but although
it should be practically safe (sizeof arguments aren't evaluated
at run time) I'm not sure about its formal legality: if it were
all OK then presumably the hack above would be less used.


Forgot to mention: in your particular example case you don't have
to worry about constructor availability, so you could just write

sizeof( foo().x )

but that won't work in general.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
OK, thank you.
Rafal
--
sed -e s/s/a/g <my_address >my_right_address
Jul 23 '05 #4

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

Similar topics

19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
7
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure,...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
2
by: hilmilho | last post by:
Hi you, I'm facing a strange problem, and I don't know what to do. Maybe someone could help me understand this? I'm storing IP and Netmask on the struct below. 1st I store IP an print it, its...
8
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
7
by: tom | last post by:
hiw is calculated size of struct for example: struct w1 { char x1; // 252 __int64 x2; // 8 // should be 260
7
by: sophia.agnes | last post by:
Dear all, why the following program is giving o/p as sizeof(struct emp) = 0 #include<stdio.h> #include<stdlib.h> struct emp
5
by: petschy | last post by:
Hello All, I confronted recently with the fact that the size of a struct member can't be determined with sizeof(Struct::Member). I'm aware of the alternative methods to do this, however, I'm...
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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
0
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.