473,320 Members | 1,957 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 a relative pointer in a struct

Hi all.
I'm trying to get the size of a variable in a struct by his relative
postion i.e.

///
#define offsetof(s,m) (size_t)&(((s *)0)->m)

struct ThePimp{
char rings[10];
char blings[20];

};

int sizeOfBling = sizeof( (*char)&ThePimp +
(char)offsetof(ThePimp,blings) );

///
(If I have any syntax mistakes please ignore)

You can easily predict that sizeOfBling will be 4... (sizeof(int*))
Is there any way to acomplish it?

Thanks
Mar 20 '08 #1
14 2747
ManicQin wrote:
#define offsetof(s,m) (size_t)&(((s *)0)->m)
AFAIU, however offsetof is defined, it is defined in stddef.h

$ cat foo.c
#include <stdio.h /* printf */
#include <stddef.h /* offsetof */
struct ThePimp { char rings[10]; char blings[20]; };
int main(void)
{
printf("%d\n", (int)offsetof(struct ThePimp, blings));
return 0;
}

$ gcc -std=c89 -pedantic -Wall -Wextra foo.c

$ ./a.out
10
Mar 20 '08 #2
REH
On Mar 20, 11:39 am, ManicQin <Manic...@gmail.comwrote:
Hi all.
I'm trying to get the size of a variable in a struct by his relative
postion i.e.

///
#define offsetof(s,m) (size_t)&(((s *)0)->m)

struct ThePimp{
char rings[10];
char blings[20];

};

int sizeOfBling = sizeof( (*char)&ThePimp +
(char)offsetof(ThePimp,blings) );

///
(If I have any syntax mistakes please ignore)

You can easily predict that sizeOfBling will be 4... (sizeof(int*))
Is there any way to acomplish it?

Thanks
1. offsetof is already defined for you.
2. why not just use sizeof?
3. Where does the "easily predicted" 4 come from?
4. How does your method adjust for alignment?

REH
Mar 20 '08 #3
On Mar 20, 11:41*am, REH <spamj...@stny.rr.comwrote:
On Mar 20, 11:39 am, ManicQin <Manic...@gmail.comwrote:
Hi all.
I'm trying to get the size of a variable in a struct by his relative
postion i.e.
///
#define offsetof(s,m) * (size_t)&(((s *)0)->m)
struct ThePimp{
char rings[10];
char blings[20];
};
int sizeOfBling = sizeof( (*char)&ThePimp +
(char)offsetof(ThePimp,blings) );
///
(If I have any syntax mistakes please ignore)
You can easily predict that sizeOfBling will be 4... (sizeof(int*))
Is there any way to acomplish it?
Thanks

1. offsetof is already defined for you.
2. why not just use sizeof?
3. Where does the "easily predicted" 4 come from?
4. How does your method adjust for alignment?

REH
The "easily predicted" 4 comes from a machine where sizeof(TYPE*) = 4
because it is the native size for that machine (most likely a 32-bit
machine).
Mar 20 '08 #4
REH
On Mar 20, 12:46 pm, rpgfan3233 <rpgfan3...@gmail.comwrote:
The "easily predicted" 4 comes from a machine where sizeof(TYPE*) = 4
because it is the native size for that machine (most likely a 32-bit
machine).
So? Where is the pointer he is trying to determine the size of. I
see two arrays, neither of which would be 4 bytes in size, regardless
of the machine.

REH
Mar 20 '08 #5
REH
On Mar 21, 9:23 am, ManicQin <Manic...@gmail.comwrote:
I would ignore most of the comments
but I do want to reply to somethings
1) sizeof a pointer is 4 (platform specific)
But you are not getting the size of a pointer, both of your fields are
arrays. The size of "rings" is 10. The size of "blings" is 20. This
will always be true, regardless of the target system, unless you
change the type of your array elements, or the number of elements in
your array.

REH
Mar 22 '08 #6
REH
On Mar 21, 9:23 am, ManicQin <Manic...@gmail.comwrote:
I cannot rely on people not messing with my struct so i cannot know
what is after or before the requested var thanks NOOB
and by the way the alignment is dealed thanks REH

