Click here to Skip to main content
15,072,311 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have a question about pointers,
can we give address directly to a pointer?
like this code:
int *p2;
p2=0x22fefc;
Posted
Comments
Mohibur Rashid 18-Jul-11 10:25am
   
If you want to share data between process then this is not the way at all.
Try ICP, client/server solutions

You can do it:

C++
int * p2 = reinterpret_cast<int*>(0x22fefc);

//C-style cast:
int * p1 = (int*)0x22fefc;

//easy to check-up: p1==p2, but de-referencing them could be a problem :-)


The result depends on the platform. On most modern platforms is would looks like some random address; and you will very likely get General Protection Fault exception. In some platforms it may work if the address makes sense. For example, with MS-DOS you could access video card this way or some other physical memory buffer. (However, in real-mode of Intel CPU the address would be the segment:offset pair, so this code won't work anyway.)

—SA
   
v2
Comments
Аslam Iqbal 18-Jul-11 5:28am
   
good ans. my 5
Sergey Alexandrovich Kryukov 18-Jul-11 10:36am
   
Thank you, Aslam.
--SA
Espen Harlinn 18-Jul-11 8:33am
   
Nice and simple, my 5
Sergey Alexandrovich Kryukov 18-Jul-11 10:36am
   
Thank you, Espen.
Updated with C-style cast.
--SA
Espen Harlinn 18-Jul-11 10:42am
   
Have you taken a look at this article:
http://www.codeproject.com/KB/cross-platform/BenchmarkCppVsDotNet.aspx
I think you'll like it :)
Sergey Alexandrovich Kryukov 18-Jul-11 11:32am
   
Yes, pretty interesting info; thank you for sharing.
--SA
Yes. But, there is a very good chance of a complete failure when you try to use it - it is unlikely that a random address will be within your programs memory space, and the OS will spot the access attempt and kill your app.
   
Comments
   
You answer is correct for most modern systems, but in fact this is platform-dependent. My 4. Please see my answer.
--SA
hakz.code 18-Jul-11 8:25am
   
This is what I too think,by the way I dont find a point in accessing direct memory unless we are programming on smaller platforms like 8085 microprocessor.
Sergey Alexandrovich Kryukov 18-Jul-11 10:32am
   
That's correct, direct memory is typical for smaller platforms.
--SA
yes, that is very much a valid way of assigning an address to a pointer in C.
When writing to the address you'd have to ensure that that your memory is set up
accordingly though... e.g. though a linker directive file.

I would rather do it as follows though:

#define SOME_ADDRESS (0x22fefc)
int *p2 = (int*)SOME_ADDRESS;


Below is an example of a linker directive file for a power pc processor:

MSIL
-sec
{
    .PPC.EMB.sdata0 0xffff8000  ABS :
    .PPC.EMB.sbss0          CLEAR ABS :

    .text 0x10000 :
    .syscall :
    .secinfo :
    .rodata :
    .sdata2 :
    .fixaddr :
    .fixtype :
    .ROM.data ROM(.data) :
    .ROM.sdata ROM(.sdata) :
    .sdabase align(8) :
    .sdata :
    .sbss :
    .data :
    .bss :
    .heap align(16) pad(0x100000) :
    .stack align(16) pad(0x80000) :

    SOME_ADDRESS 0x0022fefc MIN_SIZE(0x4) :
}
   
v6
Comments
R. Erasmus 18-Jul-11 4:34am
   
Not true, this will work in C, I don't know about C++.
R. Erasmus 18-Jul-11 4:42am
   
For the noob that voted me a 1... Think its time to maybe look into another field of expertise as this one is clearly not working for you.
hakz.code 18-Jul-11 8:18am
   
Hi your code flagged a compiler error for me,I think the typecast should be to int* not to int.
R. Erasmus 19-Jul-11 3:19am
   
Indeed. Well spotted, thx. ;-)
hakz.code 19-Jul-11 6:11am
   
welcome :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900