LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-06-2005, 09:30 AM   #1
George_gk
LQ Newbie
 
Registered: Jan 2005
Location: Athens
Posts: 11

Rep: Reputation: 0
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
 
Old 04-06-2005, 11:40 AM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
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?
 
Old 04-06-2005, 12:18 PM   #3
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Rep: Reputation: 15
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
 
Old 04-06-2005, 12:40 PM   #4
George_gk
LQ Newbie
 
Registered: Jan 2005
Location: Athens
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 04-06-2005, 12:57 PM   #5
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Rep: Reputation: 15
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)
 
Old 04-06-2005, 01:31 PM   #6
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
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.
 
Old 04-06-2005, 02:27 PM   #7
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Rep: Reputation: 15
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 )
 
Old 04-06-2005, 02:29 PM   #8
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Rep: Reputation: 15
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.
 
Old 04-06-2005, 02:43 PM   #9
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Rep: Reputation: 15
#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)
 
Old 04-07-2005, 03:06 AM   #10
George_gk
LQ Newbie
 
Registered: Jan 2005
Location: Athens
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you guys,

sizeof(struct S) works also in my machine
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
appearence applied to everything, but not to Firefox javb Suse/Novell 1 11-28-2005 10:14 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM


All times are GMT -5. The time now is 04:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration