Archived

This topic is now archived and is closed to further replies.

tonymontana

QUESTÝON ABOUT POÝNTERS

Recommended Posts

// POÝNTERS.............. #include <iostream> using namespace std; int main() { int anyinteger=1; // just an ordinary integer int* ptrtoint=&anyinteger; // just an ordinary pointertointeger that keeps the RAM adress of anyinteger /* ok know we have an pointer (to integer values) that has a value.this value is the adress of anyinteger on the RAM.But also this pointer has an adres on the RAM too -we can see adress of anyinteger (the value that is kept in ptrtoint) as that cout<

Share this post


Link to post
Share on other sites

#include <iostream>
using namespace std;
int main()
{
int anyinteger=1; // just an ordinary integer

int* ptrtoint=&anyinteger; // just an ordinary pointertointeger that keeps the RAM adress of anyinteger

/*
ok know we have an pointer (to integer values) that has a value.this value is the
adress of anyinteger on the RAM.But also this pointer has an adres on the RAM too
-we can see adress of anyinteger (the value that is kept in ptrtoint) as that
cout<<ptrint;
-we can also see the adress of ptrtoint(the own adres of ptrtoint on RAM) as that
cout<<&ptrint;
-what we can do amazing is we can get/change the value of anyinteger via ptrtoint
by the help of content operator(that is maybe not named as content op)
cout<<*ptrint;
// (i am assuming)this takes the value of ptrint( adress of anyinteger) and acts to this value
as an adress and then turns the value of this adress.
what is amazing is mybook tells me that when we declera an variable (int ,char,int*,double,int** no matter what it is)
that has a value and an adress on RAM. (nothing more. only a value and an adress)
ok i assume that is true( it must be true..)
then *(content operator) is an independent operator and i should use it that way(NOT WORKÝNG!!!)

int var1=1;
cout<<*var1 //that must return the first byte''s value on the RAM.but it doesn''t..
the content operator wolud take the value of var1 as and adress and then would return
this adress''s value which is the fist byte''s value on the RAM

if you say var1 is an integer not a pointer
(which is i exactly know ofcourse.an i also know that is a mistake to make cout<<*var1)
then i demand how this f**kin content operator is working...//this was the question 1
*/

// look what we have done

cout<<"cout<<anyinteger THAT MUST SHOW 1 "<<anyinteger<<endl;
cout<<"cout<<ptrtoint THAT MUST SHOW adres of any integer "<<ptrtoint<<endl;
int** ptrto_ptrtoint=&ptrtoint //this pointertopointer takes the adress of ptrtoint which is already an pointer

// well seccond question is ...(i will try to take the first byte of the RAM''S VALUE again with constants)

*ptrto_ptrtoint= 00000000; // *(ptrto_ptrtoint) is the adress of ptrtoint

// i tried to assign the constant 00000000(hexadecimal) to the *(ptrto_ptrtoint)

cout<<**(ptrto_ptrtoint)<<endl;
/* this shoul turn the value of first byte''s value to because *(ptrto_ptrtoint) is the adress
00000000 and **(ptrto_ptrtoint) is 00000000''s value..actually not working
second question is how can assign a hexadecimal value( a constant adress) to a pointer &
where i am making mistake the things that i told you...
*/

return 0;
}


Press edit to see my source tags

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
quote:
Original post by tonymontana
what is amazing is mybook tells me that when we declera an variable (int ,char,int*,double,int** no matter what it is)
that has a value and an adress on RAM. (nothing more. only a value and an adress)
ok i assume that is true( it must be true..)
then *(content operator) is an independent operator and i should use it that way(NOT WORKÝNG!!!)

int var1=1;
cout<<*var1 //that must return the first byte''s value on the RAM.but it doesn''t..


That part of the RAM doesn''t belong to your program, so you can''t see it. It''s possible that there is actually no memory there; the operating system does a mapping between memory addresses that the program sees and real "physical" memory addresses.

Also, when you try to use an integer as a pointer like this, the language doesn''t know what kind of pointer you want. So it doesn''t know how much data to grab from that location (a char * would look for 1 byte, a double * for 8, for example).

Also, even if you could access the "first byte of RAM" - what are you expecting to be there? Don''t worry about it. As a rule you should only use the & (address-of) operator once, and only because you need to provide a reference to a function (i.e. the function has an "output parameter"). You probably shouldn''t ever need it in C++ since you can specify that your parameter is passed by reference.

