|
From: Michael C U. <mu...@cb...> - 2004-01-31 17:48:16
|
On Sat, 31 Jan 2004, Brian Gordon wrote: > I'm new to C++ and in my C++ book, I am learning about pointers. I have > a question: can you store an address to some kind of "omnitype" pointer? > I am thinking about doing something like this: Although in my last message I showed you how to do what you are asking, I would also stress that directly manipulating pointer addresses is usually quite risky unless you are doing some kind of very low level programming (like programming a microcontroller). Pointer address manipulation is only guranteed to behave well if you are dealing with an array, where you know the the elements of the array are stored contigiously in memory. The following code sample is a disaster waiting to happen: int x, y; int *p = x; p += 1; In this case, I have increased the address of *p by one integer offset. In other words, I have increased the address that *p points to by 32 bits. So *p no longer points at x. But does it point at y? Probably not, because there is no gurantee that x and y are stroed contigiously in memory, even though they were declared one right after the other. So in the above sample, we cannot predict what the results will be since *p now points to an address with unpredictable contents. (More then likely, *p now points to garbage). Hope that clears things up, and hope I wasn't just telling you stuff you already know. But you said you were new to C++, and it looked like what you were doing was probably unsafe. Mike |