A problem about use List

X

xianwei

Guys:
The problem come from a book
Data structures and Algorithm Analysis in C

First, some code in list.h
struct Node;
typedef struct Node Node;
typedef struct Node *PtrNode;
typedef PtrNode List;

another code in list.c
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit(1)

List
MakeEmpty( List L )
{
if (L != NULL)
{
DeleteList( L );
}

L = malloc( sizeof (struct Node) );
if (L == NULL)
{
FatalError( "Out of memory!" );
}

L->Next = NULL;
return L;
}


finally, the code is below: in testlist.c
List L;

L = MakeEmpty( NULL );

The book tell me, that can initialize L.

I don't know why *NULL* can be as a parameter in MakeEmpty()
In MakeEmpty(), L's address is zero(or NULL)
so like below
0 = malloc( sizeof (struct Node));
I think is too dangerous, someone tell me, In 16 bit system,
the system use real address, so the 0's address has been
cover BIOS data, In 32 bit system, use virtual address,
so safer than 16 bit system.
But I want to know
the above code is whether a good choice?
writting some code to 0's address.

thanks.
 
M

Mark Bluemel

xianwei said:
First, some code in list.h
struct Node;
typedef struct Node Node;
typedef struct Node *PtrNode;
typedef PtrNode List;
another code in list.c ....

List
MakeEmpty( List L )
{ ....
L = malloc( sizeof (struct Node) ); ....

finally, the code is below: in testlist.c
List L;

L = MakeEmpty( NULL );

The book tell me, that can initialize L.

I don't know why *NULL* can be as a parameter in MakeEmpty()

Because C is a "pass by value" language, the "List L" parameter in
MakeEmpty can be treated as a local variable containing the value "NULL"
(0).

I think you need to spend more time reading some basic texts before
trying to read this sort of code. Not that this code is particularly
difficult, but you clearly haven't got a good grasp of the basics.
 
J

Joachim Schmitz

xianwei said:
Guys:
The problem come from a book
Data structures and Algorithm Analysis in C

First, some code in list.h
struct Node;
typedef struct Node Node;
typedef struct Node *PtrNode;
typedef PtrNode List;

another code in list.c
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit(1)

List
MakeEmpty( List L )
{
if (L != NULL)
{
DeleteList( L );
}

L = malloc( sizeof (struct Node) );
if (L == NULL)
{
FatalError( "Out of memory!" );
}

L->Next = NULL;
return L;
}


finally, the code is below: in testlist.c
List L;

L = MakeEmpty( NULL );

The book tell me, that can initialize L.

I don't know why *NULL* can be as a parameter in MakeEmpty()
In MakeEmpty(), L's address is zero(or NULL)
No, L's content is NULL, it points to NULL
so like below
0 = malloc( sizeof (struct Node));
I think is too dangerous, someone tell me, In 16 bit system,
It won't be possible as 0 isn't an lvalue
the system use real address, so the 0's address has been
cover BIOS data, In 32 bit system, use virtual address,
so safer than 16 bit system.
But I want to know
the above code is whether a good choice?
writting some code to 0's address.
It doesn't do that here. it writes the resultof malloc into L, overwriting
the previous content (a NULL in this case).

Bye, Jojo
 
R

Richard

Joachim Schmitz said:
No, L's content is NULL, it points to NULL

L's content is not NULL. L's content is a pointer to a NODE element.

What does "it points to NULL" mean? is this different from a NULL
pointer (which it isn't anyway)?
 
X

xianwei

No, L's content is NULL, it points to NULL
Yes, I express fault. My means

+++++++
+ L + <---------- 0x00000000 (address)
+++++++
It won't be possible as 0 isn't an lvalue
(void *)0 = malloc( sizeof (struct Node));
translate the 0 to an address.~~~, Is it Possible?
It doesn't do that here. it writes the resultof malloc into L, overwriting
the previous content (a NULL in this case).
Yes, It writes the result of malloc into L,
But In the MakeEmpty where is *L*
S = MakeEmpty(NULL);
MakeEmpty declaration is
List MakeEmpty( List L );
In MakeEmpty
L = malloc(sizeof (struct Node));
the L is Local variable~, so the NULL and the *L*, I think is the
same.
 
X

xianwei

Because C is a "pass by value" language, the "List L" parameter in
MakeEmpty can be treated as a local variable containing the value "NULL"
(0).
This is my express fault~~.
I know
#define NULL (void *)0
In the below reply, I try to express clearly again.
 
M

Mark Bluemel

xianwei said:
This is my express fault~~.
I know
#define NULL (void *)0
In the below reply, I try to express clearly again.

You definitely to do more reading and less posting...

I'll try once more.

When a function is invoked in C, all the parameters to the function are
passed to it _by value_. That means that some fresh storage is provided
into which the parameters are put. That storage is available to the
function _exactly_ as if it was a local variable in the function - so
the _value_ NULL is passed to MakeEmpty() as "List L", and L behaves
exactly like a local variable in MakeEmpty(), so it is perfectly OK for
MakeEmpty() to assign a value into L and for MakeEmpty() to return that
value.
 
G

Guest

xianwei said:
Guys:
The problem come from a book
Data structures and Algorithm Analysis in C

First, some code in list.h
struct Node;
typedef struct Node Node;
typedef struct Node *PtrNode;
typedef PtrNode List;

another code in list.c
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), exit(1)

List
MakeEmpty( List L )
{
if (L != NULL)
{
DeleteList( L );
}

L = malloc( sizeof (struct Node) );
if (L == NULL)
{
FatalError( "Out of memory!" );
}

L->Next = NULL;
return L;
}


finally, the code is below: in testlist.c
List L;

L = MakeEmpty( NULL );

The book tell me, that can initialize L.

I don't know why *NULL* can be as a parameter in MakeEmpty()
In MakeEmpty(), L's address is zero(or NULL)
so like below
0 = malloc( sizeof (struct Node));
I think is too dangerous, someone tell me, In 16 bit system,
the system use real address, so the 0's address has been
cover BIOS data, In 32 bit system, use virtual address,
so safer than 16 bit system.
But I want to know
the above code is whether a good choice?
writting some code to 0's address.

thanks.

"MakeEmpty" is has 2 meanings, depending on whether the list exists.

L also has 2 meanings, boolean and pointer to list.

In other words,

If a list exists, L != NULL (boolean)
make the list empty, that is delete list L (pointer)

Make an new empty list by
assigning to L (pointer) the address of a new first node

return L (pointer)


I have no opinion the 16/32 bits.

Jim
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top