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

Pointer to std::vector issues *code shown*

Started by adder_noir Sep 5, 2010 at 7:52 PM 20 replies 2.9k views
Original Post
adder_noir
adder_noir
I can't get this to work. I want a pointer to a vector not a full vector. But I want to be able to retrieve the size of the vector the pointer points to. So far this produces a compile error.

<<>>

Any ideas how it can be done? It means I can save about 200kb or so of memory per instance of my main character in the animation data each instance stores. Thanks ;o)

[Edited by - adder_noir on September 7, 2010 4:28:09 AM]
Buckeye
Buckeye
Either (*v).size() or v->size().
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
AverageMidget
AverageMidget
void find_size(void);

If that's meant as a forward declaration of the void find_size(vector* v) function, its wrong. It's parameter list needs to match, otherwise it's treated as a different function.
iMalc
iMalc
Quote:
Original post by adder_noir
I can't get this to work. I want a pointer to a vector not a full vector.
In most cases wanting to declare a pointer to a vector as a local variable or a class member is the wrong thing to do, and every time I've seen it on a forum it indicates a misunderatanding of how vectors work.
You mentioned saving some amount of memory, possibly related to doing this. This is almost certainly a misguided belief.

Can you please explain why you want to do what you want to do. What would you use it for?
karpatzio
karpatzio
If youre worried about passing arguments by value, why not pass them by reference?
void fooFunc(std::vector &foo)
or
void fooFunc(const std::vector &foo)
Zahlman
Zahlman
Quote:
Original post by adder_noir
It means I can save about 200kb or so of memory per instance of my main character in the animation data each instance stores.


Please explain, in detail, how you think this is going to work. Why are there even multiple instances of your "main character"? What does that mean? Show some code.
adder_noir
adder_noir
Buckeye's suggestion worked. Thanks mate as always. By main character I really mean main infantry unit. There is one class which loads all data relevent to this unit. The mesh, the vertex groups for animating and so on. I only want this to happen once.

For the actual rendering of the infantry unit there is another class. So one class loads all the stuff and the other is responsible for animating and rendering.

Perhaps I explain it badly but it works fine. Every time I want to create an infantry unit I make an instance of the infantry unit class. That class though needs data about vertex groups for animating. Now I can either define these once in the class that loads the soldier or I can copy them every time I want to make a copy of the unit. Pretty obvious which the better choice is.

I only want a pointer to an std::vector (which contains the vertices needed for individual limb animation) in the instances of the soldier class that does all the rendering. Their *mother* if you like is the class that loads all the stuff. I only want the vertex group vectors to exist in complete form in the mother class, not the instances of the unit class. It means a space saving and I need to know how to do what I asked for the internal animation functions within the unit class.

Maybe questions of this nature are better posted on a C++ forum where people won't always ask the nature of what's going on behind the question? Might save some confusion or the assumption of incompetency (in a beginner's forum).
karpatzio
karpatzio
If noone ever questions your motives for anything, and assumes you are an omnipotent c++ programmer, you would never get the benefit of others experience and advice.
Zakwayda
Zakwayda
Quote:
Maybe questions of this nature are better posted on a C++ forum where people won't always ask the nature of what's going on behind the question? Might save some confusion or the assumption of incompetency (in a beginner's forum).
I imagine this sort of thing is likely to be questioned on any technical forum; I don't think it's specific to GDNet. (Could be wrong though - you could always try some other programming forums and see if it's any different.)

In any case, storing or passing a raw pointer to a std::vector or other container from the standard library definitely raises a red flag, IMO, and is worth asking about. It sounds to me like what you're looking for is the flyweight pattern, more or less (although I could be wrong about that). If so, I think it'd be a bit more logical to wrap up the vector and any other shared data in a 'flyweight' object, and pass that around instead. Lastly, storing and passing of raw pointers tends to raise questions of ownership; using a smart pointer of some sort instead can help to address this problem.

IMO, a flyweight object stored using shared_ptr seems like it would be a logical solution here.
adder_noir
adder_noir
Quote:
Original post by jyk
IMO, a flyweight object stored using shared_ptr seems like it would be a logical solution here.


Thank you for your input jyk.

iMalc
iMalc
Quote:
Original post by adder_noir
Maybe questions of this nature are better posted on a C++ forum where people won't always ask the nature of what's going on behind the question? Might save some confusion or the assumption of incompetency (in a beginner's forum).
That would be your loss.

You'd find that virtually everyone who's opened up what they were really doing to certain resident experts here, that they were pleasantly surprised of remarkably simpler, more efficient, and easier to understand their code became after some really good suggestions.

You're not incompetent. As good a programmer as you probably are, you still simply don't have anywhere near the level of experience of many others here. I belong to several forums and the level of expertise of people here is second to none.
adder_noir
adder_noir
Quote:
Original post by iMalc
You'd find that virtually everyone who's opened up what they were really doing to certain resident experts here, that they were pleasantly surprised of remarkably simpler, more efficient, and easier to understand their code became after some really good suggestions.


Agreed.

Quote:
Original post by iMalc
You're not incompetent. As good a programmer as you probably are, you still simply don't have anywhere near the level of experience of many others here. I belong to several forums and the level of expertise of people here is second to none.


Mate I'm a s**t programmer who's only just started his degree in computing ;o) n00b extraordinaire. I am also diagnosed paranoid and have a mental health record as long as your arm (including a couple of admissions to a lock-in secure unit) which doesn't help with perception. Sometimes when the questions come too hard and fast I feel I'm under attack. You must be able to understand it I'm sure.

Thanks for your help it was very good.

adder_noir
adder_noir
Quote:
Original post by jykIMO, a flyweight object stored using shared_ptr seems like it would be a logical solution here.


I'm going to go with this. Checkout this link for a good reference about flyweight stuff:

http://sourcemaking.com/design_patterns/flyweight/cpp/1
adder_noir
adder_noir
I might start a topic on flyweight objects if that's ok?
Zakwayda
Zakwayda
Quote:
Original post by adder_noir
I might start a topic on flyweight objects if that's ok?
You are allowed to start new topics :)
adder_noir
adder_noir
Hahahah ok ;o) Let me do a bit more research first so I've got constructive things to say. I'm having a little bit of difficulty with shared_ptrs right now I'll be back when that's sorted ;o)
adder_noir
adder_noir
I might replace it with code that worked but I think the general theme of the thread suggests not just my code, but my concepts were garbage prior to input from others and as thus it is probably best removed to prevent confusion.
M2tM
M2tM
Quote:
Original post by adder_noir
I might replace it with code that worked but I think the general theme of the thread suggests not just my code, but my concepts were garbage prior to input from others and as thus it is probably best removed to prevent confusion.


Don't remove source code to save your ego once you've resolved a problem, it's confusing and nobody is making fun of you for posting a problem.

When people come reading this thread after searching for something they will see a large discussion centering on removed code and be even more confused.

Just leave code in. Remember, this isn't a 1 on 1 private discussion, but a forum which is indexed and searched and read by people who never saw your original source code but may have a similar issue.

*edit: I've just read that you said you have problems with paranoia and have a history of mental health problems. I apologize if this came off stern, the point I was trying to make was not meant to be made at your expense. I was just trying to be instructional and accurate, I hope it hasn't stressed you out! :)
_____"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

Topic Locked

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

Sign in to reply to this topic.