Jump to content

Storing Pointers to Objects in Containers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
LukeyJ

LukeyJ

    Learning Programmer

  • Members
  • PipPipPip
  • 93 posts
Hi,

I had an idea that in order to keep track of dynamically created objects, it was useful to store their pointers in a container. While, I don't understand containers yet, it is my next point of learning. I understand vectors would be useful for creating objects. But if I decided that my program can delete objects at any time from any position, would using a vector be a wise choice? As this would entail either setting the value to NULL or deleting the item, and I was under the impression you can't delete from the middle of a vector.

Do you have any experience doing this or a suggestion against this method?

Thanks in advance,

(Another stupid question from) LukeyJ

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

LukeyJ said:

I had an idea that in order to keep track of dynamically created objects, it was useful to store their pointers in a container. While, I don't understand containers yet, it is my next point of learning. I understand vectors would be useful for creating objects.
Not my arena either, but I'll take a stab.

LukeyJ said:

But if I decided that my program can delete objects at any time from any position, would using a vector be a wise choice?
I don't think it's the best choice.

LukeyJ said:

As this would entail either setting the value to NULL or deleting the item, and I was under the impression you can't delete from the middle of a vector.
I believe you can, it may just be that the frequency of doing so may call into question the efficiency of the chosen container.

This thread is somewhat related.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It will depend on how you need to access the pointers, and the type of work you'll be doing. A linked list of pointers, a set<*T>, or various other containers all come to mind, but they also have different overheads for getting access to the pointer you want.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
I made a simple solution for this for projectiles in games. I've used this simple code in quite a few games. It's an unordered collection of pointers. There's also an iterator function:
typedef struct { 
	void ** p;
	int len;
	int space;	
} list;

int list_init(list * l, int siz){
	if(l->p = malloc(sizeof(void *) * siz)){
		l->space = siz;
		l->len = 0;
		return 1;
	}
	return 0;
}

int list_add(list * l, void * v){
	if(l->len != l->space){
		l->p[l->len] = v;
		l->len += 1;
		return 1;
	}
	return 0;
}

int list_del(list * l, int n){
	if(n < l->len){
		l->len -= 1;
		l->p[n] = l->p[l->len];
		return 1;
	}
	return 0;
}

int list_kill(list * l){
	free(l->p);
	l->space = 0;
	l->len = 0;
}

void list_pass(list * l, void (*h)(list *,void *,int)){
	int i = 0;
	while(i < l->len){
		(*h)(l,l->p[i],i);
		i++;
	}
}
I don't know if that's useful, but it's useful to me. Whatever.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz