Dereferencing Confusion

The below is an array of pointers, and a pointer to the array of pointers. Currently I'm getting segfault. The pointers point to members of another array.

p.s. I know this is strange

1
2
3
4
5
6
7
8
9
10
classname **array
classname *parray[ARRAYSIZE];
classname permanent{ARRAYSIZE];
array = parray;

foo()
{
  *array[some NULL location within bounds] = 
    permanent[a nonnull location]; 
}


Again, i'm trying to get the pointer in the array of pointers to point TO the location in the "permanent" array.

and again... please understand i'm experimenting here.
*array[some NULL location within bounds]
This will not work because you are dereferencing a null pointer which is not allowed.

What you want, even if strange, should be possible:

parray[someIndex] = &permanent[theSameIndexMaybe?];

Or am I misunderstanding?
@Peter
What do you mean? All of my array indices must be pointing to something? If so, that's rather inconvenient for my program and I'll have to figure out something new. Or do you just mean I'm doing it wrong. If so; I get that much lol! I'm looking for advice on how to do it correctly :)

@Web
Well, the thing is I have different pointers pointing to similar arrays-of-pointers at different times. So your code assumes direct access to the array. I'd rather avoid the ugly hardcoding required to do that (in my case by detecting which array is the "active" array of pointers)

Feel free to post in response to this. I'll try to get around to fixing the segfault tomorrow and post results or more questions. However, if you understand the problem better now, more solutions/help can't hurt :D


I may try the reference code you posted (with "array" in place of "parray") as between the two comments that seems to be the implied solution.

That said, it won't be the same index.

(Long explanation below)
Essentially I'm mimicking list behavior to test speed vs. storage. There's more to it:
-to avoid copying information I'm using pointers
-to avoid allocating I'm using large permanent arrays of objects/pointers to objects
-to avoid walking over dead nodes very often I "optimize" the array by pointing "dead" positions to "alive" positions occasionally (utilizing a third array) and keeping a pointer to the newly optimized "end point" of the array.

If none of that makes sense; I can link the bitbucket if I get it working (as it explains it all long hand and is well commented.)

I'm basically building a bunch of data structures around the idea of managing *very* large amounts of objects that get frequently created/destroyed (100's per second being the first round of tests) and running them against each other (this being a custom one.) If all goes well then the arrays will sit in cache all day, never allocating or deallocating, with all of the positive properties of a list (except for efficient storage.)

Thanks thus far!

p.s. maybe to clarify how the array is initialized:
1
2
3
4
psuedocode:
for each index i in ARRAYSIZE
parray[i] = NULL
array = parray


It's not truly a null location, but a location int he array that points to NULL. It shouldn't be a NULL pointer that I'm dereferencing (I think) as it's a pointer to an array of pointers; by dereferencing I was hoping to access the indexed pointer and have THAT point to the permanent location.
Last edited on
Because you do this array = parray;, array[i][j] will always give the same element as parray[i][j] so which one we talk about is irrelevant.

This parray[i] = NULL; makes parray[i] a null pointer (it doesn't point to a valid object). Trying to dereference the null pointer *parray[i] is not allowed.

I think webJose is close to what you are trying to do.
parray[i] = &permanent[j];
This will make parray[i] (and array[i], because they are the same) point to the object stored at permanent[j].
Last edited on
Oh boy... In my head I had acknowledged that an array of pointers was similar to a pointer to a pointer, but I guess I had figured it wasn't syntactically the same.

So.. now i'm confused. Should I be doing:
1
2
3
psuedocode:
for each index i in ARRAYSIZE
parray[i][j] = NULL


and then
1
2
3
4
5
foo()
{
  array[0][some NULL location within bounds] = 
    &permanent[a nonnull location]; 
}

?


Or should I just be adding the reference as in
parray[i] = &permanent[j]

I'll try the reference first as that made sense until I thought about it too much. Maybe I'm just not sure why you mentioned array[i][j] vs. parray[i][j] as an example vs. just array[i] vs. parray[i]


Thanks again
Last edited on
Perhaps you had intended to do (*parray)[index] ? (wild guess)
The solution WebJose posted seems to have worked;
I did check your solution L B, but it still segfaulted; I think that had crossed my mind at some point.
Topic archived. No new replies allowed.