Derefrencing memory through varable

Programming, for all ages and all languages.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Derefrencing memory through varable

Post by neon »

Hey everyone,

I (thought) this would be a simple one...

Im trying to access memory directly through a varable.
For example:

Code: Select all

int addr = pState->esp;
How can I derefrence addr (as if it was a pointer)?
(ie, get a pointer to the memory location stored in addr)?

I have tried:

Code: Select all

*(int**)&addr
*(int**)addr
(int*)addr
*(int*)addr
None work.

I am creating a stack trace for my RSOD. ESP is stored in pState->esp.
I need to derefrence this location to get stack data.

Here is the full source:

Code: Select all

		Sys_puts ("Stack trace\n");
		for (index=0; index<4; index++) {
			int addr = pState->esp;

			Sys_printf ("%x %x\n", (int**)&addr, (int**)&addr+1);

			addr+=2;
		}
Any help is appreciated.

Thanks!
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

Never mind, I think I found another way :D
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

For future reference and anyone else who thinks you should write an operating system before actually knowing the language... you don't have to reference it "like it is a pointer", just use a pointer.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

type pointer to memory address

Post by Kevin McGuire »


type *ptr = (type*)address;

ptr[0] = type_value;
*ptr ==ptr[0] == 1;
*(ptr+1) == ptr[1] == 1;
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

For future reference and anyone else who thinks you should write an operating system before actually knowing the language... you don't have to reference it "like it is a pointer", just use a pointer.
I know C and C++ (Including STL, some Win32, DX, and OGL).
So, yes--I know the language, and have been using it for nearly 4
to 5 yrs writing both low level and high level programs.

(Theres a reason I posted that I found it, you know :) )

Thanks for the reply though.
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

neon wrote:
For future reference and anyone else who thinks you should write an operating system before actually knowing the language... you don't have to reference it "like it is a pointer", just use a pointer.
I know C and C++ (Including STL, some Win32, DX, and OGL).
So, yes--I know the language, and have been using it for nearly 4
to 5 yrs writing both low level and high level programs.

(Theres a reason I posted that I found it, you know :) )

Thanks for the reply though.
Sorry, i didn't mean to imply you didn't know the language... it was meant to be more for people who come check and can't work it out because you hadn't posted your how. Bur good glad to see you have posted it now.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

No worries :) -- I understand where you are comming from.

For anyone else, here is the full source:

Code: Select all

Sys_puts ("Stack Trace:\n");
int* esp = (int*) pState->esp;
for (y=0; y<4; y++) {
	for (index=0; index<6; index++, esp-=1) {
		Sys_printInt (*esp, 16);
		Sys_putch (' ');
	}
	Sys_putch ('\n');
}
I might replace the output with my printf() routine, but it works.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

I know C and C++ (Including STL, some Win32, DX, and OGL).
So, yes--I know the language, and have been using it for nearly 4
to 5 yrs writing both low level and high level programs.

(Theres a reason I posted that I found it, you know Smile )

Thanks for the reply though.
I hope he has enough sense to get inside when it rains, because he sure did not know what he was doing (in the first post) and somehow knows the language.. and most likely makes a larger paycheck than me (if he does make one).

>> Oooh Oooh You told me I did not know the language!!!
>> I know AKD, JSD, IWO2, MXN, DJS, XCX, SSS, @@E, and WW@!
>> I have been using these languages and libraries for two-thousands years!
>> I am so good the code jumps out of the screen and runs in circles!
>> The only problem I have is how to turn on my computer!?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

@Kevin McGuire:

There is absolutley no reason for you to post your message.

I (And I am sure many others) have made little mistakes in the past,
*Including You*. And yes, I know the language very well. If I didnt,
I wouldnt know the first thing about programming, now would I?

It sounds to me you are attempting to attack me for no absolute
reason. And to rephrase what I have said earlier (as apparently
you missed it):
I know C and C++ (Including STL, some Win32, DX, and OGL).
So, yes--I know the language, and have been using it for nearly 4
to 5 yrs writing both low level and high level programs.
Note the word *Some*. I personally dont think its possible to fully
learn Win32 API/DX/OGL; however one could work with them.

*hint*
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

And yes, I know the language very well.
Then why were you asking about pointers you big freaking dummy from another world...

Ok. Watch this video. Are you sitting behind the desk, in front of it, or somewhere in the room and really confused how this applies to you?
http://www.youtube.com/watch?v=teMlv3ripSM

I think what has happened is you have learned STL very well with all the garbage collection and never completely learned a powerful and very important aspect of the language I feel. Now it sets in that you have to wrap you're head around a concept that you should have aquired from the beginning when we talk about the C Language, and knowing the C Language well.

Pointers go right to the root of the language, and translate beautifully to machine code. They are essential to programming in C, and are really quite simple and clearly defined.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I do know pointers very well (Hence the reason I fixed it)

I am to admit, however, that I tend to try avoiding pointers
when possible. The primary reason is that, in my game dev
and software exp, There are *alot* of problems with pointers from
a high level perspecitive. C++ refrences are, imo, alot better.

Alas, to take another quote I have previously said:
I have tried:
Code:
*(int**)&addr
*(int**)addr
(int*)addr << Correct version
*(int*)addr
None work. <<
The problem wasnt the pointer, but how I was using it. All of
the above is synthatically correct, but output different data.

(I do have to admit, I have been using C++ alot more then C)
User avatar
crackers
Member
Member
Posts: 27
Joined: Wed Nov 15, 2006 6:31 am

Post by crackers »

neon wrote: The problem wasnt the pointer, but how I was using it. All of
the above is synthatically correct, but output different data.
Nobody said that it's synthatically incorect, but I think your argument is a little bit missed. You could also said that
1 + 2
and
12 - 9 + 13 * 4
are correct from mathematical point view, it's just that the result is different :wink:
What Kevin McGuire mean that it's just a little bit strange that someone that is using language for 5 years don't know it's basics (which pointers definitively are).
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Well, switching between pointers and their numerical representation is hardly standard C programming, even though pointers are commonplace (classname * variablename;) It has mainly to do with how a processor works rather than how pointers work, which is just that one step further. Hence i find the argument completely valid. I once had the same problem with pointers when their values started to mean more than just some random number.

Converting between pointers and addresses isn't on the list of vaxocentrism because of nothing.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Combuster wrote:Well, switching between pointers and their numerical representation is hardly standard C programming...
Since 1999:
intptr_t - The type specifies a signed integer type large enough to support interconversion with a void pointer. (You can convert a void pointer to intptr_t and back, and the result compares equal to the original pointer value.)
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

And in what percentage of everyday code would you expect people to use intptr_t? :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply