int *p=(int *) 1000
Well I kinda understood you question here buddy. So lemme put my say as follow.
Question can be rephrased as
1> How do you ask a pointer to point to a specific memory address that you will provide it from your side and NOT system generated (Assuming your given address is valid address).
2> Now once you have your pointer pointed to that address, How do you put the content on that address.
Explanation is here :-
You are right, the way to do it is
int *p=(int *) )0X1000 ; (Assuming Ox1000 is valid System Address)
Now your pointer p is pointing to address location 0x1000 .
In this case if you do
cout<<p<<endl;
//I WILL GUARANTEE YOU YOUR PROGRAM NEVER CRASH and output will be the memory address that we assigned and that is 0X1000 in our case.
However here is the real question now .
Like system generated pointer variable ..
int *myPointer = new int();
* myPointer = 55 ; // This is how we set the content to the pointer .
but how do we set it in our case ?
Lets see how many people can answer this clearly enough .
I think you meant that you guarantee that it won't crash if they do cout<< p <<endl. If the pointer is dereferenced (as in cout<< *p <<endl), it might well crash.
To set the value, you'd set it in the same way you set any other pointer, e.g. *myPtr = 55. The only problem is that depending on what memory address you're pointing to, it might not be legal to write that address, in which case you'd get a segfault. That has nothing to do with the fact that the pointer was cast from int, and everything to do with the address in the pointer.
@Eugene :- Right Man!! I indeed it was meant for that but got a typo, Anyways quiet interesting that you caught that. After a long time Hope you are doing good.
Now the second part of which I haven't completed my above comment I guess you made it clear enough. Returned memory address is actually a valid address for reading and not writable ( until you are in jurisdiction of that memory being addressed ). Which means ,
int *myPointer = new int();
* myPointer = 55 ;
Always Passes &
int *myPointer = (int *)0x12345;
* myPointer = 55 ;
Fails !!!
When you do int* myPointer = new int(); you always get back a pointer to valid, writeable memory assuming the allocation itself doesn't fail. If you just cast some arbitrary address to a pointer, the memory address could be not allocated to the process or not writeable.
Another thing that you have to consider with an address like 0x12345 is that it's not aligned to a 4-byte boundary. On some architectures integer writes must be aligned to 4-byte boundaries, so this could be another reason it fails. When you do new int(), the returned memory is always correctly aligned.
AOL support +1-800-893-9752
When AOL user is unable to get the email or unable to log-in to the AOL account, then it is best to get the quick help by dialup toll-free # +1-800-893-9752
contact aol member services, aol support phone number usa, aol email support phone number, aol email tech support number, aol email customer service number, aol email customer support, aol customer support telephone number, aol technical support contact number, aol com customer service phone number, download aol mail to computer, Aol email support phone number, aol gold desktop download, aol mail tech support number, aol mail hotline number, aol mail toll free number, contact aol mail support number, aol customer service number, aol support phone number, aol support number, aol mail repair
The memory location numbered 1000 is likely not writeable, which would explain the segfault.
- eugene.yarovoi May 10, 2014