Page 1 of 3

Derefrencing memory through varable

Posted: Sun May 13, 2007 11:18 am
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!

Posted: Sun May 13, 2007 11:35 am
by neon
Never mind, I think I found another way :D

Posted: Sun May 13, 2007 2:35 pm
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.

type pointer to memory address

Posted: Sun May 13, 2007 3:05 pm
by Kevin McGuire

type *ptr = (type*)address;

ptr[0] = type_value;
*ptr ==ptr[0] == 1;
*(ptr+1) == ptr[1] == 1;

Posted: Sun May 13, 2007 3:50 pm
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.

Posted: Sun May 13, 2007 4:16 pm
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.

Posted: Sun May 13, 2007 6:23 pm
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.

Posted: Mon May 14, 2007 1:39 pm
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!?

Posted: Mon May 14, 2007 5:54 pm
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*

Posted: Mon May 14, 2007 6:16 pm
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.

Posted: Mon May 14, 2007 6:50 pm
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)

Posted: Tue May 15, 2007 3:40 am
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).

Posted: Tue May 15, 2007 4:46 am
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.

Posted: Tue May 15, 2007 5:33 am
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.)

Posted: Tue May 15, 2007 10:32 am
by Combuster
And in what percentage of everyday code would you expect people to use intptr_t? :wink: