Array sizes and arbitrary sized arrays

This is a discussion on Array sizes and arbitrary sized arrays within the C++ Programming forums, part of the General Programming Boards category; I'm delving deep into the Windows security systems and I've hit a bit of a brick wall. It's made me ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    Array sizes and arbitrary sized arrays

    I'm delving deep into the Windows security systems and I've hit a bit of a brick wall. It's made me realize that my knowledge is quite lacking in some areas. My first problem is using the sizeof() function to get the size of an array or structure pointed to by a pointer. Eg:

    Code:
    char *tc=new char[256];
    
    cout << sizeof(tc) << endl;
    Of course, this didn't work, and why should it? The size of tc is 4, which is appropriate for a pointer. But is there any way to find out the size of the array? Ie, I need a function that will return 256 when I input tc.

    My second problem is with arbitrary sized arrays. For example, here is the structure of TOKEN_PRIVILEGES:

    Code:
    typedef struct _TOKEN_PRIVILEGES 
    {  
    DWORD PrivilegeCount;  
    LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];
    } TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES;
    I'm using this structure in conjunction with the function GetTokenInformation(). The problem is, in the context that I am using it, it needs a TOKEN_PRIVILEGES structure of size 220 bytes. How do I manipulate the structure to achieve this? This is my code:

    Code:
    TOKEN_PRIVILEGES ti;
    DWORD soti; //sizeof(ti)
    DWORD rl; //Returns the required length of the array
    int rCode; //Return code
    
    soti=sizeof(ti);
    rCode=GetTokenInformation(hToken,TokenPrivileges,&ti,&soti,&rl);
    if (!rCode)
    {
    soti=rl;
    GetTokenInformation(...);
    }
    I'm sure you get the idea. Basically, I create the empiric structure, pass it to the function, then repeat if necessary with the appropriate sized array. Except, I need to know how to make the TOKEN_PRIVILEGES structure the size returned in rl. Any help, please?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Of course, this didn't work, and why should it? The size of tc is 4, which is appropriate for a pointer. But is there any way to find out the size of the array? Ie, I need a function that will return 256 when I input tc.
    No such function exists. It's due to how C++ uses arrays and pointers.

    You can use a container like std::vector in place of an array; that may solve this problem.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    To answer your first question about getting the number of elements from your pointer, you can't.

    say you have:

    Code:
    char *pArray = new char[256];
    
    // This will give you the size of the memory address
    cout << sizeof(pArray) << endl;
    
    // This will give you the size of a single element being pointed to
    cout << sizeof(*pArray) << endl;
    So there is no way to know how much memory was allocated. Just need to keep track of it separetly and pass it to other functions if necessary.

    I'm not really sure what you are asking with your other question. Why can't you just dynamically create the structs as you need them?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Okay, I can deal with not being able to get the array size.

    MrWizard, what do you mean by dynamically creating structures?

    I was thinking about the problem and I thought maybe I could do some messy type casting like so:

    Code:
    TOKEN_PRIVILEGES *tp;
    DWORD reqSize; //Required size
    
    GetTokenInformation(hToken,TokenPrivileges,NULL,0,&reqSize);
    tp=(PTOKEN_PRIVILEGES)new BYTE[reqSize];
    GetTokenInformation(hToken,TokenPrivileges,tp,reqSize,&reqSize);
    I know it's messy, but it should work.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

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