stuck in Infinite-loop

B

Ben Bacarisse

arnuld said:
Why didn't I think of this :(

And why did I bother to write, four days ago:

| Date: Fri, 13 Nov 2009 14:03:47 +0000
|
| Somehow you have manage to convince your self that this is complex
| with lots of special cases. It isn't -- trust me! You just need to
| scan the list to find where the new element goes. Finding the right
| place for an element with priority p is usually a loop that stops when
| an element with priority <= p is found. You just need to change that
| to <.

:-(
 
C

Chad

I don't know what exactly you mean by above statement. The scalar
variable (object) is actually a struct pointer varibale (object).

I'm hoping that Keith, Richard, or the rest of the regulars will
correct me on this one. I was under the impression that a scalar
variable and an object were two distinct beasts.
I don't know why unions exists in C, I never saw anyone using that data
structure (except in K&R2 where authors tell us that the malloc() is
implemented internally using union of struct, section 8.7)

Perhaps one day you should purchase the book "Unix Network
Programming: The Sockets Networking API" by Stevens, Fenner, and
Rudoff. There are a couple of good examples in that book use unions.
With a tad bit of thought, it's really not hard to see why a union in
C is useful.
 
S

Seebs

I'm hoping that Keith, Richard, or the rest of the regulars will
correct me on this one. I was under the impression that a scalar
variable and an object were two distinct beasts.

Maybe.

"scalar types" in C are arithmetic types and pointer types; arrays and
structures are "aggregate types" by contrast.

An "object" is a "region of data storage in the execution environment,
the contents of which can represent values".

So a variable of scalar type necessarily denotes an object. Not every
object is an object of scalar type, nor is every object of scalar type
a variable.

Examples:
int i; // variable, scalar
struct { int i; } j; // variable, not scalar
int *p; // variable, scalar

p = malloc(100 * sizeof(int));

p[5] = 0; // p[5] is a scalar object, but not a variable

struct { int i; } *q;

q = malloc(100 * sizeof(*q));

q[5].i = 0; // q[5] is neither a scalar nor a variable

-s
 
K

Keith Thompson

Chad said:
I'm hoping that Keith, Richard, or the rest of the regulars will
correct me on this one. I was under the impression that a scalar
variable and an object were two distinct beasts.
[...]

An object is simply a "region of data storage in the execution
environment, the contents of which can represent values" (C99 3.14).

A "scalar" type is either an arithmetic type (integer (signed or
unsigned), floating-point, complex, or imaginary) or a pointer type
(pointer to object, to an incomplete type, or to function).

The standard doesn't use the word "variable", at least not in this
sense, but roughly speaking a variable is an object [*].

So all scalar variables are objects, but not all objects are scalar
variables. Similarly, all pointer objects are scalar objects, and all
scalar objects are objects.

In particular, given:

struct pq_element *head1;

head1 is both an object and a variable.

[*] Is a const-qualified object a "variable", since it can't vary?
Is an anonymous object created by malloc() a "variable"? Is an
object that's part of another object, such as an array element or
a struct or union member a "variable"? You might have definitive
answers to any or all of these questions, but others might have
*different* definitive answers. The standard could have defined
the word "variable" and settled all these questions, but it didn't.
There's no problem using the word "variable" in cases where it's
unambiguous, but "object" is more precise.
 
B

Barry Schwarz

I don't know what exactly you mean by above statement. The scalar
variable (object) is actually a struct pointer varibale (object).

Consider the two options described using pseudo-code

1 - a structure containing the head pointer
struct head_ptr (struct node *member_head}*struct_ptr;
struct_ptr = malloc(...
struct_ptr->member_head = NULL;
add_node(struct_ptr, ...

2 - a scalar object pointing to the head
struct node *scalar_head = NULL;
add_node(&scalar_head, ...

In 1, the first parameter of add_node has form
struct head_ptr *S
In 2, it has the form
struct node **N

In 1, you refer the address of head of your list with the expression
S->member_head
In 2, you do it with
*N

The only point I'm making is that the single member structure buys you
nothing but extra typing (and the confusion of others, including you
later, reading your code wondering why the struct has only one
member).
I don't know why unions exists in C, I never saw anyone using that data
structure (except in K&R2 where authors tell us that the malloc() is
implemented internally using union of struct, section 8.7)

Your changing the subject. I mentioned unions only to show that your
code derives absolutely no benefit from any advantage provided by the
structure.
Later one.




__func__ prints a function's name. (or you don't like C99 ?)

But in the case, __func__ prints "main" which is not the function
where the allocation failure occurred, thereby misdirecting any
potential analysis.
All the statements/messages are intended for programmers only.




I have thrown away my push() and using James version now, which still has
one problem I need to work on. Check my reply to James for that.



IIRC, the process included only copying of the elements by the caller of
pop(). Don't remember how exactly I did it but I never ever put the
liability (of free()ing the malloc() created by my library) on 3rd party.
I don't think its a good idea. Do you ?

Since pop returns nothing, how does the caller know what to copy?

The documentation for fopen documents the need to call fclose. The
non-standard but very common strdup function requires the user to free
the allocated copy. Since, as you say, this is for programmers and
not "users", reading the documentation is reasonably expected
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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top