C++ pointer question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

C++ pointer question

Post 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...
Image ImageImage Image
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: C++ pointer question

Post 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.
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: C++ pointer question

Post 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;
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: C++ pointer question

Post 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

Code: Select all

    vector<int*> result;
to

Code: Select all

vector<int> result;
and it will work fine (just pass the actual value to push_back).
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: C++ pointer question

Post 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

Code: Select all

    vector<int*> result;
to

Code: Select all

vector<int> result;
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.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: C++ pointer question

Post 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

Code: Select all

    vector<int*> result;
to

Code: Select all

vector<int> result;
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...
Image ImageImage Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: C++ pointer question

Post 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

Code: Select all

    vector<int*> result;
to

Code: Select all

vector<int> result;
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.
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: C++ pointer question

Post 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

Code: Select all

    vector<int*> result;
to

Code: Select all

vector<int> result;
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).
Image
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: C++ pointer question

Post 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:

Code: Select all

vector<Entity*> entities;
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...... :shock2:
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: C++ pointer question

Post 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.
Image ImageImage Image
Post Reply