Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-06-2005, 09:30 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2005
Location: Athens
Posts: 11
Rep:
|
sizeof applied on a struct
Hello,
I have the following part of code:
memcpy(A, B, sizeof(B));
A and B are structs defined as:
struct S
{
u_int8_t IPaddress[32];
u_int8_t L2address[8];
u_int8_t CoA[32];
}
The memcpy does not work correclty because the sizeof(B) returns 3 and not 32+8+32, so it copies only 3 bytes from B to A
Am I doing something wrong? Is there any other function to return the number of bytes of a struct?
I am using g++ compiler in Linux Mandrake 10.0
Moreover, if I have also a pointer to another struct:
struct S
{
u_int8_t IPaddress[32];
u_int8_t L2address[8];
u_int8_t CoA[32];
S *S_pointer;
}
I define two structs C & D, C pointing to D and D points to NULL. If I have a pointer pointing on C, how can I get the bytes for all the sequence of structs (C & D => [32+8+32+(pointer)] x 2)?
Sizeof(pointer_on_C) could work?
Thanks in advance,
George
|
|
|
|
04-06-2005, 11:40 AM
|
#2
|
|
Member
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920
Rep:
|
I think it depends on how you're defining your structs.
I (sort of) replicated your program (using int instead of u_int8_t), and got the correct answer:
Code:
[lthurber@hostname ~]$ cat test.c
#include <stdio.h>
typedef struct
{
int IPaddress[32];
int L2address[8];
int CoA[32];
}foo;
int main(void)
{
foo mystruct;
int size = sizeof(mystruct);
printf("sizeof(mystruct): %d", size);
printf("%s", "\n");
return 0;
}
[lthurber@hostname ~]$ g++ -o test test.c
[lthurber@hostname ~]$ ./test
sizeof(mystruct): 288
This makes me think that either something's being used wrong somewhere, or that perhaps the way structs pack is affecting how the struct itself is being represented.
Could you post a bit more of the program?
|
|
|
|
04-06-2005, 12:18 PM
|
#3
|
|
Member
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109
Rep:
|
uint8_t is a pointer, and I think, its size is 1 byte. So sizeof(struct)= 3* sizeof(uint8_t) == 3; Everything is correct
(uint8_t points to other 32 uints, but in struct there is only first pointer
|
|
|
|
04-06-2005, 12:40 PM
|
#4
|
|
LQ Newbie
Registered: Jan 2005
Location: Athens
Posts: 11
Original Poster
Rep:
|
At first, I would like to thank both of you for your help.
From your replies I conclude to the fact that if the struct contains pointers, they count as 1 byte, but the struct to which they point is not included in the sizeof calculations.
Is there any other option to include also the size of the structs that are pointed from the pointers?
But, this is more a C++ question than a Linux question. Maybe I have to send it in another forum.
Anyway, thanks,
George
|
|
|
|
04-06-2005, 12:57 PM
|
#5
|
|
Member
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109
Rep:
|
Aclually, size of a pointer is 4 bytes. I guess, this is packing issues. May be helps to declare srtuct as voilatile for compiler not to optimize it
(I don't know)
|
|
|
|
04-06-2005, 01:31 PM
|
#6
|
|
Member
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920
Rep:
|
I think that yes, size of a pointer under normal circumstances is 4bytes, but he's specifying u_int8_t, and therefore if it's 1byte the sizeof call comes out right (and I'm sort of more inclined to trust c++ knowing what sizes its own types are than my own faulty memory  )
The reason that you've got the size of each whole array represented as 1 byte is that you're acually getting at the size of the array identifier, which is -you said it- a pointer and therefore has the size of whatever you declared the array as. If you declared the array as being of type u_int8_t the "sizeof(array)" call will return sizeof(u_int8_32) which is 1byte.
However, if you want to get the total size of the struct plus the actual sum of all the elements in the arrays, my guess is you'll have to manually do the addition --
sizeof(*(foo.IPaddress)) + sizeof(*(foo.L2address)) + sizeof(*(foo.CoA)) ...I've no idea if that will work as written, but that's the idea
or just use a few for loops and get the size of all the elements....maybe there's a more elegant way out there, but I don't know it.
Edit: Yep, it's right. u_int8_t is an unsigned integer, length 8 bits. u_int32_t is 32 bits, also unsigned.
Last edited by rose_bud4201; 04-06-2005 at 01:40 PM.
|
|
|
|
04-06-2005, 02:27 PM
|
#7
|
|
Member
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109
Rep:
|
Quote:
Originally posted by George_gk
But, this is more a C++ question than a Linux question. Maybe I have to send it in another forum.
Anyway, thanks,
George
|
It would be great, if every programming question could be answered in this forum (I guess, it is true now  )
|
|
|
|
04-06-2005, 02:29 PM
|
#8
|
|
Member
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109
Rep:
|
struct S
{
u_int8_t IPaddress[32];
u_int8_t L2address[8];
u_int8_t CoA[32];
}
;
sizeof(struct S);
should work
Last edited by Nad0xFF; 04-06-2005 at 02:33 PM.
|
|
|
|
04-06-2005, 02:43 PM
|
#9
|
|
Member
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109
Rep:
|
#include <stdio.h>
#include <sys/types.h>
struct S
{
u_int8_t IPaddress[32];
u_int8_t L2address[8];
u_int8_t CoA[32];
}
;
int main(void)
{
S mystruct;
int size = sizeof(S);
printf("sizeof(mystruct): %d", size);
printf("%s", "\n");
return 0;
}
as
#include <stdio.h>
#include <sys/types.h>
struct S
{
u_int8_t IPaddress[32];
u_int8_t L2address[8];
u_int8_t CoA[32];
}
;
int main(void)
{
S mystruct;
int size = sizeof(mystruct);
printf("sizeof(mystruct): %d", size);
printf("%s", "\n");
return 0;
}
prints 72 on my machine (works correct)
|
|
|
|
04-07-2005, 03:06 AM
|
#10
|
|
LQ Newbie
Registered: Jan 2005
Location: Athens
Posts: 11
Original Poster
Rep:
|
Thank you guys,
sizeof(struct S) works also in my machine
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:26 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|