This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: sizeof an array data member


MR <nimrod.bm@optusnet.com.au> writes:

> HI All, 
> 
> I realise this might be a dumb question, but here goes.
> 
> if I have a method:
>     const int operator[](const char *) const {
>  	    int len;
> 	    int numer = sizeof (this->data);

This takes the sizeof a pointer - *not* the sizeof the array. Since
    the array is a dynamic structure, you cannot use sizeof to
    determine its size.

> 	    int denom = sizeof data[0];
>  	    len = numer  / denom;
> 
>  	    return len;
>     }
> 
> and a private data member....
> private:
>     float * data;
> 
> that I use as an array of floats by alocating to it with new[], 
> why does the [] overload always return 1?  I was hopeful that 
> it might give me the size of the array!

A function that returns the size of an array ought to be called 'size'
    - not []. I think [] is the worst name I've seen for a function
    that returns a size.

As another poster said, you should use std::vector<float> .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]