From: Fil on
Hi,

I am currently learning C++ and I am trying to make a function that would
take a parameter and tell what is its size in bytes. I did the following
which works fine with int variables or any other type provided you adapt it:

-------------------------------------------------------------------------------------------

int varSize(int variable)
{
int * pointer;
int * oldPointer;
pointer=&variable;
oldPointer=pointer;
pointer++;
return (int)pointer - (int)oldPointer;
}

-------------------------------------------------------------------------------------------

If I was asking for the size of a double the function would be:

-------------------------------------------------------------------------------------------

int varSize(double variable)
{
double * pointer;
double * oldPointer;
pointer=&variable;
oldPointer=pointer;
pointer++;
return (int)pointer - (int)oldPointer;
}

-------------------------------------------------------------------------------------------

At the end, since I wasn't able to write one only function that would take
as parameter a variable of any type, I had to write one function per type.
That's not really clever. Isn't there a way to define a function with a
parameter with an undefined type or a variant type like in vba?
Do you have any simple suggestion?

Thank you
From: Doug Harrison [MVP] on
On Tue, 27 May 2008 14:25:03 -0700, Fil <Fil(a)discussions.microsoft.com>
wrote:

>Hi,
>
>I am currently learning C++ and I am trying to make a function that would
>take a parameter and tell what is its size in bytes. I did the following
>which works fine with int variables or any other type provided you adapt it:
>
>-------------------------------------------------------------------------------------------
>
>int varSize(int variable)
>{
> int * pointer;
> int * oldPointer;
> pointer=&variable;
> oldPointer=pointer;
> pointer++;
> return (int)pointer - (int)oldPointer;
>}
>
>-------------------------------------------------------------------------------------------
>
>If I was asking for the size of a double the function would be:
>
>-------------------------------------------------------------------------------------------
>
>int varSize(double variable)
>{
> double * pointer;
> double * oldPointer;
> pointer=&variable;
> oldPointer=pointer;
> pointer++;
> return (int)pointer - (int)oldPointer;
>}
>
>-------------------------------------------------------------------------------------------

That approach isn't portable for several reasons, and you can accomplish
the same thing portably with:

size_t varSize(int)
{
return sizeof(int);
}

size_t varSize(double)
{
return sizeof(double);
}

Note that I didn't name these parameters or refer to them inside the
function, because they don't matter once overload resolution has selected
one of these functions.

None of this makes any sense, because varSize(int) will lie about
varSize(x) when x is short, and you would just use sizeof directly instead
of writing a function to hide it.

>At the end, since I wasn't able to write one only function that would take
>as parameter a variable of any type, I had to write one function per type.
>That's not really clever. Isn't there a way to define a function with a
>parameter with an undefined type or a variant type like in vba?
>Do you have any simple suggestion?

You would just use sizeof(x). You could wrap it in a template, but there's
no point, and you don't need to worry about templates for a while.

--
Doug Harrison
Visual C++ MVP
From: Igor Tandetnik on
Fil <Fil(a)discussions.microsoft.com> wrote:
> I am currently learning C++ and I am trying to make a function that
> would take a parameter and tell what is its size in bytes. I did the
> following which works fine with int variables or any other type
> provided you adapt it:
>
> -------------------------------------------------------------------------------------------
>
> int varSize(int variable)
> {
> int * pointer;
> int * oldPointer;
> pointer=&variable;
> oldPointer=pointer;
> pointer++;
> return (int)pointer - (int)oldPointer;
> }

Why not just

size_t intSize() { return sizeof(int); }

You are arriving at the same answer in a rather circuitous way.

> If I was asking for the size of a double the function would be:

return sizeof(double);

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: Fil on
Sure, thanks, I didn't know sizeof().

"Doug Harrison [MVP]" wrote:

> On Tue, 27 May 2008 14:25:03 -0700, Fil <Fil(a)discussions.microsoft.com>
> wrote:
>
> >Hi,
> >
> >I am currently learning C++ and I am trying to make a function that would
> >take a parameter and tell what is its size in bytes. I did the following
> >which works fine with int variables or any other type provided you adapt it:
> >
> >-------------------------------------------------------------------------------------------
> >
> >int varSize(int variable)
> >{
> > int * pointer;
> > int * oldPointer;
> > pointer=&variable;
> > oldPointer=pointer;
> > pointer++;
> > return (int)pointer - (int)oldPointer;
> >}
> >
> >-------------------------------------------------------------------------------------------
> >
> >If I was asking for the size of a double the function would be:
> >
> >-------------------------------------------------------------------------------------------
> >
> >int varSize(double variable)
> >{
> > double * pointer;
> > double * oldPointer;
> > pointer=&variable;
> > oldPointer=pointer;
> > pointer++;
> > return (int)pointer - (int)oldPointer;
> >}
> >
> >-------------------------------------------------------------------------------------------
>
> That approach isn't portable for several reasons, and you can accomplish
> the same thing portably with:
>
> size_t varSize(int)
> {
> return sizeof(int);
> }
>
> size_t varSize(double)
> {
> return sizeof(double);
> }
>
> Note that I didn't name these parameters or refer to them inside the
> function, because they don't matter once overload resolution has selected
> one of these functions.
>
> None of this makes any sense, because varSize(int) will lie about
> varSize(x) when x is short, and you would just use sizeof directly instead
> of writing a function to hide it.
>
> >At the end, since I wasn't able to write one only function that would take
> >as parameter a variable of any type, I had to write one function per type.
> >That's not really clever. Isn't there a way to define a function with a
> >parameter with an undefined type or a variant type like in vba?
> >Do you have any simple suggestion?
>
> You would just use sizeof(x). You could wrap it in a template, but there's
> no point, and you don't need to worry about templates for a while.
>
> --
> Doug Harrison
> Visual C++ MVP
>
From: Fil on
Thanks, I didn't know sizeof().
What's size_t?

"Igor Tandetnik" wrote:

> Fil <Fil(a)discussions.microsoft.com> wrote:
> > I am currently learning C++ and I am trying to make a function that
> > would take a parameter and tell what is its size in bytes. I did the
> > following which works fine with int variables or any other type
> > provided you adapt it:
> >
> > -------------------------------------------------------------------------------------------
> >
> > int varSize(int variable)
> > {
> > int * pointer;
> > int * oldPointer;
> > pointer=&variable;
> > oldPointer=pointer;
> > pointer++;
> > return (int)pointer - (int)oldPointer;
> > }
>
> Why not just
>
> size_t intSize() { return sizeof(int); }
>
> You are arriving at the same answer in a rather circuitous way.
>
> > If I was asking for the size of a double the function would be:
>
> return sizeof(double);
>
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>
 |  Next  |  Last
Pages: 1 2 3
Prev: backward compatibility
Next: Simulating out-of-memory