Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / C++ Pointer Question

Author
Message
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 13th Sep 2007 03:40
I have a tricky routine I wrote in freePascal that I'm having a problem porting. It's not about what I want to do - its about the syntax naturally - and I'm stumped.

I NEED to Force a Value into a pointer because I KNOW the memory address I need. I have an unsigned int that I want to put it's VALUE INSIDE a VOID pointer so that VOID pointer NOW has the # I put there as the address it points to.

Can anyone give me a one liner source snippet for this?

Psuedo code example


I DO NOT want lp to get the address OF uiMemoryAddressOfSomething, I WANT to force the ADDRESS I want lp to point to in the actual memory used by the pointer to point at stuff.


Why? I have a generic routine I PASS the OFFSET of a class member to, it then adds this OFFSET to the ACTUAL memory address the class resides at. Then by forcing this value into the VOID (or any other pointer for that matter) I can now access this data locates at:

CLASS_MEMORY_ADDRESS + OFFSET_OF_CLASSMEMBERVARIABLE

Thanx in advance.

unitech
17
Years of Service
User Offline
Joined: 27th Jun 2007
Location:
Posted: 13th Sep 2007 17:16 Edited at: 13th Sep 2007 17:19
Hmm, normally you set a point to that address dynamically..


myPTR *item;

then
myAddress = myPTR;
or

myAddress = &myPTR;

Now if I get you correctly your just trying to select an address manually...??


like

myAddress = 0x03400; ?

this is not good. That is if this address was selected randomly. You should only use an address that has been allocated first. then use to the address of the object that allocated it..

void *lp;
//allocation.
int var; //the address to var is done at run time and will never be the same..
lp= &var;
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 13th Sep 2007 20:00
@UNITECH - THANX - I think you pointed out something! That dealing with the pointer directly IS the address and the * is only when dereferencing. Am I correct? I'll play tonight. this means I over complicated it. Just

myAddress=lpPtr;

Should do it! (Simplicity! At Last!)

(No I'm not arbitrarily coming up with addresses.)

I have a home grown double linked list I Ported over from FreePascal I wrote for its pure simplicity (Well... let me qualify) Its not always super simple but its the leanest I've seen compared to the FreePascal "Object Lib" and the C StdLib "Iterator" stuff.

Basically there is a base class for Items...shorthand below:

and the actual "Controller" (Actually has base class of JFC_ITEM to allow multiple dimensions)


Now there are "low level" functions I'm working on that work in FreePascal that make this thing really cool and makes this useful for DarkGDK. It allows you to build anything you want with the Item class JFC_ITEM as the base. Say a class of bad guys in a game. The low level search uses the pointer address of said "Bag guy" class in memory and then Adds the Offset of the member of that class .. say "BadGuy.iBadGuyHealth" and then does a binary search for the value you are looking for using the calculated POINTER+OFFSET to the data, and the data length. This allows you to sort of "Query" the double linked list for things based on exact values and ranges and step through them like a database recordset.

Its this lpMyPointer=lpBaseAddress + uiOffset; that spaked this thread.

thanx for your help - I'll try the simple syntax (I can't believe I missed that) In FreePascal you have to type cast a pointer to a endian sized unsigned - like C++ unsigned INT... kinda like:

myAddress=@lpMyPointer; <-- ADDRESS OF POINTER
myPointerPointsTo=CARDINAL(lpMyPointer);

From this syntax - I'm trying to do: MyNewMemoryAddress=myPointerPointsTo + MyOffset;

MyNewMemoryAddress=The area in RAM I want to scan during the search - if it matches - great, if not DL->MoveNext() and try again.

It is blazing fast in FReePascal and SO FAR is screaming in C++ as well.

unitech
17
Years of Service
User Offline
Joined: 27th Jun 2007
Location:
Posted: 13th Sep 2007 20:14 Edited at: 13th Sep 2007 20:17
Ok, cool give that a go if

myAddress=lpPtr;

fails try..

myAddress=&lpPtr;// <-- ADDRESS OF POINTER

one or the other should work for you. If not ill take a closer look at what you have up here.
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 13th Sep 2007 22:11
Yeah - I will. I think the first will do what I want. I'm pretty sure the &lpPtr is the same as FreePascal's @lpPtr in that it gives you the ADDRESS of the POINTER not the ADDRESS STORED IN the pointer.

Thanx though bro - I know this is the silverbullet I need - I can't wait to be on my dev box.

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 14th Sep 2007 04:23
This Worked - though I feel its a fudge. I'd like to have it in one line not piece meal. But it does work - the binary search does find the correct record so I think its working. what are the chances of memcmp() returning ZERO unless it's dead on?!?



Normally I don't name unsigned integers lpSomething - but I wanted to remind myself it still was a pointer - just being chopped up - because regular pointer math adds the data type size. so lpPtr++; (If that lpPtr was to a long (32bit lets say) then lpPtr++ increments lpPtr internal address up 4 bytes - not one! (Cool for many things but not my deal.

I get absolute Address then need to add number of bytes to this address because I know thats where something is I want.

unitech
17
Years of Service
User Offline
Joined: 27th Jun 2007
Location:
Posted: 14th Sep 2007 15:54 Edited at: 14th Sep 2007 15:55
Ah very nice Jason, I see what your after now. Thats some crazy casting, but hey if it works.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Sep 2007 19:38
@jason,

If you want to take an address, add an offset and then put it into a void pointer, why not just do that?


... Cast lpItem to a 'pointer to unsigned int', retrieve the value at this address, add the offset to the integer value, then cast the result to a 'pointer to char'.

Wrap it in an inline function to avoid repeating the code and any function call overhead.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 14th Sep 2007 22:30
@Unitech - Yeah - it is - but what I'm trying to accomplish is blazing fast for the double linked list stuff I'm doing. In this case its all about "searching" "stuff" in the linked list. Whether its just a list of "numbers" or an "array"(sorta) of classes - and I want to search for a specific value in a particular "variable" in the class. Pretty nifty. In FreePascal this class is the core to like literally megabytes of my source code - performs fast and well - and I even made a multi-threaded web server with it that is faster than Lighttp, apache, IIS - (Even does CGI, Perl, PHP - but only CGI and a homegrown system are native - shells out for the other things like PHP - but that's cuz I didn't care really - more concerned with my basic HTTP stuff and being able to literally write truly compiled web applications - that compile and run on both microsoft and linux... did it too!)


@IanM - BRO!!!!! THANX! That's exactly what I'm looking for - its the semantics because I'm not a c++ guru! THANK YOU! As for the inline thing - this is already in a binary search function - but as soon as I need the same thing in a few more places - I'll chop it into an inline function like you recommended. I really do try to shave off clock cycles where I can - I don't get totally crazy but if its something "deep" or something that gets called a lot (which these classes and routines I'm working on will) I try to be very particular.

Thanks you guys! Going into the weekend with NEW ammo!

jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 15th Sep 2007 01:10
@IanM - IT WORKED! Thanx Man. Boy that's UGLY syntax but it's compiling tight to what I wanted to do... In Assembly that is actually very easy - because everything is an unsigned int and a pointer at the same time - unless referencing words and bytes which isn't processor native (as they say) so it requires more cycles per command for that kinda thing... but anyway - THANX!!!!!



IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 15th Sep 2007 22:35
Glad to help

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code

Login to post a reply

Server time is: 2024-11-16 20:43:37
Your offset time is: 2024-11-16 20:43:37