From: Brian G. <mic...@ad...> - 2004-01-31 15:19:59
|
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: int *point = 0x241xff5f; cout << *point; so I can see what is in block 0x241xff5f. However, Dev_C++ won't allow me to compile this, and I think I know why: 0x241xff5f isn't neccessarily an int. Is there any way to do something like: readMem("0x241xff5f"); and it would return the ONE BYTE that is located at 0x241xff5f? Would the first block of code work if I put an "&" in front of the address? |
From: Michael C U. <mu...@cb...> - 2004-01-31 15:59:04
|
Brian, > int *point = 0x241xff5f; > cout << *point; This doesn't work because it changes the value that *point contains. Remembet that * is the dereference operator. It access the value rather than the address. To access the pointer itself, you don't want anything before it. So here is an example to do what you want: int *point; /* declare a pointer */ point = 0x241xff5f; /* change the address that it points to */ cout < *point; /* derefernce the poiner and print its value */ Notice that i don't use any operator when assigning the value to point. That's because I want to change the actual address that point is point at, rather than change the value that the current address of point contains. Of course, the above example is only useful if you know that the address contains an int value. Also, if you try to access a memory address that doesn't belong to your program, Windows might catch you and kill your program. So make sure you know that the address you assign to point is actually "owned" by your program. Hope this helps, Mike |
From: Per W. <pw...@ia...> - 2004-01-31 17:52:02
|
No, you are confusing the declaration of a pointer variable with the use of one. int *point = 0x241xff5f; does not change what *point contains. It is just an assignment with wrong type, i.e the hexadecimal constant is not of type 'pointer to int'. int *point = (*int)0x241xff5f; would work better - except for the fact that the hexadecimal constant is broken - where does the second x come from? The problem with this construct is that since you don't know what the address 0x241... points to, you don't know if the program has the right to read the address. If it hasn't, the program will be terminated! /Per W On Sat, 31 Jan 2004, Michael C Urban wrote: > Brian, > > > int *point = 0x241xff5f; > > cout << *point; > > This doesn't work because it changes the value that *point contains. > Remembet that * is the dereference operator. It access the value rather > than the address. > > To access the pointer itself, you don't want anything before it. So here > is an example to do what you want: > > int *point; /* declare a pointer */ > point = 0x241xff5f; /* change the address that it points to */ > cout < *point; /* derefernce the poiner and print its value */ > > Notice that i don't use any operator when assigning the value to point. > That's because I want to change the actual address that point is point at, > rather than change the value that the current address of point contains. > > Of course, the above example is only useful if you know that the address > contains an int value. Also, if you try to access a memory address that > doesn't belong to your program, Windows might catch you and kill your > program. So make sure you know that the address you assign to point is > actually "owned" by your program. > > Hope this helps, > > Mike > > > ------------------------------------------------------- > The SF.Net email is sponsored by EclipseCon 2004 > Premiere Conference on Open Tools Development and Integration > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. > http://www.eclipsecon.org/osdn > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Michael C U. <mu...@cb...> - 2004-01-31 18:02:00
|
On Sat, 31 Jan 2004, Per Westermark wrote: > No, you are confusing the declaration of a pointer variable with the use > of one. Oops. Yeah. I read it too quickly and missed that it was an assignment. I also missed that he had a second x in there. |
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 |
From: Michael C U. <mu...@cb...> - 2004-01-31 17:52:44
|
On Sat, 31 Jan 2004, Michael C Urban wrote: > int x, y; > int *p = x; > p += 1; Oops. Of course, that should have been: int x, y; int *p = &x; p += 1; Mike |
From: Per W. <pw...@ia...> - 2004-01-31 17:54:17
|
Hmmm. I assume your example was ment to be: int x,y; int *p = &x; /* Don't forget the address operator here... */ p += 1; /Per W On Sat, 31 Jan 2004, Michael C Urban wrote: > 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 > > > ------------------------------------------------------- > The SF.Net email is sponsored by EclipseCon 2004 > Premiere Conference on Open Tools Development and Integration > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. > http://www.eclipsecon.org/osdn > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |