Page 1 of 1
C++ pointer question
Posted: Mon Dec 06, 2010 12:51 pm
by ismetteren
I have recently begun to look into C++ again after using Java and scala for a long time, and i have come across something about pointers i find quite odd:
Say i have a program like this:
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
std::vector<int *> foo()
{
vector<int*> result;
int j = 10;
result.push_back(&j);
return result;
}
int main()
{
std::vector<int *> r = foo();
std::cout << *r.at(0) << std::endl;
return 0;
}
What i would expect is to get the output 10, but instead it gives me: 134515514
It is probably something simple, but i just can't figure out why i don't get the expected output...
Re: C++ pointer question
Posted: Mon Dec 06, 2010 1:02 pm
by adikid89
j is a local variable and it gets destroyed when the function returns. Thus the pointer to it is undefined, so are the results you're having.
Re: C++ pointer question
Posted: Mon Dec 06, 2010 2:31 pm
by X Abstract X
Adikid answered your question but if you did need to do something like this, you could dynamically allocate inside the function and then take care of deleting it later. Also, it might be a good idea to pass your vector in by reference to avoid extra copying. I think the compiler might optimize the return by value you did to avoid copying but I'm not sure. Either way, I would only return by value where it is absolutely necessary or when you're returning a primitive type.
Code: Select all
void foo(std::vector<int*>& vec) {
vec.push_back(new int(10));
}
std::vector<int*> r;
foo(r);
//Clean up, dynamically allocated objects will live on until you delete them yourself
for (std::vector<int*>::const_iterator i = r.begin(); i != r.end(); ++i)
delete *i;
Re: C++ pointer question
Posted: Tue Dec 07, 2010 3:30 pm
by Falco Girgis
For primitive datatypes, you are waaaay better off just storing them by value rather than reference (they are smaller than a pointer AND you won't have to manually deallocate them).
Change
to
and it will work fine (just pass the actual value to push_back).
Re: C++ pointer question
Posted: Wed Dec 08, 2010 2:44 am
by MrDeathNote
GyroVorbis wrote:For primitive datatypes, you are waaaay better off just storing them by value rather than reference (they are smaller than a pointer AND you won't have to manually deallocate them).
Change
to
and it will work fine (just pass the actual value to push_back).
Definitely a smarter move, i think he was using this as an example but it would have been better to use a reference variable of some kind rather than an int.
Re: C++ pointer question
Posted: Thu Dec 09, 2010 11:14 am
by ismetteren
MrDeathNote wrote:GyroVorbis wrote:For primitive datatypes, you are waaaay better off just storing them by value rather than reference (they are smaller than a pointer AND you won't have to manually deallocate them).
Change
to
and it will work fine (just pass the actual value to push_back).
Definitely a smarter move, i think he was using this as an example but it would have been better to use a reference variable of some kind rather than an int.
Yeah, it was just an example.
I find that i'm using pointers quite often compared to how late they often are introduced to people who are learning the language. On the other hand, in languages like java, you can only use pointer-like things...
Re: C++ pointer question
Posted: Thu Dec 09, 2010 7:38 pm
by Falco Girgis
ismetteren wrote:MrDeathNote wrote:GyroVorbis wrote:For primitive datatypes, you are waaaay better off just storing them by value rather than reference (they are smaller than a pointer AND you won't have to manually deallocate them).
Change
to
and it will work fine (just pass the actual value to push_back).
Definitely a smarter move, i think he was using this as an example but it would have been better to use a reference variable of some kind rather than an int.
Yeah, it was just an example.
I find that i'm using pointers quite often compared to how late they often are introduced to people who are learning the language. On the other hand, in languages like java, you can only use pointer-like things...
Good. Pointers are used just as often as primitive objects in C/++. They're a GIGANTIC deal.
Re: C++ pointer question
Posted: Thu Dec 09, 2010 10:05 pm
by Kyosaur
GyroVorbis wrote:ismetteren wrote:MrDeathNote wrote:GyroVorbis wrote:For primitive datatypes, you are waaaay better off just storing them by value rather than reference (they are smaller than a pointer AND you won't have to manually deallocate them).
Change
to
and it will work fine (just pass the actual value to push_back).
Definitely a smarter move, i think he was using this as an example but it would have been better to use a reference variable of some kind rather than an int.
Yeah, it was just an example.
I find that i'm using pointers quite often compared to how late they often are introduced to people who are learning the language. On the other hand, in languages like java, you can only use pointer-like things...
Good. Pointers are used just as often as primitive objects in C/++. They're a GIGANTIC deal.
Can you show how you personally use them? I know how to use them, just not WHEN to. I think this is a common thing when it comes to pointers

.
If i didnt use STL i would use them for dynamic memory and linked lists, but besides than that i dont really when to use them; other than as params (even then i tend to use references more).
Re: C++ pointer question
Posted: Fri Dec 10, 2010 12:11 am
by adikid89
Polymorphism ftw: !!!!
Code: Select all
void DoSomething(Base* base)
{
base->Update();
}
int main()
{
Derived* d = new Derived;
DoSomething(d);
delete d;
return 0;
}
Storing stuff ftw:
Dynamic memory allocation.
Returning reference to objects without crashing...
Code: Select all
Image* GetImage(int id)
{
ImageList::iterator i = images.find(id);
if(i != images.end())
return i->second;
else return 0; //you can't do this with "normal" references => crash
}
Pointers are godly......

Re: C++ pointer question
Posted: Fri Dec 10, 2010 1:37 am
by ismetteren
Kyosaur wrote:
Can you show how you personally use them? I know how to use them, just not WHEN to. I think this is a common thing when it comes to pointers

.
If i didnt use STL i would use them for dynamic memory and linked lists, but besides than that i dont really when to use them; other than as params (even then i tend to use references more).
Like adikid89 said, polymorphism. That, and when i want several objects/classes to have references to the same object.