Thread: Pointers?

  1. #16
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by brewbuck View Post
    Quote Originally Posted by nadroj
    i think it is a subjective issue.
    Not really.
    "not really" by itself is a subjective answer.

    the functions purpose is to check if a number is even or not, the parameter x has postcondition that it will be even.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nadroj View Post
    "not really" is a subjective answer.

    the functions purpose is to check if a number is even or not, the parameter x has postcondition that it will be even.
    Then the function's purpose is clearly not to "check if a number is even or not." Its purpose is to ENSURE the number is even.

    Remind me not to have you write any code for me...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    dont worry i wont

    again, the functions purpose is obvious. a required side effect or postcondition is that the parameter x is even after the function has returned.

    anyways, we arent being constructive and this is doing no good to this thread or forum.

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nadroj View Post
    dont worry i wont

    again, the functions purpose is obvious. a required side effect or postcondition is that the parameter x is even after the function has returned.

    anyways, we arent being constructive and this is doing no good to this thread or forum.
    I think the advice to name functions appropriate to their purpose does a great deal of good in general.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i agree (though ive never heard of that design paradigm before!).

    from the start, i said it was a silly example, but i just thought of something quick and on the spot to give the thread starter an example. if this were a real-world application, of course the function and postcondition would be documented in detail, so the design team doesnt assume anything incorrectly. it is possible the thread starter isnt even reading this thread anymore, however you or anyone of course can always give them different or better examples to help their understanding.

  6. #21
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by sharkbate24 View Post
    Hello Everyone.

    Just wondering, why would you use pointers (ie. Find the memory address of a variable etc)?
    lots of reasons. consider if you had an object whose data is used and represented in many different places. using a pointer to that object's data prevents you from having to update the values at each of the other locations.

    also, if you have a large object, assigning or passing it by value requires you to make a copy of that object which can be quite resource-intensive. a pointer is the same size as an int.

    judicious use of pointers can make your code more efficient, reliable, and reduce the total amount of work you have to do.

    Quote Originally Posted by sharkbate24 View Post
    Also, is there any chance that memory addresses change after (say...) closing the program and then restarting your computer, then reopening the program?
    yes, they will change.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nadroj
    here are 2 important uses of pointers:
    There would be a third, though it is related to the first: for the implementation of data structures that involve "links" of some sort, e.g., linked lists and trees. Also, the second is not as important in C++ due to the existence of reference types, even though C++ references are themselves typically implemented as const pointers.

    Quote Originally Posted by nadroj
    - thankfully, "by default", arrays and pointers are "passed-by-reference", where only a copy of the fixed-size pointer argument is passed, rather than the actual data structure.
    Actually, only arrays are "passed by reference" due to them decaying to a pointer to their first element, since the pointers themselves are passed by value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by nadroj View Post
    2) pass-by-reference
    - lets say we have some math program that has a 100 by 100 matrix of integers. we also have a function to search a given matrix for the value at a specific row-column pair. if the function used "pass-by-value" for the matrix parameter, it would involve (the computer automatically) making a copy of the 100*100*(size of integer, usually 4 bytes)=40000 byte matrix so it can be used in the function, then forgotten about after the search function has returned.
    - thankfully, "by default", arrays and pointers are "passed-by-reference", where only a copy of the fixed-size pointer argument is passed, rather than the actual data structure.
    - all primitive data types are passed by value, unless "told" otherwise. these are things such as char, int, float, etc.
    I am so happy to report that you are wrong!
    Since we are discussing C++, they are passed by pointer or by address, not reference.

    4 good reasons (most already stated) for using pointers (and/or references):
    - Dynamic memory (not so common in C++; usually std::vector is used).
    - Allow other functions to modify local variables (if they are not passed by pointer/reference, copies are made and thus the original values are not changed).
    - Avoid overhead of copying data (only the pointer would be duplicated).
    - Allow several places in the program to see the same data and be able to update it and make the changes reflect everywhere this data is used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    I am so happy to report that you are wrong!
    Since we are discussing C++, they are passed by pointer or by address, not reference.
    Look carefully at the quotes. We are talking about the computer science term, not the C++ term.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, this is C++, so pass-by-reference means using references. There I object to the term. I always have and this is even a better reason to object it. Avoids confusion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    We will not have this discussion again.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We will not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    This thread is full of hate.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM