77 Replies - 3586 Views - Last Post: 14 June 2009 - 09:18 AM
#1
** used in C++, what is it?
Posted 12 June 2009 - 06:42 PM
Replies To: ** used in C++, what is it?
#2
Re: ** used in C++, what is it?
Posted 12 June 2009 - 06:45 PM
pointer to pointer to integer - double indirection
#3
Re: ** used in C++, what is it?
Posted 12 June 2009 - 06:54 PM
#4
Re: ** used in C++, what is it?
Posted 12 June 2009 - 07:06 PM
#5
Re: ** used in C++, what is it?
Posted 12 June 2009 - 07:11 PM
#7
#8
Re: ** used in C++, what is it?
Posted 12 June 2009 - 08:47 PM
mikeblas, on 12 Jun, 2009 - 07:36 PM, said:
I called a pointer "integer variable" because that's precisely what a pointer is in C\C++. A low-level language that uses integers to hold integers? Shocking...
int x = 5; int* ptr = &x; cout << ptr << endl; cout << (int)ptr << endl;
You'll get the same exact output (though in different number systems). Why? Because C\C++ interprets pointers and integers as the same thing.
=============
I feel a big shit-storm coming this way...
This post has been edited by Dantheman: 12 June 2009 - 08:54 PM
#10
Re: ** used in C++, what is it?
Posted 12 June 2009 - 08:53 PM
A pointer is a pointer an integer is an integer -- its not really a good idea to mix the two concepts.
#11
Re: ** used in C++, what is it?
Posted 12 June 2009 - 08:59 PM
NickDMax, on 12 Jun, 2009 - 07:53 PM, said:
A pointer is a pointer an integer is an integer -- its not really a good idea to mix the two concepts.
True. But all the pointer arithmetic and type association is nothing but a compiler feature. You only give a pointer a type so that a compiler can substitute that correct numbers during arithmetic. So an adding 1 to int* is really a 4 being added to an integer.
And I've never seen C\C++ give a different size to pointers and ints. Don't know about other languages, but I was careful to note that I'm only talking about C\C++. Now, if there are some C\C++ implementations that treat those differently, then I will shut up and admit I was wrong.
This post has been edited by Dantheman: 12 June 2009 - 09:02 PM
#12
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:15 PM
Dantheman, on 12 Jun, 2009 - 07:59 PM, said:
Any compiler that targets Win64 is an example that fits your request.
There are many other implementations where sizeof(int*) is not the same as sizeof(int), particularly in embedded systems. For desktop programming, 64-bit operating systems are probably the most common place you'll find the condition to be true. There's still some debate about which model is best.
Even when sizeof(int*) is the same as sizeof(int), your description of the "int **x" declaration is incorrect. The variable contains an address of the address of the integer, not the address of the integer that has the address.
This post has been edited by mikeblas: 12 June 2009 - 09:17 PM
#13
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:21 PM
Kanvus said:
Dantheman said:
Just because you've never seen cases where an integer cannot be used to represent a pointer does not mean they are the same. By the way, do you have a 64 bit system around?
#14
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:28 PM
In 16bit windows/DOS programming the pointer size depended upon the memory model used. While the int size was 16, pointers were either 16 or 32 bits depending upon the memory model.
On the Windows XP64 an int is 32 bits and a pointer is 64 bits.
Pointers and integers are NOT the same, pointers happen to be integral types but they are NOT the same as ints.
#15
Re: ** used in C++, what is it?
Posted 12 June 2009 - 09:29 PM
C:\foo>type ptrs.cpp
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int* ptr = &x;
cout << ptr << endl;
cout << (int)ptr << endl;
cout << sizeof(x) << endl;
cout << sizeof(ptr) << endl;
}
I modified it slightly to show the size of x and the size of the pointer.
When I built it with a 64-bit compiler, I get warnings (as long as I ask for them):
C:\foo>cl /EHsc /Wp64 ptrs.cpp Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64 Copyright (C) Microsoft Corporation. All rights reserved. ptrs.cpp ptrs.cpp(11) : warning C4311: 'type cast' : pointer truncation from 'int *' to 'int' Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation. All rights reserved. /out:ptrs.exe ptrs.obj
When I run the code, it's plain to see that the pointer is eight bytes -- 64 bits -- long, while the integer is only four bytes long.
C:\foo>ptrs 000000000012FEF0 1244912 4 8
|
|

New Topic/Question
Reply




MultiQuote






|