 |
|
 |
|
|
 |
is comparing pointers valid? |
 |
 |
|
|
01-24-07 06:16 PM
Like so:
while(search!=NULL) {
if(!strcmp(tmp->x,search->x)&&
!strcmp(tmp->y,search->y)&&
!strcmp(tmp->z,search->z)&&
tmp!=search) { /* To exclude ourselves from search */
printf("Coordinates in use\n");
}
search=search->next;}
I think so but i'm not entirely sure.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-24-07 06:16 PM
"atv" <alef@xs4all.nl> wrote in message
news:45b76879$0$331$e4fe514c@news.xs4all.nl...
> Like so:
>
> while(search!=NULL) {
> if(!strcmp(tmp->x,search->x)&&
> !strcmp(tmp->y,search->y)&&
> !strcmp(tmp->z,search->z)&&
> tmp!=search) { /* To exclude ourselves from search */
> printf("Coordinates in use\n");
> }
> search=search->next;}
>
> I think so but i'm not entirely sure.
This is a better question for comp.lang.c.
Yes, comparing pointers for equality is valid. The code you posted looks
right to me.
However, I believe the 'C' standard says you may only compare relationally
(<, >, >=, <=) and subtract using pointers within the same array. So, if
you have a linked list with separately allocated blocks of memory for each
node (which you appear to have), you may compare the pointers for equality
but you are not allowed to subtract the pointers or relationally compare
them.
You should re-post this question in comp.lang.c. They answer the question
definitively and promptly.
--
David T. Ashley (dta@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-24-07 06:16 PM
David T. Ashley wrote:
> "atv" <alef@xs4all.nl> wrote in message
> news:45b76879$0$331$e4fe514c@news.xs4all.nl...
>
>
>
> This is a better question for comp.lang.c.
>
> Yes, comparing pointers for equality is valid. The code you posted looks
> right to me.
... tmp!=search /* To exclude ourselves from search */ should be better
the first comparison and not the last.
Herbert
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-25-07 12:19 AM
In article <45b76879$0$331$e4fe514c@news.xs4all.nl>, atv <alef@xs4all.nl>
wrote:
> Like so:
>
> while(search!=NULL) {
> if(!strcmp(tmp->x,search->x)&&
> !strcmp(tmp->y,search->y)&&
> !strcmp(tmp->z,search->z)&&
> tmp!=search) { /* To exclude ourselves from search */
> printf("Coordinates in use\n");
> }
> search=search->next;}
>
> I think so but i'm not entirely sure.
Yes, you can compare pointers.
But the example you gave is inefficient. The program will run a little
faster if you rearrange the if statement like this:
if (tmp!=search
&& !strcmp(tmp->x,search->x)
&& !strcmp(tmp->y,search->y)
&& !strcmp(tmp->z,search->z) )
{ /* To exclude ourselves from search */
This compares the pointers first. If they're equal, it knows that the
entire expression is false, so it doesn't have to call strcmp().
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-25-07 12:19 AM
"Herbert Pophal" <pophal@zrz.tu-berlin.de> wrote in message
news:51pe7uF1kpphsU1@mid.dfncis.de...
> David T. Ashley wrote:
>
> ... tmp!=search /* To exclude ourselves from search */ should be better
> the first comparison and not the last.
I think that depends on the probabilities involved. If there are a large
number of nodes (where only one will be "search"), the expected value of
execution may be less if you put that test at the end (i.e. it is far more
probable that the other tests will fail).
I make mistakes every day of the week ... but at first glance it seems to
depend on the probabilities of the other conditions and the number of nodes.
--
David T. Ashley (dta@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-25-07 12:19 AM
"Wayne C. Morris" <wayne.morris@this.is.invalid> wrote in message
news:wayne.morris-F97FFD.17175724012007@shawnews.wp.shawcable.net...
> In article <45b76879$0$331$e4fe514c@news.xs4all.nl>, atv <alef@xs4all.nl>
> wrote:
>
>
> Yes, you can compare pointers.
>
> But the example you gave is inefficient. The program will run a little
> faster if you rearrange the if statement like this:
>
> if (tmp!=search
> && !strcmp(tmp->x,search->x)
> && !strcmp(tmp->y,search->y)
> && !strcmp(tmp->z,search->z) )
> { /* To exclude ourselves from search */
>
> This compares the pointers first. If they're equal, it knows that the
> entire expression is false, so it doesn't have to call strcmp().
I think perhaps not.
The vast majority of times, (tmp != search) will be TRUE, so it seems to be
less efficient rather than more efficient, particularly with a lot of nodes.
With 100,000 nodes (for example), that test will be wasted 99,999/100,000
times. It will still have to go on to the other clauses 99,999/100,000
times.
Am I reading it wrong?
--
David T. Ashley (dta@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-25-07 12:19 AM
David T. Ashley wrote:
> "Wayne C. Morris" <wayne.morris@this.is.invalid> wrote in message
> news:wayne.morris-F97FFD.17175724012007@shawnews.wp.shawcable.net...
>
>
> I think perhaps not.
>
> The vast majority of times, (tmp != search) will be TRUE, so it seems to b
e
> less efficient rather than more efficient, particularly with a lot of node
s.
>
> With 100,000 nodes (for example), that test will be wasted 99,999/100,000
> times. It will still have to go on to the other clauses 99,999/100,000
> times.
>
> Am I reading it wrong?
You might be, the only way to know is for the OP to do the test with his
own data. The simple pointer comparison may return true most of the
time, but the cost of a compare is lower than a call to strcmp.
Test!
--
Ian Collins.
[ Post a follow-up to this message ]
|
|
|
 |
|
 |
|
 |
|
|
 |
Re: is comparing pointers valid? |
 |
 |
|
|
01-25-07 06:32 AM
On Jan 24, 3:17 pm, "Wayne C. Morris" <wayne.mor...@this.is.invalid>
wrote:
> But the example you gave is inefficient. The program will run a little
> faster if you rearrange the if statement like this:
If you care about efficiency, you should work on eliminating the string
compares, not optimizing the pointer comparison. For example, you can
generate a 32-bit hash of the x, y, and z values, and compare the hash
values first.
This is a *huge* performance boost if you have to test each entry
against every other when you add/create it.
The next step up is to use hash buckets so you only need to compare
against some of the entries.
DS
[ Post a follow-up to this message ]
|
|
|
 |
|
|
|
|
Sponsored Links |
 |
 |
|
|
 |
All times are GMT. The time now is 02:15 AM. |
 |
|
|
 |
|
 |
|
|
 |
|
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
|
|
|
|
Medical and Health forum | Computer Games Reviews | Graphics design forum
|
 |
|
 |
|