GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #11  
Old 13-Nov-2005, 14:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by Paramesh
Hi Dave,

Thank You for your wonderful explanation.
Since you told not to say sorry, i'll correct my mistakes from now.

That statement would fit to me.


That little wisecrack at the end of my post was not (not) directed at any currently active members, helpers or guests; it's just another of the silly attempts that I make from time to time to try lighten things up a little. Sometimes people think I mean something personal, bit I really don't (really). (But seriously: if you don't know pointers, you don't know C, in my very humble opinion.)

I have learned more (about pointers and lots of other things) from following threads on this forum for the last year or so than I have in many, many, many years of previous experience. (How many? Don't ask.)

That's the main reason I keep coming back: I always (always) learn something. (I have tried to stay away, but it's more than a little addictive, right?)

Regards,

Dave
  #12  
Old 13-Nov-2005, 19:26
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by Dave
That little wisecrack at the end of my post was not (not) directed at any currently active members, helpers or guests;
No. I didnt think that way. It was just my comment.

Quote:
Originally Posted by Dave
That's the main reason I keep coming back: I always (always) learn something. (I have tried to stay away, but it's more than a little addictive, right?)
Yeah. True.


Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #13  
Old 13-Nov-2005, 21:11
svenveer svenveer is offline
New Member
 
Join Date: Oct 2005
Posts: 15
svenveer is on a distinguished road

Re: Sorting program using pointer notation NEED HELP!!


Just a thought. A pionter is nothing more than a long so:

CPP / C++ / C Code:
tmpptr=name[maxpos];
name[maxpos]=name[eff_size-1];
name[eff_size-1]=tmpptr;

could be replaced with:
name[maxpos] ^= name[eff_size-1];
name[eff_size-1] ^= name[maxpos];
name[maxpos] ^= name[eff_size-1];

Or am I way out of line here ;-)
Last edited by LuciWiz : 14-Nov-2005 at 03:04. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #14  
Old 14-Nov-2005, 08:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by svenveer
Just a thought. A pionter is nothing more than a long so:

Or am I way out of line here ;-)

There is nothing in the C or C++ language specification that says "a pointer is nothing more than a long". Where did you get the idea that a pointer and a long are the same thing?

Pointer arithmetic is defined for the following:

1.Adding an integer data type to a pointer.

2. Subtracting an integer data type from a pointer.

3. Subtracting a pointer from another pointer (Where the pointers point to the same thing).

If you actually try your code, the compiler will refuse to compile, and you will see a compile-time error message. (If you don't see an error message, your compiler is not compliant with the C standard language specification. I would be interested in knowing what compiler allows exclusive-or operations on pointers.)

Here comes the editorial: [edit] Skip the editorial if you want to, but you might (or might not) be interested in the p.s. at the end[/edit]

Why, oh why, would you deliberately create a program that does things not defined in the language standard? (Especially since there are several legal ways to do what you propose --- swap the values of two variables.)

The following points are my opinion only, and you are entitled to your opinion (even if it differs from mind):

1. The use of exclusive-or operations to swap the values of two integer data types is looked upon by some as a neat trick that is somehow more efficient than use of a temporary variable (the way that the original program did for its pointers). That was actually a valuable thing in one's little bag of tricks in 1973 with assembly language programs for an Intel 8008 processor that didn't have very many registers, and for which memory accesses were agonizingly slow and took several instructions to set up address registers for memory access.

2. It's a good academic exercise for people studying bit operators and can show some of the interesting properties arithmetic with the exclusive-or operatiion.

3. It is not more efficient in time or code than use of a temporary variable for any modern processor on which people are likely to be running their code. If you find a situation where it is better in any way for your application: use it (It's your decision to make, but I would be interested in seeing the context in which anyone thinks it's appropriate --- for whatever reason.). Otherwise store it away for the day when someone comes to you trying to do this with floating point variables (using casts to make the compiler treat them as ints), and asks why it doesn't work: It only works for integer data types.

4. In C or C++, exclusive-or operations are only defined for integer data types (int, long, char, unsigned int, unsigned long, etc.), so the compiler will not even allow you to try it for pointers, floats, and other non-integer data types.

Regards,

Dave

P.S. Opinions are like belly-buttons: everyone has one. Furthermore, everyone has a right to his/her own opionion, and everyone has a right to defend his/her own opinions --- actively or passively --- against unwanted invasion. I can't think of anything more frightening than a world populated entirely with people whose opinions on everything are exactly like mine.

D
Last edited by davekw7x : 14-Nov-2005 at 09:02.
  #15  
Old 14-Nov-2005, 11:44
svenveer svenveer is offline
New Member
 
Join Date: Oct 2005
Posts: 15
svenveer is on a distinguished road

Re: Sorting program using pointer notation NEED HELP!!


That's exactly why I added:

"Or am I way out of line here ;-)"
  #16  
Old 14-Nov-2005, 12:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sorting program using pointer notation NEED HELP!!


Quote:
Originally Posted by svenveer
That's exactly why I added:

"Or am I way out of line here ;-)"

Your stated assumption that, "a pointer is nothing more than a long" is incorrect. I think I covered that.

The question, "am I way out of line?" is requesting a judgment call, which I am not prepared to make. I gave a few reasons that I wouldn't do it (even if I could). So, I "answered" the question with a question: "Why would you do this (even if you could)?"

Some people say that there's no such thing as a dumb question. I question that--- I've seen lots of dumb questions.

However, I don't think that your question was a dumb question at all. Lots of people have seen this little trick (swapping values using exclusive-or instead of a temporary variable) and think it's a good, clever, thing to put into a C program. I enjoy the opportunity to show the downside of it.

Regards,

Dave
  #17  
Old 14-Nov-2005, 12:15
svenveer svenveer is offline
New Member
 
Join Date: Oct 2005
Posts: 15
svenveer is on a distinguished road

Re: Sorting program using pointer notation NEED HELP!!


Althoug in 1973 I was a little young, when I started C/C++ programming the intell 80286 was something for people with a lot of money. I used these tricks a lot. Unfortunately, faith moved me to Java and is now moving me back to C++. I seem to have forgotten some things.

Since I work on software for small devices (mobile phones), I guess this neat trick is still might be usefull, i guess it just won work for pointers.

As to dumb questions. Most questions aren't dumb IMHO. The dumbest questions are the ones not asked.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.