LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-26-2008, 09:08 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
CPP: Multiple pointers to one heap object problem.


So while writing a game of mine, I ran into a problem that has mixed me up on how I thought pointers worked. I was just a little over 125% sure the following worked. So let me first explain a bit on how I see pointers working in one way, to see if someone can catch a problem if any.

Lets say you had two pointers:

Code:
type* a = NULL;
type* b = NULL;
You had one pointed to heap memory:

Code:
a = new type;
and then you had 'b' point to what 'a' points to, and then afterwards you were to set the fields through 'b':

Code:
a = b;

b->field_one ( 0 );
b->field_two ( 0 );
Now, if I check the values of those two fields through 'a', they come out exactly as I expected them too.

Now the large code block labled 'MAIN TEST CODE' seems more or less the same to me. Except 'b' would point to what 'a' points to by the means of a member function of a class. Such as:

Code:
class some_class
{
//...

public:
 type* a ( void ) const;

//...
};

//...
b = some_class->a ();
If I were to set the type that 'a' points to through 'b', 'a' seems unaffected.

My question is why? I was convinced before this and still am to some degree that I have done this before, but I can't find the code that proves this(I've lost some code over the past month so maybe it was there). Am I doing something wrong that I am oblivious to? Or am I wrong on me thinking that I have done this before?

The following code I mentioned a couple times earlier roughly demonstrates in a condensed way how exactly my game operates at a certain point, where the problem lies.


'MAIN TEST CODE'

Code:
#include <iostream>

struct data
{
 int a, b;
};

class one
{
private:
 data* var_data;

public:
 data* get_data ( void ) const { return var_data; }
 void  del_data ( void )       { delete var_data; }

 one () { var_data = NULL; }
};

void set_data ( data* i )
{
 i = new data;
 i->a = 3;
 i->b = 4;

 // Here it is shown that the data has been set.
 std::cout << "set_data:: i->a == " << i->a << " : i->b == " << i->b << "\n";
}

int main ()
{
 one a;

 set_data ( a.get_data () );

 data* b = a.get_data ();

 // Yet the pointer to the same (or at least it should be the same) data
 // shows it not being set.
 std::cout << b->a << " " << b->b << "\n";
 std::cout << a->a << " " << a->b << "\n";

 a.del_data ();

 return 0;
}
Thanks in advance.

Last edited by RHLinuxGUY; 03-26-2008 at 09:12 PM.
 
Old 03-27-2008, 05:02 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
The error is here:

Code:
void set_data ( data* i )
{
 i = new data;
 i->a = 3;
 i->b = 4;
}
In particular, in your function, i is a value, namely an adress which you pass to set_data. In the next step, i will be overwritten by a new value (the adress that "new data" will produce), so basically the original adress is being ignored here.


There are two (and more) solutions to this situation, which will also most likely help explain why it doesn't work.

Code:
// by pointer
void set_data ( data** i )
{
 *i = new data;
 (*i)->a = 3;
 (*i)->b = 4;
}

// in this case get_data has to be like that:
data** get_data ( void ) { return &var_data; }
Code:
// by reference
void set_data ( data*& i )
{
 i = new data;
 i->a = 3;
 i->b = 4;
}

// in this case get_data has to be like that:
data*& get_data ( void ) { return var_data; }

Last edited by fantas; 03-27-2008 at 07:21 AM.
 
Old 03-27-2008, 09:01 AM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
I see my error. Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CPP: 'delete' not deleting object. RHLinuxGUY Programming 4 01-07-2008 08:36 AM
A heap of problem, starting at error 17 bear^ Linux - Newbie 45 05-13-2007 12:47 PM
CPP: Multiple Defintion Error. RHLinuxGUY Programming 9 05-04-2007 10:16 AM
Multiple Core Pointers in xorg.conf file ? srfpala Linux - Hardware 1 12-08-2006 04:00 PM
cpp: pointers Wonko the Sane Programming 3 11-17-2003 03:38 AM


All times are GMT -5. The time now is 08:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration