Didn't get Ben Pfaff code (chapter 12 of The Book).

  • Thread starter grishin-mailing-lists
  • Start date
G

grishin-mailing-lists

Hello,

Now I'm reading Unleashed C, a book written by the community.
I like the style it's written. There are a lot of fun, I appreciate
it. Thank you guys.

There is a point I can't get for the last 2 days, though Ben explained
it. Possible the hassle is due to a poor translation from English into
Russian (Unfortunately I'm reading a translation).

Here we have a double pointer "new". I can't get what is it for and
the way it's used.

/* bin.c lines 79-109 */
int bin_insert(struct bin_tree *tree, int item)
{
struct bin_node *node, **new;

assert(tree != NULL);
new = &tree->root;
node = tree->root;
for (;;) {
if (node == NULL) {
node = malloc(sizeof *node);
*new = node;
if (node != NULL) {
node->data = item;
node->left = node->right = NULL;
tree->count++;
return 1;
}
else
return 0;
}
else if (item == node->data)
return 2;
else if (item > node->data) {
new = &node->right;
node = node->right;
}
else {
new = &node->left;
node = node->left;
}
}
}

On one hand the value stored in the variable has never been used. On
the other hand I tried to comment all "new" entries and eventually the
program doesn't work properly printing
D:\projects\MinGW\ch12\wpj>noname.exe
Seed value = 23488
Inserted 11:
Tree has 0 nodes, but tree count is 1.
Error(s) encountered, aborting execution.

So it used in a mysterious way (as for me).

In addition the same technique is used afterwards so I can't sluff it
(otherwise I would be sensible to skip the chapter altogether).

Thank you in advance.
 
B

Ben Pfaff

Here we have a double pointer "new". I can't get what is it for and
the way it's used.
[...]

On one hand the value stored in the variable has never been used.

Sure it gets used. "new" tracks the pointer that was
dereferenced to arrive at "node". When "node" turns out to be
NULL, the function assigns to *new to update that pointer.

I probably wouldn't write this code exactly this way any longer,
but I do believe that it is correct.
 
F

frank

Ben said:
Here we have a double pointer "new". I can't get what is it for and
the way it's used.
[...]

On one hand the value stored in the variable has never been used.

Sure it gets used. "new" tracks the pointer that was
dereferenced to arrive at "node". When "node" turns out to be
NULL, the function assigns to *new to update that pointer.

I probably wouldn't write this code exactly this way any longer,
but I do believe that it is correct.

Is it this one?

dan@dan-desktop:~/Desktop/source/dan$ gcc ben2.c -Wall -o out
dan@dan-desktop:~/Desktop/source/dan$ ./out
Seed value = 5622
Inserted 7: 7
Inserted 3: 3 7
Inserted 2: 2 3 7
Inserted 1: 1 2 3 7
Inserted 5: 1 2 3 5 7
Inserted 12: 1 2 3 5 7 12
Inserted 14: 1 2 3 5 7 12 14
Inserted 0: 0 1 2 3 5 7 12 14
Inserted 10: 0 1 2 3 5 7 10 12 14
Inserted 6: 0 1 2 3 5 6 7 10 12 14
Inserted 13: 0 1 2 3 5 6 7 10 12 13 14
Inserted 9: 0 1 2 3 5 6 7 9 10 12 13 14
Inserted 4: 0 1 2 3 4 5 6 7 9 10 12 13 14
Inserted 11: 0 1 2 3 4 5 6 7 9 10 11 12 13 14
Inserted 15: 0 1 2 3 4 5 6 7 9 10 11 12 13 14 15
Inserted 8: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Removed 1: 0 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Removed 15: 0 2 3 4 5 6 7 8 9 10 11 12 13 14
Removed 0: 2 3 4 5 6 7 8 9 10 11 12 13 14
Removed 3: 2 4 5 6 7 8 9 10 11 12 13 14
Removed 10: 2 4 5 6 7 8 9 11 12 13 14
Removed 7: 2 4 5 6 8 9 11 12 13 14
Removed 14: 2 4 5 6 8 9 11 12 13
Removed 5: 2 4 6 8 9 11 12 13
Removed 2: 4 6 8 9 11 12 13
Removed 11: 4 6 8 9 12 13
Removed 6: 4 8 9 12 13
Removed 13: 4 8 9 12
Removed 9: 4 8 12
Removed 4: 8 12
Removed 12: 8
Removed 8:
Success!
dan@dan-desktop:~/Desktop/source/dan$ cat ben2.c

I used Ben's trees break up texts in the Bible. It turned out to be a
faith-demoting experience. I was shocked by what "holiness" looked like
when dismantled on a tree.

What's with the total 76 here?

dan@dan-desktop:~/source/unleashed/ch12$ ls -l
total 76
-rw-r--r-- 1 dan dan 15246 2000-06-18 16:25 avl.c
-rw-r--r-- 1 dan dan 10850 2000-06-18 16:25 bin.c
-rw-r--r-- 1 dan dan 12337 2000-06-18 16:25 pbin.c
-rw-r--r-- 1 dan dan 15759 2000-06-18 16:25 rb.c
-rw-r--r-- 1 dan dan 15668 2000-06-18 16:25 tbin.c
dan@dan-desktop:~/source/unleashed/ch12$

--
 
M

Mark

frank said:
I used Ben's trees break up texts in the Bible. It turned out to be a
faith-demoting experience. I was shocked by what "holiness" looked like
when dismantled on a tree.

[OT]
Out of curiosity: what has shocked you so much?
[/OT]
 
F

frank

Mark said:
frank said:
I used Ben's trees break up texts in the Bible. It turned out to be a
faith-demoting experience. I was shocked by what "holiness" looked
like when dismantled on a tree.

[OT]
Out of curiosity: what has shocked you so much?
[/OT]

The sheer volume of hatred per byte.
--
 
P

Phred Phungus

Ben said:
Here we have a double pointer "new". I can't get what is it for and
the way it's used.
[...]

On one hand the value stored in the variable has never been used.

Sure it gets used. "new" tracks the pointer that was
dereferenced to arrive at "node". When "node" turns out to be
NULL, the function assigns to *new to update that pointer.

I probably wouldn't write this code exactly this way any longer,
but I do believe that it is correct.


I was looking at code that I found in my source backups recently and
find some odd-looking things:


dan@dan-desktop:~/Desktop/source/dan$ gcc ben9.c -Wall -o out
ben9.c: In function ‘main’:
ben9.c:37: warning: passing argument 1 of ‘addtree’ makes pointer from
integer without a cast
ben9.c:37: error: too few arguments to function ‘addtree’
dan@dan-desktop:~/Desktop/source/dan$ gcc ben8.c -Wall -o out
ben8.c: In function ‘main’:
ben8.c:24: warning: format not a string literal and no format arguments
ben8.c:27: warning: format ‘%f’ expects type ‘double’, but argument 3
has type ‘size_t’
dan@dan-desktop:~/Desktop/source/dan$ ./ out
bash: ./: is a directory
dan@dan-desktop:~/Desktop/source/dan$ ./out
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\dan\Application Data
CLIENTNAME=Console
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=TJA1-D5768E2D16
ComSpec=C:\WINDOWS\system32\cmd.exe
f95include=C:\Program Files\Silverfrost\FTN95\include
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Documents and Settings\dan
LIBRARY_PATH=C:\q\lib\gcc-lib\i686-pc-mingw32\4.0.4;C:\q\lib
LOGONSERVER=\\TJA1-D5768E2D16
mod_path=C:\Program Files\Silverfrost\FTN95\include
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Path=C:\Program Files\Silverfrost\FTN95;C:\q\bin;
C:\Perl\site\bin;C:\Perl\bin;C:\MinGW\bin;
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
E:\gfortran\libexec\gcc\i586-pc-mingw32\4.4.0;E:\gfortran\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 15 Model 4 Stepping 10, AuthenticAMD
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=040a
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\dan\LOCALS~1\Temp
TMP=C:\DOCUME~1\dan\LOCALS~1\Temp
USERDOMAIN=TJA1-D5768E2D16
USERNAME=dan
USERPROFILE=C:\Documents and Settings\dan
windir=C:\WINDOWS
There were 32 lines, average length -0.000000
dan@dan-desktop:~/Desktop/source/dan$ gcc ben5.c -Wall -o out
ben5.c: In function ‘main’:
ben5.c:24: warning: implicit declaration of function ‘getline’
ben5.c:15: warning: unused variable ‘lineNumber’
dan@dan-desktop:~/Desktop/source/dan$ ./out
^C
dan@dan-desktop:~/Desktop/source/dan$ gcc ben9.c -Wall -o out
ben9.c: In function ‘main’:
ben9.c:38: warning: passing argument 1 of ‘addtree’ makes pointer from
integer without a cast
ben9.c:38: error: too few arguments to function ‘addtree’
dan@dan-desktop:~/Desktop/source/dan$ cat ben9.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define my_file "text42.txt"
#define NUMBER 100
#define MAXFMTLEN 200

struct tnode
{
char *text;
unsigned long lineNumber;
struct tnode *left;
struct tnode *right;
};

int getline(FILE *, char *, int );
struct tnode *addtree(struct tnode * , char *, unsigned long);

int main(void)
{
FILE *fp;
char text[NUMBER];
unsigned long lineNumber = 0;
int len;
struct tnode *root;

if ((fp = fopen(my_file, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(EXIT_FAILURE);
}
root = NULL;

while((len = getline(fp, text, NUMBER)) > 0) {
++ lineNumber,
printf("%lu, %s", lineNumber, text);
addtree(lineNumber, text);
}


fclose(fp);
return 0;
}


/* getline: get line into s, return length */
int getline(FILE *fp, char *s, int lim)
{
char *p;
int c;


p = s;
while (--lim > 0 && (c = getc(fp)) != EOF && c != '\n')
*p++ = c;
if (c == '\n')
*p++ = c;
*p = '\0';
return (p - s);
}


// gcc ben9.c -Wall -o out
dan@dan-desktop:~/Desktop/source/dan$


The first part is odd, because none of the system stuff is true, because
I'm now on ubuntu.

Those who desire to port open source to windows might want to take a
better look at Joe Windows than they have. It may be ugly, but it's me
minus six months.

The second part I would call a broken branch.
--
 
B

Ben Bacarisse

Phred Phungus said:
The first part is odd, because none of the system stuff is true,
because I'm now on ubuntu.

I can't tell from you output what you compiled and what you ran. In
one case there was an error that would normally prevent a program from
being built. To be sure what you are running, I'd suggest you get
into the habit or doing something like:

$ rm b1
$ gcc b1.c -o b1
$ ./b1

I.e. delete any old version of the compiled program and name the
compiled binary in some way so that it relates to the source.

<snip>
 
S

santosh

[ ... ]

Your use of a non-standard character set for your post renders your
text illegible.
 
G

grishin-mailing-lists

Here we have a double pointer "new". I can't get what is it for and
the way it's used.
[...]

On one hand the value stored in the variable has never been used.

Sure it gets used.  "new" tracks the pointer that was
dereferenced to arrive at "node".  When "node" turns out to be
NULL, the function assigns to *new to update that pointer.

I probably wouldn't write this code exactly this way any longer,
but I do believe that it is correct.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Eventually got it, thanks.
 
P

Phred Phungus

santosh said:
[ ... ]

Your use of a non-standard character set for your post renders your
text illegible.

Is is not utf-8? I never intend to post gibberish, but it happens.
 
N

Nick Keighley

[OT]
Out of curiosity: what has shocked you so much?
[/OT]

The sheer volume of hatred per byte.

shouldn't that be "density". How do you measure hatred? Or more to the
point how do get a computer to recognise it? Key words? Key phrases?
How does a tree help you?
 
S

santosh

Phred said:
santosh said:
[ ... ]

Your use of a non-standard character set for your post renders your
text illegible.

Is is not utf-8?

I recall that it was some Windows specific character set, some
characters of which are not rendered properly over here. Generally for
Usenet, you should stick to ASCII or a standardised encoding like
ISO-8859.
I never intend to post gibberish, but it happens.

Hmm. So I notice.
 
F

frank

Nick said:
Mark said:
I used Ben's trees break up texts in the Bible. It turned out to be a
faith-demoting experience. I was shocked by what "holiness" looked
like when dismantled on a tree.
[OT]
Out of curiosity: what has shocked you so much?
[/OT]
The sheer volume of hatred per byte.

shouldn't that be "density". How do you measure hatred? Or more to the
point how do get a computer to recognise it? Key words? Key phrases?
How does a tree help you?


1) Quoting Leviticus. Leviticus is for wimpy, women hating
mediterraenanean neanderthals. I'm unaware of a quote of Leviticus in
the new testament, so contrary is it to good news.

Quoting Leviticus is what that horrible Dr. Laura did the other day.
What institution presided over her ideological formation? I don't know,
but I'll guess it's Fixed Noise.

2) The software I used to display religious texts divided on lines, not
words. I've been trying to find the source and data, but have failed.

3) With a tree, you can see what they were talking about, ordered in a
way that Luke or Kohelet or Prediger or Ecclesiastes could not imagine.
As one peruses this text on a tree, things stick out that didn't
before. It's a basic technique in textual deconstruction.

As long as I'm talking about hatred, let me bring up the Grand Pooh-bah
thereof in America, Karl Rove. He was on fixed noise talking about 96
instances of Barack Obama saying 'I' in the stof. I've heard other
numbers tossed around, because it's complete Fox Shitpie.

I would remind these jerks that they seek to undermine the identity and
character of a democratically-elected president, as opposed to Bush regime.

If I have the text, I can tell you exactly what he said. The lies,
well-funded, will continue through the flux of hatred that is Fixed Noise.
--
 
D

Dann Corbit

Nick said:
Mark wrote:
I used Ben's trees break up texts in the Bible. It turned out to be a
faith-demoting experience. I was shocked by what "holiness" looked
like when dismantled on a tree.
[OT]
Out of curiosity: what has shocked you so much?
[/OT]
The sheer volume of hatred per byte.

shouldn't that be "density". How do you measure hatred? Or more to the
point how do get a computer to recognise it? Key words? Key phrases?
How does a tree help you?


1) Quoting Leviticus. Leviticus is for wimpy, women hating
mediterraenanean neanderthals. I'm unaware of a quote of Leviticus in
the new testament, so contrary is it to good news.

And yet there are hundreds. Here is a quote, to confirm my wimpy, women
hating, mediterraenanean neanderthals status:
(Leviticus 19:18) You must not take vengeance nor have a grudge against
the sons of your people; and you must love your fellow as yourself. I am
Jehovah.

Quite frankly, it doesn't sound so awful to me.
Quoting Leviticus is what that horrible Dr. Laura did the other day.
What institution presided over her ideological formation? I don't know,
but I'll guess it's Fixed Noise.

2) The software I used to display religious texts divided on lines, not
words. I've been trying to find the source and data, but have failed.

3) With a tree, you can see what they were talking about, ordered in a
way that Luke or Kohelet or Prediger or Ecclesiastes could not imagine.
As one peruses this text on a tree, things stick out that didn't
before. It's a basic technique in textual deconstruction.

As long as I'm talking about hatred, let me bring up the Grand Pooh-bah
thereof in America, Karl Rove. He was on fixed noise talking about 96
instances of Barack Obama saying 'I' in the stof. I've heard other
numbers tossed around, because it's complete Fox Shitpie.

I would remind these jerks that they seek to undermine the identity and
character of a democratically-elected president, as opposed to Bush regime.

If I have the text, I can tell you exactly what he said. The lies,
well-funded, will continue through the flux of hatred that is Fixed Noise.

The sensible way to classify a big pile of text is with a database
and/or clucene
 
R

Richard Tobin

Is is not utf-8? I never intend to post gibberish, but it happens.

It was the Microsoft "pseudo latin-1":

Content-Type: text/plain; charset=windows-1252; format=flowed

-- Richard
 
P

Phred Phungus

Dann said:
Nick said:
Mark wrote:
I used Ben's trees break up texts in the Bible. It turned out to be a
faith-demoting experience. I was shocked by what "holiness" looked
like when dismantled on a tree.
[OT]
Out of curiosity: what has shocked you so much?
[/OT]
The sheer volume of hatred per byte.
shouldn't that be "density". How do you measure hatred? Or more to the
point how do get a computer to recognise it? Key words? Key phrases?
How does a tree help you?

1) Quoting Leviticus. Leviticus is for wimpy, women hating
mediterraenanean neanderthals. I'm unaware of a quote of Leviticus in
the new testament, so contrary is it to good news.

And yet there are hundreds. Here is a quote, to confirm my wimpy, women
hating, mediterraenanean neanderthals status:
(Leviticus 19:18) You must not take vengeance nor have a grudge against
the sons of your people; and you must love your fellow as yourself. I am
Jehovah.

Quite frankly, it doesn't sound so awful to me.


There are not hundreds of Leviticus quotes in the new testament. I am
that I am is more Poppeye than anything, but when it is quoted by the
central figure of the new testament, what is the reaction?

They would pick up stones. Like hairy, stinky apes.

Do they take into account Ayeh asher Ayeh? Hardly.
The sensible way to classify a big pile of text is with a database
and/or clucene

Well I got a C in Book of Mormon from BYU.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top