sizeof(struct.member) problem

This is a discussion on sizeof(struct.member) problem within the C++ Programming forums, part of the General Programming Boards category; Hey all, a small problem: If I have a basic structure declared as follows: Code: struct mystruct { int item1; ...

  1. #1
    huh
    huh is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    5

    sizeof(struct.member) problem

    Hey all, a small problem:
    If I have a basic structure declared as follows:

    Code:
    struct mystruct
    {
        int item1;
        int item2;
        char name1[CONST_SIZE];
    };
    How do I find the length of a member of mystruct without first declaring it etc?

    i.e. something like:
    Code:
    int nsize1 = sizeof(mystruct.item1); // Like this
    int nsize2 = sizeof(mystruct.name1); // Or this
    
    // Instead of:
    mystruct mys;
    int nsize3 = sizeof(mys.item1);
    int nsize4 = sizeof(mys.name1);

    Cheers for any help, it will be much appreciated

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You might want to look at this code and the output:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int test;
    
    	cout<<sizeof(int)<<endl;
    	cout<<sizeof(test)<<endl;
    	
    	const int CONST_SIZE=20;
    	char name1[CONST_SIZE];
    
    	cout<<CONST_SIZE<<endl;
    	cout<<sizeof(name1)/sizeof(name1[0])<<endl;
    
    	return 0;
    }}
    Last edited by 7stud; 08-08-2003 at 03:08 AM.

  3. #3
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    That wasn't his question. He asked for the size of a structmember without having to create an instance of the struct, if I got it right

    I guess I would try something like sizeof(TMyStruct::iMyIntMember);

    edit: whoop, doesn't work unless you want a to be static...
    Last edited by darksaidin; 08-08-2003 at 03:28 AM.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    uh....

    how bout sizeof(int) and sizeof(char)*CONST_SIZE ?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or, use a pointer:
    Code:
    int main(void)
    {
      mystruct *p;
      cout <<sizeof(p->item1) <<endl;
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >How do I find the length of a member of mystruct without first declaring it etc?

    I normally don't do C++, but I did encounter a similar situation recently in C. And since I haven't seen it mentioned yet and I'm curious if there might be any problems with this in C++, here we go:
    Code:
    /* I'll stick with C and let you C++ folks C++-ize it properly. */
    #include <stdio.h>
    
    #define CONST_SIZE 15
    
    struct mystruct
    {
        int  item1;
        int  item2;
        char name1[CONST_SIZE];
        int  item3[CONST_SIZE];
    };
    
    int main(void)
    {
        printf("sizeof(item1) = %lu\n", (long unsigned)sizeof(((struct mystruct*)0)->item1));
        printf("sizeof(item2) = %lu\n", (long unsigned)sizeof(((struct mystruct*)0)->item2));
        printf("sizeof(name1) = %lu\n", (long unsigned)sizeof(((struct mystruct*)0)->name1));
        printf("sizeof(item3) = %lu\n", (long unsigned)sizeof(((struct mystruct*)0)->item3));
        return 0;
    }
    
    /* my output
    sizeof(item1) = 4
    sizeof(item2) = 4
    sizeof(name1) = 15
    sizeof(item3) = 60
    */
    There seems to be some support for this method here and here.

    And before I'm told that this dereferences a NULL pointer, I believe a C reply I'd tracked down (somewhere) stated that the sizeof operator does not evaluate its argument (except for variable-length arrays in C99), so no dereferencing is actually done. Whether this is the case for Standard C++ I don't know (I only briefly skimmed a draft).

    [edit]
    Fixed a minor typo and highlighted the construct.
    [/edit]
    Last edited by Dave_Sinkula; 08-08-2003 at 01:42 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's pretty slick actually. I never had a need to do that though. I usually will just sizeof(The Type) instead.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by FillYourBrain
    that's pretty slick actually. I never had a need to do that though. I usually will just sizeof(The Type) instead.
    Don't structs use a 4 byte grid to align variables ? That might cause some trouble if you try to calculate the size instead of getting it with sizeof(). I can't really image a situation where sizeof(structtype.field) makes sense, but that doesn't mean there isn't any =)

  9. #9
    huh
    huh is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    5
    Cheers all for the replys, espically Dave Sinkula,

    darksaidin:
    >"Don't structs use a 4 byte grid to align variables ? That might cause some trouble if you try to calculate the size instead of getting it with sizeof()."

    From my poor understanding of C++ that, yes a struct and its members can be aligned (default is to the next 4 bytes boundry, i think) and thus the size of a struct in memory is not the sum of the sizes of its members, but to "cause some trouble if you try to calculate the size instead of getting it with sizeof()" is defeating the purpose of the sizeof() operator anyway.

    >"I can't really image a situation where sizeof(structtype.field) makes sense, but that doesn't mean there isn't any =)."

    If I am storing some data in a database, it's a logical choice to use a structure to represent each record and to reference the data and its individual fields when moving the data in and out of the database.

    If I wanted sort the data I am going to use a key field inside each record that can be compared to other records key fields. Since I have declared my key field inside of a structure already, it would be nice to be able to pass the size of my key field (and its relative offset) to a function that sorts the data, hence my orginal problem.

    Thanks again, huh

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I still don't see it. Do you have a "keyfield" of different types in different records? Suppose it is int.

    CompareByKeyfield(record1.keyfield, record2.keyfield);

    You definitely don't need to know the size here. Additionally, if you used classes (this is the C++ forum after all) you could do a compare like this:

    if(record1 > record2)

    where the overloaded operator needs to know the sizeof
    Code:
    int operator > (Record & record)
    {
    sizeof(keyfield) //do something with the size
    then you clearly don't need any of what we've been talking about anyway!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21