Re: Examples Heap Sort - problem

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 12/15/04


Date: Wed, 15 Dec 2004 19:40:13 +1100


"B. v Ingen Schenau" <bart@ingen.ddns.info> wrote in message
news:3262v5F3j336gU1@individual.net...
| Chris ( Val ) wrote:
|
| >
| > "B. v Ingen Schenau" <bart@ingen.ddns.info> wrote in message
| > news:322kkiF3hkq59U1@individual.net...
| > | Dumas wrote:
| >
| > [snip]
| >
| > | To get the number of elements in an array, you can use the following
| > | function
| > |
| > | template <typename T, size_t N>
| > | size_t array_size(T (&)[N]){return N;}
| >
| > [snip]
| >
| > True - However, I personally find it more readable to
| > use the following:
| >
| > template<typename DataType>
| > inline std::size_t SizeOf( DataType& Source )
| > {
| > return sizeof Source / sizeof* Source;
| > }
|
| The problem with this version is that it can be called with a pointer
| argument, which can lead to unexpected behaviour.
| When you use my version, the compiler will immediately diagnose the
| error that you ask the array length of a pointer.

Thanks Bart.

I agree that you're version is safer in this
regard, but I do like readability offered by
the more simplistic version :-)

Thanks for the tip.
Chris Val



Relevant Pages

  • Re: A couple of things from H&S
    ... expression of array type decays to a pointer to the array's first ... element *unless* it's in one of three contexts: the operand of sizeof, ... is quite obvious, p is the pointer. ...
    (comp.lang.c)
  • Re: [C or C++] Is this legal? sizeof *p
    ... where the size of an automatic array is defined at run time. ... > that case if sizeof is applied to a VLA, ... But applying sizeof to a VLA is applying ... > sizeof to an array, not a pointer, and using the name of an array as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How can I get the size of this?
    ... > I am now having a problem at using sizeof() function ... the pointer. ... Operating on something declared as an array returns the size of the ... LPCSTR is already typedef'd as "const char *", ...
    (microsoft.public.vc.language)
  • Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned?
    ... Using SizeOf on variables does not prevent programming mistakes. ... How will you solve the problem for pointer variables? ... For that, I use dynamic arrays, which case I use Length in conjunction with SizeOf to determine the amount of memory occupied by the array data. ...
    (alt.comp.lang.borland-delphi)
  • Re: A question on string literals
    ... and is why I said "or maybe even `sizeof context'". ... "pointer to T"; ... to first element of array". ... Chris Torek, Wind River Systems ...
    (comp.lang.c)