How is the alignment problem dealt with? If there is padding before a
field, it will get included in your total when your calculate
offsetof. How do you remove it?

REH
Mar 22 '08 #7
ManicQin <Ma******@gmail.comwrites:
On Mar 22, 6:08 pm, REH <spamj...@stny.rr.comwrote:
>On Mar 21, 9:23 am, ManicQin <Manic...@gmail.comwrote:
I would ignore most of the comments
but I do want to reply to somethings
1) sizeof a pointer is 4 (platform specific)

But you are not getting the size of a pointer, both of your fields are
arrays. The size of "rings" is 10. The size of "blings" is 20. This
will always be true, regardless of the target system, unless you
change the type of your array elements, or the number of elements in
your array.

REH
REH:
Sorry I rechecked it... The way I use it, it returns 4 (or
size_of(pointer) for the pedants)
(See the code I posted and try him - read this post all through to see
why I coded it that way)
Check what REH is saying. He (or she) is saying that you seem to be
trying to get the size of something that is not a pointer. Of course
you get 4 (on your system) if you apply sizeof to a pointer, but your
code:

struct ThePimp{
char rings[10];
char blings[20];

};

int sizeOfBling = sizeof( (*char)&ThePimp +
(char)offsetof(ThePimp,blings) );

suggested you wanted the size of the part of struct (just from the
name of the variable). I think all REH is saying is that you should
not take the size of a pointer if you want the size of something else.
Ha Ha Ha Keith:
In normal systems you were right... But in this hideous system I only
know the offset of the variable...
(That's why I added the offsetof in my code above...)
Are you starting to catch my drift...
Nope. I, for one, am now lost about what your question is. Keith
Thompson told you how to get the size of struct member (which is what
I though was the question) but it seems it is not. You know how to
find the offset of a member (though you should not need to define your
own offsetof macro) so that can't be it.

Are you maybe confused as to why sizeOfBling is 4? It is 4 simply
because the type of the expression sizeof is applied to is a pointer.
Are you confused as to why adding a char to a char * gives a pointer?
There's a huge switch case goes like:
switch (param)
{
case offsetof(rings) ... return 10
case offsetof(blings)... return 20
};
That does not help us follow your problem. Are you trying to write
something like this? Are you trying to modify it in some way?

--
Ben.
Mar 23 '08 #8
Ok Please dont ask me why am I writing this code the way it is, It's
an abbreviation of a much bigger complexed system
and I cant explain why it was chosen to work this way. I'm trying to
fix a particular point in the system.

I'm trying to achieve the line with the comment (and the
returnSizeofParam function) in a smaller, nicer and more DYNAMIC way.
As you can see whenever I add a new Parameter to Parameters struct I
need to update returnSizeofParamByOffest ... for me it looks a bit
crooked...
Is there a way to know the sizeof a variable in a struct only by his
offset. is it possible?

struct Parameters
{
char strIDNum[10];
char strFirstName[20];
char strFamilyName[30];
};

int returnSizeofParamByOffest(int lParamOffset)
{
Parameters cTemp;

switch (lParamOffset) {
case offsetof(Parameters,strIDNum): return sizeof(cTemp.strIDNum);
break;
case offsetof(Parameters,strFirstName): return
sizeof(cTemp.strFirstName); break;
case offsetof(Parameters,strFamilyName): return
sizeof(cTemp.strFamilyName); break;
};

return 0;
}

int main() {
Parameters cTemp= {"AAA" , "BBB" , "CCC"};
char strTempForPrinting[100] = {0};
int lParamToPrint = offsetof(Parameters , strFirstName);
int sizeOfParam =
returnSizeofParamByOffest(lParamToPrint ); //
********************

memcpy(strTempForPrinting,(char*)&cTemp + (char)lParamToPrint ,
sizeOfParam );
printf("The parameter is: %s \n",strTempForPrinting);
return 0;
}

p.s. Please tell me why I wasn't understood in the above posts. (It's
important for me to understand why there was a mis-connection)
Mar 24 '08 #9
ManicQin <Ma******@gmail.comwrites:

