node again..

B

Bill Cunningham

I have this much down for a tree but still don't know what to do with
main. I have this struct.

struct node
{
double price;
int vol;
struct node *right;
struct node *left;
};

Now in the following node is an object of type struct and so is this but
node is also a function.

struct node *new(int v, double p)
{
struct node *this;
this = malloc(sizeof this);
if (!this)
return NULL;
this->vol = v;
this->price = p;
this->right = NULL;
this->left = NULL;
return this;
}

I'm 25% of the way there. I read this->price=p as the object this
pointing to the double price and p is assigning price. The objects right and
left are pointeres to the this object and also set to NULL.

That's making a new node. Now how to set the data to right and left I
don't know and how to delete nodes I don't know. I have only got this far.
Can someone help me out and I know there are tutorials on the web and they
look nice but they don't talk or explain.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I have this much down for a tree but still don't know what to do with
main. I have this struct.

struct node
{
double price;
int vol;
struct node *right;
struct node *left;
};

Looks ok.
Now in the following node is an object of type struct and so is this but
node is also a function.

No. There is no "type struct"; there's a type "struct node". You
haven't declared an object of type "struct node"; you've declared
"this" as an object of type "pointer to struct node". Your function
is called "new", not "node". In fact, you don't have *anything*
called "node" (your type is "struct node", not "node").

That's an impressive number of errors in a single sentence. I think
you're guessing again.
struct node *new(int v, double p)
{
struct node *this;
this = malloc(sizeof this);

You're using the wrong size. "sizeof this" is going to be the size of
a pointer, but the number of bytes to allocate is the size of a
"struct node". The recommended idiom is:

this = malloc(sizeof *this);

but the following equivalent code might be a bit easier to understand:

this = malloc(sizeof (struct node));
if (!this)
return NULL;

Good. (I'd have written "if (this == NULL)", but a lot of progammers
prefer the terser form.)
this->vol = v;
this->price = p;
this->right = NULL;
this->left = NULL;
return this;
}

I'm 25% of the way there. I read this->price=p as the object this
pointing to the double price and p is assigning price. The objects right and
left are pointeres to the this object and also set to NULL.

That's making a new node. Now how to set the data to right and left I
don't know and how to delete nodes I don't know. I have only got this far.
Can someone help me out and I know there are tutorials on the web and they
look nice but they don't talk or explain.

Until you can write correct code to allocate a node, there's probably
not much point in worrying about setting left and right or deleting
nodes.
 
B

Bill Cunningham

[snip]
Until you can write correct code to allocate a node, there's probably
not much point in worrying about setting left and right or deleting
nodes.

You've helped me out Keith just in explaining the malloc error. I guess
new->r is equivalant to (*new)r if new was used.

Bill
 
K

Keith Thompson

Bill Cunningham said:
[snip]
Until you can write correct code to allocate a node, there's probably
not much point in worrying about setting left and right or deleting
nodes.

You've helped me out Keith just in explaining the malloc error. I guess
new->r is equivalant to (*new)r if new was used.

You missed a ".".

Yes, if "new" is a pointer to a struct (or union) containing a member
named "r", then "new->r" is equivalent to "(*new).r".

Except that, in the code you posted, "new" is the name of a function,
not a pointer object. If you can't keep straight what entities in
your own code have what names, there's no way you're going to be able
to make it work.
 
S

Stephen Sprunk

Bill said:
I have this much down for a tree but still don't know what to do with
main. I have this struct.

struct node
{
double price;
int vol;
struct node *right;
struct node *left;
};

You don't "have a struct"; you have defined a type of "struct node".
Now in the following node is an object of type struct and so is this but
node is also a function.

struct node *new(int v, double p)

This is a function called new() which returns a (struct node *). Do not
continue until you are absolutely clear on that point.
{
struct node *this;
this = malloc(sizeof this);

this = malloc(sizeof *this);

or

this = malloc(sizeof (struct node *));
if (!this)
return NULL;
this->vol = v;
this->price = p;
this->right = NULL;
this->left = NULL;
return this;
}

That all looks normal.
I'm 25% of the way there. I read this->price=p as the object this
pointing to the double price and p is assigning price.

"this" is a (struct node *), which points to the memory you just malloc()'d.

"this->price" is the "price" field inside the (struct node) pointed-to
by "this". It might be slightly clearer (or not) to know that
"this->price" is just shorthand for "(*this).price".

The "= p" part is a normal assignment.
The objects right and left are pointeres to the this object and also
set to NULL.

This is what makes it a (binary) tree: each node has two optional
sub-nodes, commonly designated "left" and "right" since that's how
humans typically draw binary trees on paper. Since a new node initially
has no subnodes, though, these fields are set to NULL.
That's making a new node. Now how to set the data to right and left
I don't know and how to delete nodes I don't know. I have only got this
far. Can someone help me out and I know there are tutorials on the web
and they look nice but they don't talk or explain.

There are dozens of tutorials that explain binary trees well, but you
appear to be having serious trouble with basic C syntax, structures, and
pointers. You would be better served working on those basic issues
first before attempting to tackle a more complicated concept like binary
trees.

S
 
C

CBFalconer

Stephen said:
Bill Cunningham wrote:
.... snip ...

this = malloc(sizeof (*this));


That all looks normal.

I don't think so. Howabout:

if (this) {
this->vol = v;
this->price p;
this->right = this->left = NULL;
}
return this;
}
 
K

Keith Thompson

