Pointing at a place
I have been working with pointers, and I am curious is it possible to add one (in hex) to a pointer so that if the location of the pointer is displayed I would get 0x445021 instead of 0x445020. I know that by saying *pa++ and the original address is 0x445020 i would get 0x445028. Or is it possible to force a variable to be at a certain location?
Thanks
[359 byte] By [
9v1cl0] at [2007-11-11 11:58:08]

# 1 Re: Pointing at a place
Is certainly possible. You want to use great care. Rarely would you need to point a variable at a specific location. If, however, you have a memory-mapped timecard, you might might have a routine that sets a pointer to the starting address:
volatile clock_t* clockPtr = SomeFancyLibraryCall();
It is unlikely that you would want to increment such a pointer by one byte.
If, however, you have a pointer to an array of char:
char* text = "How are you?";
char* myPtr = test;
cout << *myPtr
woud give you 't'.
Then myPtr++ and cout << *myPtr
would give you 'e'.
# 2 Re: Pointing at a place
BTW: The increment (++) operator increments your pointer by the number of bytes that constitute an element that is pointed to. If short is a 2-byte integer, ++ on the pointer will increment it by 2. If int is a 4-byte integer, ++ will increment it by 4. "char" is one byte, so ++ increments the pointer by one.
# 3 Re: Pointing at a place
add one hexa value to the value of the pointer and display it ...
it's very easy to force a variable to such location, just assigne to the pointer the location, but this is not safe at all ... how can u know if this location exists in the target computer memory ? and how can u know if it's a free non-protected location ? I recommend to let the OS select this location for you better unless you want to build an OS yourself ..
Amahdy at 2007-11-11 20:57:43 >

# 4 Re: Pointing at a place
char* myPtr = "test"; ...
and special case for char pointers :
cout<< myPtr; will display "test" and not the adress !
that's why you should care with char pointers when you delete them ..
in VC++ 6.0 "delete myPtr" generate an error .. a bug i think.
Amahdy at 2007-11-11 20:58:44 >

# 5 Re: Pointing at a place
Also, by the way,
word alignment is very important for most (all?) computers. A float or double, for instance, must begin on a (memory location) mod 4 boundary for 32-bit computers.
If you have a float point to byte x in memory, changing the pointer to x+1 and using it will result in a segmentation fault.
John
# 6 Re: Pointing at a place
It shouldn't generate an error to "delete myPtr;", but it does constitute a memory leak. You should, instead, "delete []myPtr;" since it is an array.
# 7 Re: Pointing at a place
Test it under visual C++ and you'll see it yourself.
Amahdy at 2007-11-11 21:01:46 >

# 8 Re: Pointing at a place
Did you mean doing something like this?
void * p= (void*) 0x445021;
You can do that only if your platform has the proper alignment, i.e., it allows pointers to have addresses that are not divisible by 4, 8 etc. Some CPUs will raise an exception if you use an odd address, others will accept it. Simply try it and see how your machine behaves.
As for what Amahdy said:
the << manipulator of cout is overloaded so it recognizes char* as a string and prints the string itself. If you want cout to display the address instead, cast the pointer to void* first:
cout<<(void*) test;
And with respect to delete vs delete[]: you can't mix them or use one instead of the other. Doing so would cause undefined behavior which means that anything can happen, including what may appear as a memory leak, a crash or even outwardly normal execution.
Danny at 2007-11-11 21:02:39 >

# 9 Re: Pointing at a place
Thanks for the responses guys. I know it is not recommended that I only increment by one byte. I am using this code in order to try and figure it out and see what happens to the memory...this is an experiment of sorts.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
long double a, b, c;
long double *pa = &a;
long double *pb = &b;
long double *pc = &c;
void out (string title)
{
cout<< endl<<title<<endl;
setprecision(12);
cout << "a b c = " << a << " " << b << " " << c <<endl;
cout << "&a &b &c = " << &a << " " << &b << " " << &c <<endl;
cout << "*pa *pb *pc = "<< *pa << " " << *pb << " " << *pc <<endl;
cout << "a b c = " << a << " " << b << " " << c <<endl;
}
int main()
{
a = 2;
b = 3;
c = 4;
out("Test");
&pa = 0x445011;
&pb = 0x445019;
&pc = 0x445021;
out("Test");
system("PAUSE");
return EXIT_SUCCESS;
}
Thanks again
9v1cl0 at 2007-11-11 21:03:43 >

# 10 Re: Pointing at a place
Danny and what do you suggest for deleting a char pointer ?
is it really a bug in VC6.0 (other tested editors haven't same problem in the assembled file ... starting by devC++) or there is another standard method for deleting such pointers ?
I have many other detected bugs for this version of VC , please nobody tell me to move to .net or another IDE because my projects are based on this IDE and it's very time consuming to move .. I prefere to use this time for updating and optimizing my codes ... at least I can't do this now.
another bug I think is when I call the constructor of the parent class .. and there is more ..
9v1cl0:
and your code run without problesm ?
if the "0x445011" adress is protected by another process you will get a run-time error in both cases ...
Amahdy at 2007-11-11 21:04:46 >

# 11 Re: Pointing at a place
no i am getting errors on those three lines as follows
H:\pointer test.cpp non-lvalue in assignment
9v1cl0 at 2007-11-11 21:05:49 >

# 12 Re: Pointing at a place
Oh yea you'r not allowed to change the pointer adress but the value which it point to :
pa = 0x445011;
EDIT:
I mean the pointer has an adress and you don't have permission to change this adress, like any declared normal variable's adress.
But you can change the adress which it point to it, but this won't carry with it the value stored in the previous adress, and will point to maybe a protected memory location and you can't read what this location has ... or you will get some garbage if this location is free .. but in this last case the location won't be protected for your data and any application can take it at anytime ...
Amahdy at 2007-11-11 21:06:47 >

# 13 Re: Pointing at a place
Amahdy: Visual C++ 6.0 is indeed very buggy in this respect (is this the version you meant?). When you have a char * pointer, or in fact any other pointer, there's a simple and consistent rule about which deletion should be used: check how the pointer was allocated. Was it allocated using new or new[]? If it's the former, you must use delete. If it's the latter, use delete[]. Yes, you may need to use delete in some cases. For example:
char * p = new char; //one byte, not an array
delete p;
p = new char [2]; //now an array, and yes, you can use the same p
delete[] p; //correct
Danny at 2007-11-11 21:07:49 >

# 14 Re: Pointing at a place
OF course this code will not compile:
&pa = 0x445011;
&pb = 0x445019;
&pc = 0x445021;
&pa=something;
is like writing:
2=3;
&pa is not an lvalue, i.e., it's not a piece of memory that you can access and assign to. You need to do the following instead:
pa = (double*)0x445011;
Now see what happens when you execute this code. It's likely to cause a segmentation fault on Unix, Windows might be more tolerant.
Danny at 2007-11-11 21:08:50 >

# 15 Re: Pointing at a place
It took me awhile but I think I understand now what you really meant: allocating an object on a predetermined memory address. Yes, you can do that using placement new. http://www.dev-archive.com/dev-archive/LegacyLink/9485
Danny at 2007-11-11 21:09:48 >