Share this post


Link to post
Share on other sites
Although you're roughly correct with some of your ideas, the main problem is that the OS will not allow you to access the vast majority of memory on a computer. For stability, security, and virtual memory reason. And you definitely cannot access memory at location 0, since that is reserved to indicate that a pointer doesn't point to valid memory. The only memory you are allowed to access usually is memory owned by your own process. So these operations usually work (but certainly aren't recommended, unless you really know what you're doing, or just experimenting):


//Reading random values from process's heap

int* pInt;
int* pIntBackup;
pInt = new int;
pIntBackup = pInt;
cout << *pInt << endl;
pInt += 1;
cout << *pInt << endl;
pInt += 1;
cout << *pInt << endl;
pInt += 1;
cout << *pInt << endl;

delete pIntBackup;

//reading random values from process's stack

int a;
pInt = &a;
cout << *pInt << endl;
pInt += 1;
cout << *pInt << endl;
pInt += 1;
cout << *pInt << endl;
pInt += 1;
cout << *pInt << endl;


So since you start off in both cases with a valid address, then addresses that are close to that original address are almost always going to be within the virtual memory space that you're allowed to access. But except by going through special steps with the OS, you can't simply read values from memory anywhere.

[Arg! My first time getting beaten to the post 'cause I take too long to type. Poop on you, Zahlman. Knew it would happen eventually...]

[Wow, looking at the times, I really take a long time to post. I had to test my code, though...]

[edited by - Agony on January 23, 2004 10:11:33 AM]

[edited by - Agony on January 23, 2004 10:12:40 AM]

Share this post


Link to post
Share on other sites

#include <iostream>using namespace std;int main(){ int anyinteger=1; // just an ordinary integer int* ptrtoint=&anyinteger; // just an ordinary pointertointeger that keeps the RAM adress of anyinteger /* ok know we have an pointer (to integer values) that has a value.this value is the adress of anyinteger on the RAM.But also this pointer has an adres on the RAM too -we can see adress of anyinteger (the value that is kept in ptrtoint) as that cout<<ptrint; -we can also see the adress of ptrtoint(the own adres of ptrtoint on RAM) as that cout<<&ptrint; -what we can do amazing is we can get/change the value of anyinteger via ptrtoint by the help of content operator(that is maybe not named as content op) cout<<*ptrint; // (i am assuming)this takes the value of ptrint( adress of anyinteger) and acts to this value as an adress and then turns the value of this adress. what is amazing is mybook tells me that when we declera an variable (int ,char,int*,double,int** no matter what it is) that has a value and an adress on RAM. (nothing more. only a value and an adress) ok i assume that is true( it must be true..) then *(content operator) is an independent operator and i should use it that way(NOT WORKİNG!!!) int var1=1; cout<<*var1 //that must return the first byte''s value on the RAM.but it doesn''t.. the content operator wolud take the value of var1 as and adress and then would return this adress''s value which is the fist byte''s value on the RAM if you say var1 is an integer not a pointer (which is i exactly know ofcourse.an i also know that is a mistake to make cout<<*var1) then i demand how this f**kin content operator is working...//this was the question 1 */ // look what we have done cout<<"cout<<anyinteger THAT MUST SHOW 1 "<<anyinteger<<endl; cout<<"cout<<ptrtoint THAT MUST SHOW adres of any integer "<<ptrtoint<<endl; int** ptrto_ptrtoint=&ptrtoint //this pointertopointer takes the adress of ptrtoint which is already an pointer // well seccond question is ...(i will try to take the first byte of the RAM''S VALUE again with constants) *ptrto_ptrtoint= 00000000; // *(ptrto_ptrtoint) is the adress of ptrtoint // i tried to assign the constant 00000000(hexadecimal) to the *(ptrto_ptrtoint) cout<<**(ptrto_ptrtoint)<<endl; /* this shoul turn the value of first byte''s value to because *(ptrto_ptrtoint) is the adress 00000000 and **(ptrto_ptrtoint) is 00000000''s value..actually not working second question is how can assign a hexadecimal value( a constant adress) to a pointer & where i am making mistake the things that i told you... */ return 0;




Share this post


Link to post
Share on other sites