Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2006

Tip: Looking for answers? Try searching our database.

sizeof type pointed to by pointer?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ole - 07 Dec 2006 10:41 GMT
Hi,

Doing this will work:

double *dPointer;
result = sizeof(*dPointer);

if I want the size (in this case 8) of the type that the pointer is pointing
at, but is there a more correct way to obtain it?

Thanks
Ole
William DePalo [MVP VC++] - 07 Dec 2006 15:24 GMT
> Doing this will work:
>
[quoted text clipped - 3 lines]
> if I want the size (in this case 8) of the type that the pointer is
> pointing at, but is there a more correct way to obtain it?

I don't know what "more correct" means in this context.

That said, both

   sizeof(double)

and

   sizeof(*dpointer)

will yield the same result while

   sizeof(dpointer)

yields the size of the pointer itself.

Does that help?

Regards,
Will
Tom Widmer [VC++ MVP] - 07 Dec 2006 15:52 GMT
> Hi,
>
[quoted text clipped - 5 lines]
> if I want the size (in this case 8) of the type that the pointer is pointing
> at, but is there a more correct way to obtain it?

Nope, what you've got is fine. sizeof is guaranteed not to evaluate the
expression passed to it, so although your code appears to dereference an
uninitialized pointer, in fact it doesn't.

Tom
Ole - 07 Dec 2006 18:17 GMT
>> Hi,
>>
[quoted text clipped - 11 lines]
>
> Tom

Thanks, I was worried about if sizeof would evaluate to a different result
in some special cases like uninitialized pointers etc.

Ole
Ben Voigt - 07 Dec 2006 20:25 GMT
>>> Hi,
>>>
[quoted text clipped - 14 lines]
> Thanks, I was worried about if sizeof would evaluate to a different result
> in some special cases like uninitialized pointers etc.

No, sizeof is a purely compile-time operator, with no support for
polymorphism or RTTI.  This allows you to use it in place of a literal
constant, for example to declare the size of an array:

union {
double dbl;
char bytes[sizeof(double)];
};

> Ole

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.