|
Home > Archive > Unix Programming > January 2007 > is comparing pointers valid?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
is comparing pointers valid?
|
|
|
| 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.
| |
| David T. Ashley 2007-01-24, 1: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)
| |
| Herbert Pophal 2007-01-24, 1: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
| |
| Wayne C. Morris 2007-01-24, 7:19 pm |
| 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().
| |
| David T. Ashley 2007-01-24, 7:19 pm |
| "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)
| |
| David T. Ashley 2007-01-24, 7:19 pm |
| "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)
| |
| Ian Collins 2007-01-24, 7:19 pm |
| 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 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?
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.
| |
| David Schwartz 2007-01-25, 1: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
|
|
|
|
|