Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, exile, Ayumi), 1,085 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
pointer jumping #422416
05/09/13 19:48
05/09/13 19:48
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline OP
Senior Expert
HeelX  Offline OP
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
I did some pointer jumping for the first time today and it is quiet interesting. If you don't have an array in e.g. a structure, but consecutive variables, you can just take a pointer and add some bytes to it to jump there, like in these functions:

Code:
BMAP** avViewRtRef (VIEW* v, int index)
{
	if (v == NULL || index < 0 || index >= 4)
		return NULL;
		
	if (index == 0)
		return &(v->bmap);
	else
		return &(v->target1) + (index - 1) * sizeof(BMAP*);
}

BMAP** avMtlSkinRef (MATERIAL* m, int index)
{
	if (m == NULL || index < 0 || index >= 4)
		return NULL;
		
	return &(m->skin1) + index * sizeof(BMAP*);
}



I wanted to have two functions that give me the pointer to the bitmap-pointers of the view render targets (bmap, target1, target2, target3) and the material skins (skin1, skin2, skin3, skin4). In the case of the material, all skins are after another (see struct material in atypes.h), but for views, target1...target3 are not directly in memory after bmap, therefore the if.

Essentially, you just take a pointer adress and add the size of the pointer times the fields you want to jump and voila, you have it.

The (non-efficient) alternative to this would be a switch...case, like this

Code:
BMAP** r = NULL;
	
switch (index)
{
	case 0: r = &(v->bmap); break;
	case 1: r = &(v->target1); break;
	case 2: r = &(v->target2); break;
	case 3: r = &(v->target3); break;
}		

return r;



Hm, I don't know if this is helpful but I feel motivated to share things since that good-bye post of Sid wink

Last edited by HeelX; 05/09/13 19:48.
Re: pointer jumping [Re: HeelX] #422464
05/10/13 14:41
05/10/13 14:41
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
v->target1 + offset, no?

I don't know about the Lite-C compiler, but pointer arithmetic (not jumping ;)) doesn't work by adding bytes, unless you used bytes to begin with.

Code:
int *bar = xyz;
*(bar + 0) = 1024; // First element of the "Array"
*(bar + 1) = 512; // Second element
*(bar + 2) = 32; // Third element



Since the Lite-C compiler packs everything closely anyways (and a pointer is word aligned), target1, target2 and target3 are in fact consecutively laid out in memory, so adding 1 to target1 will result in a pointer pointing to target2.

Sooo...
Code:
if(offset == 0)
    return v->bmap;
return v->target1 + offset - 1;



Speaking of, you can use return inside a case label wink

Quote:
Hm, I don't know if this is helpful but I feel motivated to share things since that good-bye post of Sid wink

I hope I didn't stop you. I'm off again now anyways, I just clicked here out of old habit...


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1