LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sizeof applied on a struct (https://www.linuxquestions.org/questions/programming-9/sizeof-applied-on-a-struct-310342/)

George_gk 04-06-2005 09:30 AM

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

rose_bud4201 04-06-2005 11:40 AM

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?

Nad0xFF 04-06-2005 12:18 PM

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

George_gk 04-06-2005 12:40 PM

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

Nad0xFF 04-06-2005 12:57 PM

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)

rose_bud4201 04-06-2005 01:31 PM

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.

Nad0xFF 04-06-2005 02:27 PM

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 :) )

Nad0xFF 04-06-2005 02:29 PM

struct S
{
u_int8_t IPaddress[32];
u_int8_t L2address[8];
u_int8_t CoA[32];
}
;

sizeof(struct S);

should work

Nad0xFF 04-06-2005 02:43 PM

#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)

George_gk 04-07-2005 03:06 AM

Thank you guys,

sizeof(struct S) works also in my machine


All times are GMT -5. The time now is 01:57 PM.