Thread: Variables | Pointers in arguments really needed?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Variables | Pointers in arguments really needed?

    Code:
    
          class Database {
     public:
     std::string Query;
     void SendQuery(std::string,&Database); // Would this be faster?
     //Reminds me of like a wildcard variable/pointer/reference thing
    
     // or
    
     void SendQuery( std::string Query, Database& db ); // or would this be faster?
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure what your two choices really represent. The actual code in alternative 1 is incorect:
    Code:
    void SendQuery(std::string,&Database);
    , you probably mean:
    Code:
    void SendQuery(std::string,Database&);
    If my "probably mean" is what you actually mean, then this:
    Code:
    void SendQuery( std::string Query, Database& db );
    is exactly the same - the only difference is that you give anyone (including yourself) reading the function prototype a clue as to what the string (and to some extent) argument(s) are referring to.

    There will be absolutely no difference in speed whether you mention the parameter name or not, nor will the compiler produce any different code if you put the same or different names for the parameters in different places where they are mentioned [but it helps us humans that have a more complex associative memory than the compiler to understand that we really mean the same thing if it's called the same thing across different points].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by matsp View Post
    Not sure what your two choices really represent. The actual code in alternative 1 is incorect:
    Code:
    void SendQuery(std::string,&Database);
    , you probably mean:
    Code:
    void SendQuery(std::string,Database&);
    If my "probably mean" is what you actually mean, then this:
    Code:
    void SendQuery( std::string Query, Database& db );
    is exactly the same - the only difference is that you give anyone (including yourself) reading the function prototype a clue as to what the string (and to some extent) argument(s) are referring to.

    There will be absolutely no difference in speed whether you mention the parameter name or not, nor will the compiler produce any different code if you put the same or different names for the parameters in different places where they are mentioned [but it helps us humans that have a more complex associative memory than the compiler to understand that we really mean the same thing if it's called the same thing across different points].

    --
    Mats
    Thanks man, and by the way, I didn't know where to put the reference symbol there because there was no variable name ( as there is in the second example ). :]

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, and if you don't change the Query within SendQuery, make it
    Code:
    void SendQuery(const std::string& Query, Database& db );
    That way, when the compiler calls SendQuery, it doesn't have to make a copy of the Query string itself[and it that query is anything like some of the longer ones I've made in my life, that could save quite a lot of time, as it could possibly get into the kilobytes range].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by matsp View Post
    Oh, and if you don't change the Query within SendQuery, make it
    Code:
    void SendQuery(const std::string& Query, Database& db );
    That way, when the compiler calls SendQuery, it doesn't have to make a copy of the Query string itself[and it that query is anything like some of the longer ones I've made in my life, that could save quite a lot of time, as it could possibly get into the kilobytes range].

    --
    Mats
    Thanks. I still have to get use to const's. ;P although they remind me of REAL constants ( #define CONSTANT ) :P I'm from PHP sorry ( Although in advanced php programming, you can still make a constant member like so: CONST myConstant = "You have to initialize constants in oop php no matter, otherwise it gives you a error", that's the only known keyword I know from PHP that uses const so )

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The pointer symbol (*) and the reference symbol (&) is placed after the type.
    So if the type is int, it becomes int* or int&.
    If the type is Database, it becomes Database* or Database&.
    If the type is std::string, it becomes std::string* or std::string&.

    Simple enough.
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    The pointer symbol (*) and the reference symbol (&) is placed after the type.
    So if the type is int, it becomes int* or int&.
    If the type is Database, it becomes Database* or Database&.
    If the type is std::string, it becomes std::string* or std::string&.

    Simple enough.
    -_- Well I am a bit confounded about Pointers still. I mean, I feel like no one would give me a good answer to them, otherwise references are easy to understand, they just get the object of the variable.

    It's like you need to experience pointers to understand them profoundly.
    Last edited by bobbelPoP; 08-04-2008 at 05:23 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    References are typically good for arguments to functions, but as class members, for example, their usefulness drops significantly because they cannot be reassigned.
    A good thing to try to strive for to learn: use references whenever you can. If you can't, then use pointers. Try it and you'll find situations where pointers are the only solution.

    As for understanding how they work, that's the work of books and tutorials.
    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. #9
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    References are typically good for arguments to functions, but as class members, for example, their usefulness drops significantly because they cannot be reassigned.
    A good thing to try to strive for to learn: use references whenever you can. If you can't, then use pointers. Try it and you'll find situations where pointers are the only solution.

    As for understanding how they work, that's the work of books and tutorials.
    Hmm, well someone ( forgot) in that other thread "Specifically need to use pointers .. etc" said that the value of the variable is really the only memory, so it's like if you have a pointer, and that variable doesn't have a value already placed, then it's forced as memory allocation? hmm Would that be a reason for pointers?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you would really like a detailed explanation of pointers, you can look to these references:
    http://www.eternallyconfuzzled.com/t..._pointers.aspx
    http://cpwiki.sf.net/Pointers

    Pointers are useful for memory allocation, yes. Since pointers hold a memory address, you can keep track of, and browse, a block of memory. Examples of memory allocation would be strings, or vectors of objects, which definitely employ pointers to store data.
    Last edited by whiteflags; 08-04-2008 at 05:41 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A pointer is not special in any sense. It's just a variable, just like all other variable types which are non-pointers. So in that sense, pointers do take memory, just like any variable.
    The only "special" thing about pointers is that they store memory addresses. But that is all they do. They do no allocate memory in any way (contrary to many beliefs).
    Therefore, defining a variable consumes about 4 bytes of memory (on the stack). However, what the pointer points to is not guaranteed to be owned memory (allocated memory). Therefore you have to either point the pointer to an existing variable, or allocate some memory using new. Then your point is valid.
    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.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by citizen View Post
    If you would really like a detailed explanation of pointers, you can look to these references:
    http://www.eternallyconfuzzled.com/t..._pointers.aspx
    http://cpwiki.sf.net/Pointers

    Pointers are useful for memory allocation, yes. Since pointers hold a memory address, you can keep track of, and browse, a block of memory. Examples of memory allocation would be strings, or vectors of objects, which definitely employ pointers to store data.
    Oh man this is awesome, very helpful citizen!

    Thank You!

    I wish we still had that thanks button. I have the urge to so badly.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    141
    Code:
    int main()
    {
    	int test = 100;
    	int t = *(&test);
    	std:: cout << t << std:: endl;
    
    }
    I did this, and it worked. so hmm.. From what Elysia said and this, I'm starting to become comfortable with them.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    141
    By the way, is the reason you say Pointers are good for speed and such is because they're just storing the addresses of which hold the indirected value? Thus the memory address holds all that data, and you just have to reference it to get the data from it? Neat.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, the same analogy holds true with references. A reference can be a name, an alias for something, and when passed to another function, the name or alias is passed, and not the actual data.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write functions with pointers variables
    By noob programmer in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:48 AM
  2. Function Pointers and Arguments
    By mike_g in forum C Programming
    Replies: 2
    Last Post: 04-08-2008, 04:39 AM
  3. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  4. pointers and variables
    By dharh in forum C Programming
    Replies: 4
    Last Post: 02-05-2002, 12:53 PM
  5. Using pointers - asterisks needed ?
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-28-2002, 06:56 PM