473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

sizeof type pointed to by pointer?

Ole
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
Dec 7 '06 #1
4 1044
"Ole" <ol*@blabla.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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?
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
Dec 7 '06 #2
Ole wrote:
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?
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
Dec 7 '06 #3
Ole
"Tom Widmer [VC++ MVP]" <to********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Ole wrote:
>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?

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
Thanks, I was worried about if sizeof would evaluate to a different result
in some special cases like uninitialized pointers etc.

Ole
Dec 7 '06 #4

"Ole" <ol*@blabla.comwrote in message
news:eq**************@TK2MSFTNGP03.phx.gbl...
"Tom Widmer [VC++ MVP]" <to********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Ole wrote:
>>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?

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

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

Dec 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Metzen | last post by:
hello, ok, I want to find the length of an int array that is being passed to a function: main() { int array={1,2,3,4,5,6,7,8}; function(array); } function(int *array) {
4
by: Gopal-M | last post by:
I have the problem with sizeof operator I also want to implement a function that can return size of an object. My problem is as follows.. I have a Base class, say Base and there are many class...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
3
by: Xiangliang Meng | last post by:
Hi, all. How does this 'sizeof(*area)' work? I think the reason is the sentence in C99: If the operand has type ¡®¡®pointer to type¡¯¡¯, the result has type ¡®¡®type¡¯¡¯, but I'm not sure. Could...
15
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
20
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to...
42
by: Jess | last post by:
Hello, if I have the following code that has an array of int*: #include<iostream> #include<string> #include<cstring> #include<cstddef> using namespace std; int main(){
17
by: venkat | last post by:
Hi, I have written a program void main() { printf("%d %d\n", sizeof main, sizeof(main())); } in this program the output is 1 and 4,
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.