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:
#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...
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.
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;
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).
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).
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.
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).
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...
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).
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.
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).
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).
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.