473,413 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

adding local variable/struct to container - problem

Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:

std::vector<Entry> models;

struct Entry{
int index;
FeatureVector* value;
int object;
};

void ModelContainer::addModel(FeatureVector* pFeatVec,int index, int
object) {
Entry tmpEntry;
tmpEntry.index=index;
tmpEntry.object = object;
tmpEntry.value=pFeatVec;
models.push_back(tmpEntry);
}

To come to my question: Is this method valid as such? It does work so
far, but I am not sure whether this could be just luck? I know that it
could make no sense to return local variables, because they are
destroyed when they get out of scope. Is it the same thing here? Am I
adding an element which is destroyed after the method or am I copying
the values to a new location by adding them to the vector and
everything is fine? If I dealt with variables, I would use the "new"
operator, but this is not possible with structs isn't it?

Thanks
Tim

Jul 1 '06 #1
2 3759
"silversurfer" <ki****@web.dewrote:
Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:

std::vector<Entrymodels;

struct Entry{
int index;
FeatureVector* value;
int object;
};

void ModelContainer::addModel(FeatureVector* pFeatVec,int index, int
object) {
Entry tmpEntry;
tmpEntry.index=index;
tmpEntry.object = object;
tmpEntry.value=pFeatVec;
models.push_back(tmpEntry);
}

To come to my question: Is this method valid as such?
Yes, it's valid.

However, it's not the way I'd do it. I'd make a parameterized
constructor for Entry; then your "addModel" function can become
just one line, like so:

struct Entry
{
Entry(FeatureVector* _value, int _index, int _object)
: value(_value), index(_index), object(_object) {}
FeatureVector* value;
int index;
int object;
};

void
ModelContainer::
addModel(FeatureVector* value, int index, int object)
{
models.push_back(Entry(value, index, object));
}
It does work so far, but I am not sure whether this could
be just luck?
Well, your method should always work; but I like my method
above better, because it's terser.
I know that it could make no sense to return local variables,
because they are destroyed when they get out of scope.
Is it the same thing here?
No.
Am I adding an element which is destroyed after the method
No.
or am I copying the values to a new location by adding them
to the vector and everything is fine?
Yes. The std::vector<whatever>::push_back() function
copies stuff into a vector by value. That is, it uses "copy
semantics", putting a separate copy of its argument into the
vector.
If I dealt with variables, I would use the "new" operator,
but this is not possible with structs isn't it?
You could do that too, if you want. In that case, it's best
to store pointers to the objects in a vector, like so:

#include <iostream>
#include <vector>

struct Gargoyle
{
Gargoyle(char a, int b) : actor(a), eger(b) {}
char actor;
int eger;
};

int main()
{
std::vector<Gargoyle*Fizzbin;
Fizzbin.push_back(new Gargoyle('a', 97));
Fizzbin.push_back(new Gargoyle('b', 98));
Fizzbin.push_back(new Gargoyle('c', 99));

// ... a bunch of code ...
// ... use elements of Fizzbin somehow ...
// ... a bunch more code ...

// Clear Fizzbin:
std::vector<Gargoyle*>::iterator i;
for (i = Fizzbin.begin() ; i != Fizzbin.end() ; )
{
std::cout << (*i)->actor << " " << (*i)->eger << std::endl;
delete (*i); // delete object at location (*i)
i = Fizzbin.erase(i); // delete pointer from vector
}
return 0;
}

I just created 3 new Gargoyle objects dynamically,
stored pointers to them in a vector, used the objects,
freed the memory to avoid leaks, and erased the pointers
to avoid leaving invalid pointers in the vector.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 2 '06 #2
Thanks for the hint.. your version really seems clearer/better..

Greetings

Robbie Hatley wrote:
"silversurfer" <ki****@web.dewrote:
Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:

std::vector<Entrymodels;

struct Entry{
int index;
FeatureVector* value;
int object;
};

void ModelContainer::addModel(FeatureVector* pFeatVec,int index, int
object) {
Entry tmpEntry;
tmpEntry.index=index;
tmpEntry.object = object;
tmpEntry.value=pFeatVec;
models.push_back(tmpEntry);
}

To come to my question: Is this method valid as such?

Yes, it's valid.

However, it's not the way I'd do it. I'd make a parameterized
constructor for Entry; then your "addModel" function can become
just one line, like so:

struct Entry
{
Entry(FeatureVector* _value, int _index, int _object)
: value(_value), index(_index), object(_object) {}
FeatureVector* value;
int index;
int object;
};

void
ModelContainer::
addModel(FeatureVector* value, int index, int object)
{
models.push_back(Entry(value, index, object));
}
It does work so far, but I am not sure whether this could
be just luck?

Well, your method should always work; but I like my method
above better, because it's terser.
I know that it could make no sense to return local variables,
because they are destroyed when they get out of scope.
Is it the same thing here?

No.
Am I adding an element which is destroyed after the method

No.
or am I copying the values to a new location by adding them
to the vector and everything is fine?

Yes. The std::vector<whatever>::push_back() function
copies stuff into a vector by value. That is, it uses "copy
semantics", putting a separate copy of its argument into the
vector.
If I dealt with variables, I would use the "new" operator,
but this is not possible with structs isn't it?

You could do that too, if you want. In that case, it's best
to store pointers to the objects in a vector, like so:

#include <iostream>
#include <vector>

struct Gargoyle
{
Gargoyle(char a, int b) : actor(a), eger(b) {}
char actor;
int eger;
};

int main()
{
std::vector<Gargoyle*Fizzbin;
Fizzbin.push_back(new Gargoyle('a', 97));
Fizzbin.push_back(new Gargoyle('b', 98));
Fizzbin.push_back(new Gargoyle('c', 99));

// ... a bunch of code ...
// ... use elements of Fizzbin somehow ...
// ... a bunch more code ...

// Clear Fizzbin:
std::vector<Gargoyle*>::iterator i;
for (i = Fizzbin.begin() ; i != Fizzbin.end() ; )
{
std::cout << (*i)->actor << " " << (*i)->eger << std::endl;
delete (*i); // delete object at location (*i)
i = Fizzbin.erase(i); // delete pointer from vector
}
return 0;
}

I just created 3 new Gargoyle objects dynamically,
stored pointers to them in a vector, used the objects,
freed the memory to avoid leaks, and erased the pointers
to avoid leaving invalid pointers in the vector.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) {...
4
by: gamja | last post by:
Hi all. I know that some padding bits are inserted between data members of a structure. Is this rule also applied for the variables on local stack or global??? For example, in following code...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
6
by: cpnet | last post by:
I've authored a custom web component (a non-ui component kinda like a DataSet) and so far it's working. When my web component is added to the web form in the designer from the toolbox, the...
16
by: Jonathan.Fillion | last post by:
Ok, I have a simple problem and I might just be blind and not see the (possibly simple) solution to it. I want to declare variables while adding them to a list simultaneously. What I want to do is...
4
by: Mike | last post by:
Hi ! I have some strange problem and I would like to know if it is a bug or not : In my projects, in 2 different .cpp files, I use the same name to define a local structure: file1.cpp : ...
1
by: slrj | last post by:
Can I use a local struct to store data in the "set" conatiner? If so, what is the correct method to do so? Compiler: Sun Studio 10 on Unix (Sun Solaris) #include<iostream> #include<set> ...
6
by: student1976 | last post by:
All Beginner/Intermediate level question. I understand that returning ptr to local stack vars is bad. Is returning foo_p_B from fnB() reliable all the time, so that using foo_p_A does not...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.