need help with sizeof operator

When I pass a char* array to a object constructor, and then use the sizeof operator to find the length of the array It always gives an answer of 4 bytes (ie one char**) no matter how big the actual size, is there any way to get the actual size in bytes of the pointer, without passing it to the constructor, the following code should give a better idea of the situation< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

For example:

char *names[]= { "eye.bmp","fonts.bmp", "white.bmp"};

int NUM_TEXTURES = sizeof(names)/4; //Returns 3 this is the answer I need

 

Where as if I pass the pointer ‘names’ to the constructor

 

Texture texture(names); //Object declaration

//Code for Texture constructor

Texture(char** _nameoftextures)

{

      textures = _nameoftextures;

      NUM_TEXTURES = sizeof(_t)/4; //Sets NUM_TEXTURES to 1, when I need

                                 //it to be 3

};

Ill try to make it more clearer if its hard to understand, and any help would be appreciated :)



Answer this question

need help with sizeof operator

  • MarkDer

    Thanks for your feedback, for now ill just pass the size to the constructor, thought thats not a bad idea using a vector, I might try implementing that once I got my classes sussed (as im still learning :)).

    Jonathan


  • TimGL

    hey my last post should have said

    NUM_TEXTURES = sizeof(_nameofarray)/4;

    Yeah I thougth thats what might have been going on lol, thanks for the reply ;), Im still a noob when it comes to c++ (as I figured the whole char** by accident), but ill investigate it a bit more.

    The reason I wanted to find the size was so that people could bung in as many textures as they needed and not care about how many were there.


  • Jumb0

    The array name is decayed to raw pointer when passed to your fun (notice your para type is char ** here)

    If you want to know the exact element count of the array, you'd better pass another para "num" or use template to deduce it. See strcpy_s for reference.

    for example

    #define __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_0(_ReturnType, _FuncName, _DstType, _Dst) \
        extern "C++" \
        { \
        template <size_t _Size> \
        inline \
        _ReturnType __CRTDECL _FuncName(_DstType (&_Dst)[_Size]) \
        { \
            return _FuncName(_Dst, _Size); \
        } \
        }


  • Todd Bannar

    vbvan is right.  Once you pass that array to another function, the initialization of that array, hence its size, becomes hidden.

    A good choice for an array implemention that carries its size is the Standard C++ Library std::vector template, but you cannot initialize the array as easily as you're doing above.  You can find more about std::vector in MSDN.

    Brian


  • need help with sizeof operator