Ok Please dont ask me why am I writing this code the way it is,
OK.

<snip>
Is there a way to know the sizeof a variable in a struct only by his
offset. is it possible?
No.
struct Parameters
{
char strIDNum[10];
char strFirstName[20];
char strFamilyName[30];
};

int returnSizeofParamByOffest(int lParamOffset)
{
Parameters cTemp;

switch (lParamOffset) {
case offsetof(Parameters,strIDNum): return sizeof(cTemp.strIDNum);
break;
case offsetof(Parameters,strFirstName): return
sizeof(cTemp.strFirstName); break;
case offsetof(Parameters,strFamilyName): return
sizeof(cTemp.strFamilyName); break;
};

return 0;
}

int main() {
Parameters cTemp= {"AAA" , "BBB" , "CCC"};
char strTempForPrinting[100] = {0};
int lParamToPrint = offsetof(Parameters , strFirstName);
int sizeOfParam =
returnSizeofParamByOffest(lParamToPrint ); //
********************

memcpy(strTempForPrinting,(char*)&cTemp + (char)lParamToPrint ,
sizeOfParam );
printf("The parameter is: %s \n",strTempForPrinting);
return 0;
}
This is not C (I am sure you know that -- the compiler will have told
you so) but since your objective is impossible, there is no point in
trying to make it C.
p.s. Please tell me why I wasn't understood in the above posts. (It's
important for me to understand why there was a mis-connection)
I think this because you question was not clear. You may have hinted
at the above question before, but since the answer is such a clear and
obvious "no" (can you tell how many pages are in a book just from its
position on the shelf?) people may have tried to re-interpret the
hints as some other question.

--
Ben.
Mar 24 '08 #10
On Mar 24, 2:25 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
ManicQin <Manic...@gmail.comwrites:
Ok Please dont ask me why am I writing this code the way it is,

OK.

<snip>
Is there a way to know the sizeof a variable in a struct only by his
offset. is it possible?

No.
struct Parameters
{
char strIDNum[10];
char strFirstName[20];
char strFamilyName[30];
};
int returnSizeofParamByOffest(int lParamOffset)
{
Parameters cTemp;
switch (lParamOffset) {
case offsetof(Parameters,strIDNum): return sizeof(cTemp.strIDNum);
break;
case offsetof(Parameters,strFirstName): return
sizeof(cTemp.strFirstName); break;
case offsetof(Parameters,strFamilyName): return
sizeof(cTemp.strFamilyName); break;
};
return 0;
}
int main() {
Parameters cTemp= {"AAA" , "BBB" , "CCC"};
char strTempForPrinting[100] = {0};
int lParamToPrint = offsetof(Parameters , strFirstName);
int sizeOfParam =
returnSizeofParamByOffest(lParamToPrint ); //
********************
memcpy(strTempForPrinting,(char*)&cTemp + (char)lParamToPrint ,
sizeOfParam );
printf("The parameter is: %s \n",strTempForPrinting);
return 0;
}

This is not C (I am sure you know that -- the compiler will have told
you so) but since your objective is impossible, there is no point in
trying to make it C.
p.s. Please tell me why I wasn't understood in the above posts. (It's
important for me to understand why there was a mis-connection)

I think this because you question was not clear. You may have hinted
at the above question before, but since the answer is such a clear and
obvious "no" (can you tell how many pages are in a book just from its
position on the shelf?) people may have tried to re-interpret the
hints as some other question.

--
Ben.
"I'm trying to get the size of a variable in a struct by his relative
postion"
.... I thought that would be self explanatory - Never mind that now.
Thanks you just shattered my dreams of creating a better world... ;)
BTW why isnt it C?
Mar 24 '08 #11
ManicQin said:

<snip>
"I'm trying to get the size of a variable in a struct by his relative
postion"
... I thought that would be self explanatory
It is. So is "this can't be done".

Think about it. If it were possible to do that, then we could control the
size of an object simply by placing it at a certain position in the
struct. If, say, the relative position "first" yields "2 bytes", then we
could fit a double into two bytes just by making it the first member of a
structure! That simply isn't how the world works. Sorry.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 24 '08 #12
ManicQin <Ma******@gmail.comwrites:
On Mar 24, 2:25 pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>ManicQin <Manic...@gmail.comwrites:
Ok Please dont ask me why am I writing this code the way it is,

