newbie c++ question

B

Bo Sun

hi:

please take a look at the following struct definition and operation:

struct node {
int item;
node* next;
node(int arg_item, node* arg_next)
{
item = arg_item;
next = arg_next;
}
};

typedef node *link;

link pp_t = new node(10, pp_t);

is pp_t now a circular linked list? will pp_t->next->item be accessed? if
not, why?

many thanks,

bo
 
J

John Carson

Bo Sun said:
hi:

please take a look at the following struct definition and operation:

struct node {
int item;
node* next;
node(int arg_item, node* arg_next)
{
item = arg_item;
next = arg_next;
}
};

typedef node *link;

link pp_t = new node(10, pp_t);

is pp_t now a circular linked list? will pp_t->next->item be
accessed? if not, why?

many thanks,

bo

The problem with this code is that pp_t does not point to anything
meaningful until new returns, so the argument you are passing to the
constructor is junk. One way around the problem is

link pp_t = new node(10, NULL);
pp_t->next = pp_t;

You could also overload the constructor with

node(int arg_item)
{
item = arg_item;
next = this;
}

and have the following code

link pp_t = new node(10);

but this would probably not be of any use for anything other than single
item lists.
 
J

Jeff Schwab

John said:
The problem with this code is that pp_t does not point to anything
meaningful until new returns, so the argument you are passing to the
constructor is junk. One way around the problem is

link pp_t = new node(10, NULL);
pp_t->next = pp_t;

You could also overload the constructor with

node(int arg_item)
{
item = arg_item;
next = this;
}

and have the following code

link pp_t = new node(10);

but this would probably not be of any use for anything other than single
item lists.

Here's what I think you want. Note that I used "this" in an initializer
list: I recently heard (in this newsgroup) that such behavior is
illegal. (If anyone explains this, I'll appreciate it.) So, you might
want to change ",\nnext( this )" to "next = this", and move it down a
few lines.

typedef struct Node* Link;

struct Node {

int item;
Link next;

Node( int arg_item, Link arg_next ):
item( arg_item ),
next( arg_next )
{

}

Node( int arg_item ):
item( arg_item ),
next( this )
{

}
};

#include <iostream>

int main( )
{
Link p = new Node( 10 );

std::cout << p->item << '\n'
<< p->next->item << '\n';
}
 
J

John Carson

Jeff Schwab said:
Here's what I think you want. Note that I used "this" in an
initializer list: I recently heard (in this newsgroup) that such
behavior is illegal. (If anyone explains this, I'll appreciate it.)

It is not illegal to use "this" in the initialiser list. There is no problem
if you are only using it to initialise a pointer. If you dereference "this"
in the initialiser list in order to get at data members, then you can have
problems.

The reason I did not use an initialiser list in my code is because it is
still legal to initialise the variables in the body of the constructor and I
didn't want to overwhelm the OP with new information.
 
J

Jeff Schwab

John said:
It is not illegal to use "this" in the initialiser list. There is no problem
if you are only using it to initialise a pointer. If you dereference "this"
in the initialiser list in order to get at data members, then you can have
problems.


Awesome, thanks for the good news.
The reason I did not use an initialiser list in my code is because it is
still legal to initialise the variables in the body of the constructor and I
didn't want to overwhelm the OP with new information.

Perfectly understandable, no criticism from this corner.
 
J

John Carson

John Carson said:
If you
dereference "this" in the initialiser list in order to get at data
members, then you can have problems.


Actually, you can also have problems if you dereference "this" to get at
member functions.
 
R

Ron Natalie

Bo Sun said:
typedef node *link;

Try to avoid obfuscating typedefs like this.
link pp_t = new node(10, pp_t);

is pp_t now a circular linked list? will pp_t->next->item be accessed? if
not, why?

No. At the time the new expression is evaluated pp_t has indeterminate value (since
it is the return from new that initializes it). The created node will have it's next pointer
set to this value. Actually, there's no guarantee that your program will even get that
far. Use of pointers with indeterminate values is undefined behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,408
Latest member
AlenaRay88

Latest Threads

Top