Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   A question about 13.9 from the c faq. (http://www.velocityreviews.com/forums/t556623-a-question-about-13-9-from-the-c-faq.html)

Chad 12-01-2007 08:06 PM

A question about 13.9 from the c faq.
 
At the following url http://c-faq.com/lib/qsort2.html, they have the
following

Q: Now I'm trying to sort an array of structures with qsort. My
comparison function takes pointers to structures, but the compiler
complains that the function is of the wrong type for qsort. How can I
cast the function pointer to shut off the warning?

A: The conversions must be in the comparison function, which must be
declared as accepting ``generic pointers'' (const void *) as discussed
in question 13.8 above. For a hypothetical little date structure

struct mystruct {
int year, month, day;
};

the comparison function might look like [footnote]

int mystructcmp(const void *p1, const void *p2)
{
const struct mystruct *sp1 = p1;
const struct mystruct *sp2 = p2;
if(sp1->year < sp2->year) return -1;
else if(sp1->year > sp2->year) return 1;
else if(sp1->month < sp2->month) return -1;
else if(sp1->month > sp2->month) return 1;
else if(sp1->day < sp2->day) return -1;
else if(sp1->day > sp2->day) return 1;
else return 0;
}

(The conversions from generic pointers to struct mystruct pointers
happen in the initializations sp1 = p1 and sp2 = p2; the compiler
performs the conversions implicitly since p1 and p2 are void
pointers.)

The question is, why don't you use something like

const struct mystruct *sp1 = &p1;
const struct mystruct *sp2 = &p2;


Chad

Thad Smith 12-01-2007 08:25 PM

Re: A question about 13.9 from the c faq.
 
Chad wrote:

> struct mystruct {
> int year, month, day;
> };
>
> the comparison function might look like [footnote]
>
> int mystructcmp(const void *p1, const void *p2)
> {
> const struct mystruct *sp1 = p1;
> const struct mystruct *sp2 = p2;
> if(sp1->year < sp2->year) return -1;
> else if(sp1->year > sp2->year) return 1;
> else if(sp1->month < sp2->month) return -1;
> else if(sp1->month > sp2->month) return 1;
> else if(sp1->day < sp2->day) return -1;
> else if(sp1->day > sp2->day) return 1;
> else return 0;
> }
>
> The question is, why don't you use something like
>
> const struct mystruct *sp1 = &p1;
> const struct mystruct *sp2 = &p2;


The initializer would have the wrong type and value. You want to assign
the pointer value, not the address of it.

--
Thad

Don Bruder 12-01-2007 08:32 PM

Re: A question about 13.9 from the c faq.
 
In article
<0e494561-cbb9-4949-9e29-70fbedade611@a39g2000pre.googlegroups.com>,
Chad <cdalten@gmail.com> wrote:

> At the following url http://c-faq.com/lib/qsort2.html, they have the
> following
>
> Q: Now I'm trying to sort an array of structures with qsort. My
> comparison function takes pointers to structures, but the compiler
> complains that the function is of the wrong type for qsort. How can I
> cast the function pointer to shut off the warning?
>
> A: The conversions must be in the comparison function, which must be
> declared as accepting ``generic pointers'' (const void *) as discussed
> in question 13.8 above. For a hypothetical little date structure
>
> struct mystruct {
> int year, month, day;
> };
>
> the comparison function might look like [footnote]
>
> int mystructcmp(const void *p1, const void *p2)
> {
> const struct mystruct *sp1 = p1;
> const struct mystruct *sp2 = p2;
> if(sp1->year < sp2->year) return -1;
> else if(sp1->year > sp2->year) return 1;
> else if(sp1->month < sp2->month) return -1;
> else if(sp1->month > sp2->month) return 1;
> else if(sp1->day < sp2->day) return -1;
> else if(sp1->day > sp2->day) return 1;
> else return 0;
> }
>
> (The conversions from generic pointers to struct mystruct pointers
> happen in the initializations sp1 = p1 and sp2 = p2; the compiler
> performs the conversions implicitly since p1 and p2 are void
> pointers.)
>
> The question is, why don't you use something like
>
> const struct mystruct *sp1 = &p1;
> const struct mystruct *sp2 = &p2;


Because:

&p1 == the address of the variable (parameter, actually) p1

p1 == the variable holding the address of the struct under comparison

*p1 == the struct under comparison

Assuming p1 contains the (randomly chosen) address 5, and assuming p1 is
located in memory at (randomly chosen) location 27, and that the "..."
is replaced by the "const struct mystruct *" as shown above, the
following would be true:

assignment result
....sp1 = &p1 sp now holds 27 (the address of p1)
....sp1 = p1 sp now holds 5 (the contents of p1)
....sp1 = *p1 sp now holds <whatever value is found at location 5>

BUT...
Since p1 is a function parameter, rather than an actual variable, I
believe trying to take its address will result in undefined behavior.
(unless special steps are taken to make things happen differently,
parameters are usually passed on the stack, which makes taking their
address either impossible, or pointless, because for practical purposes,
they don't really HAVE an address to take.)

--
Don Bruder - dakidd@sonic.net - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info

Eric Sosman 12-01-2007 08:58 PM

Re: A question about 13.9 from the c faq.
 
Chad wrote:
> [... sorting arrays of structs with qsort() ...]
>
> struct mystruct {
> int year, month, day;
> };
>
> the comparison function might look like [footnote]
>
> int mystructcmp(const void *p1, const void *p2)
> {
> const struct mystruct *sp1 = p1;
> const struct mystruct *sp2 = p2;
> [...]
>
> The question is, why don't you use something like
>
> const struct mystruct *sp1 = &p1;
> const struct mystruct *sp2 = &p2;


Why don't you try it, and see what happens?

To understand why <<spoiler alert>> the compiler complains,
ask yourself: What is the type of `&p1'? And what does `&p1'
point to? And to understand the FAQ's code, ask yourself:
What does `p1' point to?

--
Eric Sosman
esosman@ieee-dot-org.invalid

James Kuyper 12-01-2007 09:20 PM

Re: A question about 13.9 from the c faq.
 
Don Bruder wrote:
....
> Since p1 is a function parameter, rather than an actual variable, I
> believe trying to take its address will result in undefined behavior.


A function parameter is an actual variable, and it's perfectly legal to
take its address. The only problem is that it's the wrong address, in
this context.

> (unless special steps are taken to make things happen differently,
> parameters are usually passed on the stack, which makes taking their
> address either impossible, or pointless, because for practical purposes,
> they don't really HAVE an address to take.)


On the machines where I've had to be familiar with such details, the
stack was a piece of memory that served a different purpose from the
heap and other pieces of memory, but was just as addressable as any of
those other pieces. If an implementation used something for parameter
passing for which that wasn't true (such as registers), it must copy the
values out of the registers into addressable memory, at least if the
code ever actually takes the address of the parameter.

Chad 12-01-2007 09:40 PM

Re: A question about 13.9 from the c faq.
 
On Dec 1, 12:58 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> Chad wrote:
> > [... sorting arrays of structs with qsort() ...]

>
> > struct mystruct {
> > int year, month, day;
> > };

>
> > the comparison function might look like [footnote]

>
> > int mystructcmp(const void *p1, const void *p2)
> > {
> > const struct mystruct *sp1 = p1;
> > const struct mystruct *sp2 = p2;
> > [...]

>
> > The question is, why don't you use something like

>
> > const struct mystruct *sp1 = &p1;
> > const struct mystruct *sp2 = &p2;

>
> Why don't you try it, and see what happens?
>
> To understand why <<spoiler alert>> the compiler complains,
> ask yourself: What is the type of `&p1'? And what does `&p1'
> point to? And to understand the FAQ's code, ask yourself:
> What does `p1' point to?
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid



Maybe this is lack of experience or maybe it's because I will still
once in a while get this burning sensation when I try to think, but it
appears that p1 points to thing.


Willem 12-01-2007 09:42 PM

Re: A question about 13.9 from the c faq.
 
James wrote:
) On the machines where I've had to be familiar with such details, the
) stack was a piece of memory that served a different purpose from the
) heap and other pieces of memory, but was just as addressable as any of
) those other pieces. If an implementation used something for parameter
) passing for which that wasn't true (such as registers), it must copy the
) values out of the registers into addressable memory, at least if the
) code ever actually takes the address of the parameter.

Doesn't the standard allow an implementation that has a pointer type
which can point to registers as well as memory ? I could imagine having
some kind of 'fat' pointer type with a flag that indicates 'this is a
pointer to register X'.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Don Bruder 12-01-2007 09:47 PM

Re: A question about 13.9 from the c faq.
 
In article <Iek4j.400$gi7.54@trnddc04>,
James Kuyper <jameskuyper@verizon.net> wrote:

> Don Bruder wrote:
> ...
> > Since p1 is a function parameter, rather than an actual variable, I
> > believe trying to take its address will result in undefined behavior.

>
> A function parameter is an actual variable, and it's perfectly legal to
> take its address.


OK, then I've been operating under a misconception for a while. While
they BEHAVE as variables, I've always been under the impression that
they "weren't really" due to the passing mechanism.

> The only problem is that it's the wrong address, in
> this context.


Right. The comparison would be against who-knows-what, but whatever that
turned out to be, it wouldn't be the desired data.

--
Don Bruder - dakidd@sonic.net - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info

Eric Sosman 12-01-2007 10:21 PM

Re: A question about 13.9 from the c faq.
 
Chad wrote:
> On Dec 1, 12:58 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>> Chad wrote:
>>> [... sorting arrays of structs with qsort() ...]
>>> struct mystruct {
>>> int year, month, day;
>>> };
>>> the comparison function might look like [footnote]
>>> int mystructcmp(const void *p1, const void *p2)
>>> {
>>> const struct mystruct *sp1 = p1;
>>> const struct mystruct *sp2 = p2;
>>> [...]
>>> The question is, why don't you use something like
>>> const struct mystruct *sp1 = &p1;
>>> const struct mystruct *sp2 = &p2;

>> Why don't you try it, and see what happens?
>>
>> To understand why <<spoiler alert>> the compiler complains,
>> ask yourself: What is the type of `&p1'? And what does `&p1'
>> point to? And to understand the FAQ's code, ask yourself:
>> What does `p1' point to?

>
> Maybe this is lack of experience or maybe it's because I will still
> once in a while get this burning sensation when I try to think, but it
> appears that p1 points to thing.


Right: `p1' points to one of the things in the array
being sorted, that is, to a `struct mystruct' instance. The
only problem is the type: `p1' is a `void*', but to get at
the struct elements you need a `struct mystruct*'. Hence
the conversion to `sp1'.

How did you make out with the other two questions? (And
did you try the experiment I suggested, or did you just accept
my spoiler as Gospel? It's flattering when people believe
what I say, but it's not always their wisest course ...)

--
Eric Sosman
esosman@ieee-dot-org.invalid

Keith Thompson 12-01-2007 10:26 PM

Re: A question about 13.9 from the c faq.
 
Chad <cdalten@gmail.com> writes:
> At the following url http://c-faq.com/lib/qsort2.html, they have the
> following
>
> Q: Now I'm trying to sort an array of structures with qsort. My
> comparison function takes pointers to structures, but the compiler
> complains that the function is of the wrong type for qsort. How can I
> cast the function pointer to shut off the warning?
>
> A: The conversions must be in the comparison function, which must be
> declared as accepting ``generic pointers'' (const void *) as discussed
> in question 13.8 above. For a hypothetical little date structure
>
> struct mystruct {
> int year, month, day;
> };
>
> the comparison function might look like [footnote]
>
> int mystructcmp(const void *p1, const void *p2)
> {
> const struct mystruct *sp1 = p1;
> const struct mystruct *sp2 = p2;
> if(sp1->year < sp2->year) return -1;
> else if(sp1->year > sp2->year) return 1;
> else if(sp1->month < sp2->month) return -1;
> else if(sp1->month > sp2->month) return 1;
> else if(sp1->day < sp2->day) return -1;
> else if(sp1->day > sp2->day) return 1;
> else return 0;
> }
>
> (The conversions from generic pointers to struct mystruct pointers
> happen in the initializations sp1 = p1 and sp2 = p2; the compiler
> performs the conversions implicitly since p1 and p2 are void
> pointers.)
>
> The question is, why don't you use something like
>
> const struct mystruct *sp1 = &p1;
> const struct mystruct *sp2 = &p2;


Um, because it's illegal (a constraint violation), and even if it
weren't it wouldn't work.

p1 is a parameter (a local object) of type void* (I'm ignoring the
const qualifier). When converted to type ``struct mystruct*'',
assuming the caller has passed the right arguments, it points to an
object of type ``struct mystruct''.

You propose instead to use ``&p1'', i.e., the *address* of p1. This
is the address of a pointer object, and is of type void**.

The question is, why would you want to use ``&p1'' when ``p1'' is
correct?

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 02:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.