OK.

<snip>
Is there a way to know the sizeof a variable in a struct only by his
offset. is it possible?

No.
struct Parameters
{
char strIDNum[10];
char strFirstName[20];
char strFamilyName[30];
};
int returnSizeofParamByOffest(int lParamOffset)
{
Parameters cTemp;
switch (lParamOffset) {
case offsetof(Parameters,strIDNum): return sizeof(cTemp.strIDNum);
break;
case offsetof(Parameters,strFirstName): return
sizeof(cTemp.strFirstName); break;
case offsetof(Parameters,strFamilyName): return
sizeof(cTemp.strFamilyName); break;
};
return 0;
}
int main() {
Parameters cTemp= {"AAA" , "BBB" , "CCC"};
char strTempForPrinting[100] = {0};
int lParamToPrint = offsetof(Parameters , strFirstName);
int sizeOfParam =
returnSizeofParamByOffest(lParamToPrint ); //
********************
memcpy(strTempForPrinting,(char*)&cTemp + (char)lParamToPrint ,
sizeOfParam );
printf("The parameter is: %s \n",strTempForPrinting);
return 0;
}

This is not C (I am sure you know that -- the compiler will have told
you so) but since your objective is impossible, there is no point in
trying to make it C.
p.s. Please tell me why I wasn't understood in the above posts. (It's
important for me to understand why there was a mis-connection)

I think this because you question was not clear. You may have hinted
at the above question before, but since the answer is such a clear and
obvious "no" (can you tell how many pages are in a book just from its
position on the shelf?) people may have tried to re-interpret the
hints as some other question.

--
Ben.
Best not to quote sigs.
"I'm trying to get the size of a variable in a struct by his relative
postion"
... I thought that would be self explanatory - Never mind that now.
Well it was and I answered it. However, this:
BTW why isnt it C?
made me look in detail more. First the answer: it is not C because it
has syntax errors and does not comply with C's other rules. In
particular, offsetof does produce a constant suitable for use in a
switch statement. However, any switch can be replaced by if-then-else
so that would work.

Programmers look for general solutions, so your general question got
answered (at least by me) in the most general way: can't be done. The
solution for one structure, written out by hand, is so obvious that I
did not think you were asking for it:

struct S temp;
if (arg == offsetof(struct S, member1))
return sizeof temp.member1;
if (arg == offsetof(struct S, member2))
return sizeof temp.member2;

and so on. I am sorry if this sounds rude -- I really did not think
you could be asking for this. If you want something more automatic
and general, then the original answer still stands: you can't.

You can use C99 features to make it less of a maintenance problem:

-------------------
#include <stddef.h>

struct S {
int member1;
int member2;
};

size_t get_size(size_t arg)
{
struct S temp;
size_t sizes[] = {
[offsetof(struct S, member1)] = sizeof temp.member1,
[offsetof(struct S, member2)] = sizeof temp.member2,
};
return sizes[arg];
}
-------------------

but that is about it.

--
Ben.
Mar 24 '08 #13
Lets say that the answer did not shock me, I thought maybe there's
something I missed so it was worth asking.
Thank You all.
Mar 24 '08 #14
ManicQin wrote:
"I'm trying to get the size of a variable in a struct by his relative
postion"
... I thought that would be self explanatory
It's bizarre.
It's like "I'm trying to shoot a target by stabbing at it with a knife."

You use the sizeof operator to get the size of a variable.

--
pete
Mar 29 '08 #15

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

Similar topics

12
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class...
2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
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...
15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
15
by: ak | last post by:
struct xy{ int x; int y; } _xy; size_of_xy(struct xy * a) { int len = sizeof a; printf( "\sizeof xy: %i\n", len ); }
2
by: aegis | last post by:
sizeof could not possibly evaluate to some size for a type, unless two things for a type occur: a) the type is complete and sizeof will evaluate to appropriate size b) type is not complete and...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
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: 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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.