Skip to main content
GameDev.net gamedev.net
🔒 Locked

Does VC++ allow triple pointers?

Started by blueshogun96 Aug 23, 2011 at 10:06 AM 9 replies 4.3k views
Original Post
blueshogun96
blueshogun96
Atm, I'm porting a project (libemu: http://libemu.carnivore.it/) from GCC to Visual Studio.net (C/C++) and so far, I'm a bit confused on the best way to handle this scenario. In the GCC port, there's a pointer allocation using a triple pointer (which I didn't think was possible) allocated by using malloc() for use as a double array of pointers. So, the pointer being allocated is of a void*** type which is allocated like this:

void*** em::pagetable = malloc((1 << (32 - PAGE_BITS - PAGESET_BITS)) * sizeof(void *));

And commonly used like so:

em->pagetable[PAGESET(addr)] = (void**)malloc(PAGESET_SIZE * sizeof(void *));

...

em->pagetable[PAGESET(addr)][PAGE(addr)] = malloc(PAGE_SIZE);

I feel kinda dumb for not knowing the answer to this myself, but I'm trying to get this fixed with minimal hassle. Should I try using new[][][] instead? Or use a different variable type? Thanks.
rip-off
rip-off
Try it out...

#include <iostream>

int main()
{
int a = 42;
int *b = &a
int **c = &b
int ***d = &c
int ****e = &d
int *****f = &e
int ******g = &f
int *******h = &g
int ********i = &h
int *********j = &i
int **********k = &j
int ***********l = &k
int ************m = &l
int *************n = &m
int **************o = &n
int ***************p = &o

std::cout << a << ' ' << ***************p << '\n';
}

There is nothing particularly special about a double pointer, or a triple pointer. Higher orders get rarer though. The head of a linked list of size N can be though of N level pointer to the tail.
Antheus
Antheus

Atm, I'm porting a project (libemu: http://libemu.carnivore.it/) from GCC to Visual Studio.net (C/C++) and so far, I'm a bit confused on the best way to handle this scenario. In the GCC port, there's a pointer allocation using a triple pointer (which I didn't think was possible) allocated by using malloc() for use as a double array of pointers. So, the pointer being allocated is of a void*** type which is allocated like this:

void*** em::pagetable = malloc((1 << (32 - PAGE_BITS - PAGESET_BITS)) * sizeof(void *));

And commonly used like so:

em->pagetable[PAGESET(addr)] = (void**)malloc(PAGESET_SIZE * sizeof(void *));

...

em->pagetable[PAGESET(addr)][PAGE(addr)] = malloc(PAGE_SIZE);


Dear god almighty...

I feel kinda dumb for not knowing the answer to this myself, but I'm trying to get this fixed with minimal hassle. Should I try using new[][][] instead? Or use a different variable type? Thanks.
[/quote]
The above does something else.

Try with typedefs:typedef void * FirstPtr;
typedef FirstPtr * SecondPtr;

SecondPtr * em::pagetable = malloc((1 << (32 - PAGE_BITS - PAGESET_BITS)) * sizeof(void *));


Or, it allocates an array of SecondPtr types. Similar to:SecondPtr sp[(1 << (32 - PAGE_BITS - PAGESET_BITS))];

It does not allocate a 3D array. Just a simple array that contains pointers to (void *).

Also, malloc works just fine in visual studio, why change it?
NightCreature83
NightCreature83

Atm, I'm porting a project (libemu: http://libemu.carnivore.it/) from GCC to Visual Studio.net (C/C++) and so far, I'm a bit confused on the best way to handle this scenario. In the GCC port, there's a pointer allocation using a triple pointer (which I didn't think was possible) allocated by using malloc() for use as a double array of pointers. So, the pointer being allocated is of a void*** type which is allocated like this:

void*** em::pagetable = malloc((1 << (32 - PAGE_BITS - PAGESET_BITS)) * sizeof(void *));

And commonly used like so:

em->pagetable[PAGESET(addr)] = (void**)malloc(PAGESET_SIZE * sizeof(void *));

...

em->pagetable[PAGESET(addr)][PAGE(addr)] = malloc(PAGE_SIZE);

I feel kinda dumb for not knowing the answer to this myself, but I'm trying to get this fixed with minimal hassle. Should I try using new[][][] instead? Or use a different variable type? Thanks.


Don't ever mix new[], delete[] with malloc and free that will lead to undefined behaviour. That tripple pointer stuff makes your eyes bleed though, use some typedefs which could clear up stuff, void* are generally a bad practice in C++. It also smells of a thoughtless port of C code to C++.
blueshogun96
blueshogun96
Thanks for the input guys. It looks like I fixed it (so far), at least I hope I did. So it looks like it's time to press on. Thanks again. smile.gif
Oberon_Command
Oberon_Command
I actually put this into VC++ and compiled it. It cmpiled just fine.




int main(int argc, char * argv[])
{
int ****************************************************************************************************************************************************************************************************************************************************************a = NULL;

return 0;
}
sam_hughes
sam_hughes
Except for the problem of too many layers of indirection.
PropheticEdge
PropheticEdge

Except for the problem of too many layers of indirection.


Nah, just abstract it behind a clean API that hides the complexity of the indirection.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.