Stephen Sprunk said:
Bill Cunningham wrote: [...]
{
struct node *this;
this = malloc(sizeof this);

this = malloc(sizeof *this);

or

this = malloc(sizeof (struct node *));
[...]

Correction: the latter should be:

this = malloc(sizeof (struct node));
 
B

Barry Schwarz

I have this much down for a tree but still don't know what to do with
main. I have this struct.

struct node
{
double price;
int vol;
struct node *right;
struct node *left;
};

Now in the following node is an object of type struct and so is this but
node is also a function.

You are either referring to code you did not post or you need to slow
and read the code and your message more carefully.

There is no object named node. There is not type named struct. There
is no function named node.

There is a type named struct node. In this case node is the tag of
the struct. There is also a function named new. Within that function
there is an automatic object named this which is a pointer to struct
node.
struct node *new(int v, double p)
{
struct node *this;
this = malloc(sizeof this);

This allocates the wrong amount of space. this is a pointer and
sizeof this is most likely 4 (but possibly 8) bytes. On a modern
system, sizeof (struct node) is going to be at least 20 and also a
multiple of 8 (making 24 the most likely result). You probably meant
to use
this = malloc(sizeof *this); /* note the * dereference operator */
but more important is that you realize why.
if (!this)
return NULL;
this->vol = v;
this->price = p;
this->right = NULL;
this->left = NULL;

All of these (except possibly the one involving price) invoke
undefined behavior because the attempt to store data beyond the end of
the allocated area.
return this;
}

I'm 25% of the way there. I read this->price=p as the object this

There appears to be an extra 2 in your estimate.
pointing to the double price and p is assigning price. The objects right and

While it is true that the member price is guaranteed to start at the
beginning of the structure, this points to the struct and not to any
member in particular. It is the ->price which causes the evaluation
to result in the member price of the struct pointed to by this.

p is an object and not an operator or operation. p contains the value
that will be assigned to the member price.
left are pointeres to the this object and also set to NULL.

right and left are pointers but they ay not point to this. They must
contain the address of an object of type struct node this is an object
of type pointer to struct node. Also, it would defeat the purpose of
a tree if either contained the same address as this.
That's making a new node. Now how to set the data to right and left I
don't know and how to delete nodes I don't know. I have only got this far.
Can someone help me out and I know there are tutorials on the web and they
look nice but they don't talk or explain.

It seems to me you need to understand simple linked lists before you
tackle trees.
 
K

Keith Thompson

CBFalconer said:
I don't think so. Howabout:

if (this) {
this->vol = v;
this->price p;
this->right = this->left = NULL;
}
return this;
}

Unless I'm missing something, your code is exactly equivalent to
Bill's code. Bill does an early return, which some people dislike,
but apart from that I see nothing wrong with it.
 
S

Stephen Sprunk

CBFalconer said:
I don't think so. Howabout:

if (this) {
this->vol = v;
this->price p;
this->right = this->left = NULL;
}
return this;
}

The two sequences are equivalent and which to use is a matter of style.
Both look "normal" to me, though I'm more likely to write the former
since most of the projects I've worked on make extensive use of early
returns.

S
 
C

CBFalconer

Keith said:
Unless I'm missing something, your code is exactly equivalent to
Bill's code. Bill does an early return, which some people dislike,
but apart from that I see nothing wrong with it.

You're not missing anything. I think mine is far clearer.
 
M

Moi

This allocates the wrong amount of space. this is a pointer and sizeof
this is most likely 4 (but possibly 8) bytes. On a modern system,
sizeof (struct node) is going to be at least 20 and also a multiple of 8
(making 24 the most likely result). You probably meant to use
this = malloc(sizeof *this); /* note the * dereference operator */
but more important is that you realize why.


All of these (except possibly the one involving price) invoke undefined
behavior because the attempt to store data beyond the end of the
allocated area.


There appears to be an extra 2 in your estimate.

It seems to me you need to understand simple linked lists before you
tackle trees.

Please note the similar thread ("Trees or Tables") in comp.programming
started on dec 21. Bill actually managed to screw up. Omitting the '*' in
the malloc(sizeof this) and changing the function name to "new"
demonstrate excellent trolling ability. My guess is his next step will be
to throw in some "strcpy((char*) this, "1.23" ))" -like goodies for extra
comfort.

FYI,
AvK
 
R

Richard Tobin

You're not missing anything. I think mine is far clearer.

I think the original is far clearer. It has the normal, non-error
case at the top-level rather than inside a conditional.

-- Richard
 
B

Bill Cunningham

[snip]
Please note the similar thread ("Trees or Tables") in comp.programming
started on dec 21. Bill actually managed to screw up. Omitting the '*' in
the malloc(sizeof this) and changing the function name to "new"
demonstrate excellent trolling ability. My guess is his next step will be
to throw in some "strcpy((char*) this, "1.23" ))" -like goodies for extra
comfort.

Greetings Moi,

A lot of the code I have used here has been from learning code ha Moi wrote.
I have also looked a other tutorials and if I hadn't learned something I
wouldn't have made it this far. And Moi I think you showed me more than you
realize. I value your help too. Don't be offended that you don't think I
will not give you any credit. You have showed me much too.

Bill
 
B

Bill Cunningham

[snip]

Please note the similar thread ("Trees or Tables") in comp.programming
started on dec 21. Bill actually managed to screw up. Omitting the '*' in
the malloc(sizeof this) and changing the function name to "new"
demonstrate excellent trolling ability. My guess is his next step will be
to throw in some "strcpy((char*) this, "1.23" ))" -like goodies for extra
comfort.

Sorry you feel I'm trolling. I am trying to learn this stuff. I'm just a
hobbyist programmer. I tried to make your code simpler for my benefit and as
you say "screwed it up".

Bill
 
M

Moi

Sorry you feel I'm trolling. I am trying to learn this stuff. I'm
just a
hobbyist programmer. I tried to make your code simpler for my benefit
and as you say "screwed it up".

Bill

Don't feel offended by my gut feelings.

Happy new year, and may all your wishes come true.

HTH,
AvK
 

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
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top