Skip to main content
gamedev.net
🔒 Locked

pointers and vectors

Started by MadProgrammer Oct 13, 2001 at 2:57 PM 9 replies 300+ views
Original Post
MadProgrammer
MadProgrammer
ok this is what i have: struct CLIENT { SOCKET s; HANDLE h; DWORD id; }; vector clients(0); and, every time a client connects, clients is made 1 bigger and a thread is started that talks to the client. i want to send the thread a CLIENT* which would be a pointer to clients[some_num]. my question is this: when i resize the vector again, all its data goes to new memory locations. would that screw up my pointers? i.e. i have a CLIENT * lpClient; that is set to &clients[0]. when i resize the vector, will lpClient still point to clients[0] or will it point to the OLD memory location of where clients[0] used to be? i can correct the problem by using linked lists, but i don''t feel like doing that at the moment.
jaxson
jaxson
Your pointers will point to the old locations unless you update them too.
MadProgrammer
MadProgrammer
i can''t do that if i wana eventually be able to remove elements from the vector. that screws up the indexing and is poor memory management
Krunk
Krunk
How about putting pointers to CLIENT structs in the vector. Its messy (and the performance is dire), but its quicker and easier than writing a linked list.

You have to allocate the memory manually for the structs, but their addresses stay the same even when you resize the vector.
Dactylos
Dactylos
quote:
Original post by Krunk
How about putting pointers to CLIENT structs in the vector. Its messy (and the performance is dire), but its quicker and easier than writing a linked list.

You have to allocate the memory manually for the structs, but their addresses stay the same even when you resize the vector.

There''s no need to use a custom-written linked list, just use std::list or std::slist from the STL.
Krunk
Krunk
*ahem* Good call Dactylos and AP...

I had the same problem as MadProgrammer a while back and ended up using the nasty solution I posted above. The only reason I couldnt switch to list was that all the code I had written before i hit the problem needed indexed access to the collection.

There
s a lesson in there somewhere
synopsis
synopsis
Hi,

Can''t you just use a simple linked list? They are simple, can be implemented in about five to ten minutes, and are damned fast in terms of runtime. You don''t need the C++-Runtime library, you don''t need to include the STL headers and so forth. Performance rocks, code stays small and you can work with simple pointers instead of some classes thatdo things you don''t know of... You dramatically enhance control of what is being done if you stuck to ye olde C-techniques...

What do you mean by ''I don''t feel like doing that at the moment''?

BTW: How many clients are there? What about a static array?

Topic Locked

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

Sign in to reply to this topic.