Variables | Pointers in arguments really needed?

This is a discussion on Variables | Pointers in arguments really needed? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia Yes, the same analogy holds true with references. A reference can be a name, an alias ...

  1. #16
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    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.
    I understand. The code above seemed to take the object ( the reference ) and pass it through the pointer, thus it got the value.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When it comes to speed, pointers are references are essentially equal - a reference, when it gets compiled, gives identical code to the same solution using pointers [1]. The detail differences between pointers and references are mainly in what the compiler allows you to do and what you have to type into your source file to let it know what you want to have happen.

    So a reference and pointer (at least when we pass them to functions) will have the same benefits and drawbacks:
    1. Passing a pointer/reference passes a pointer-sized element.
    2. Dereferencing such a pointer/reference will incur some overhead compared to having the object directly available.


    [1] Assuming we're not looking for strange & unusual ways of using either pointers or references, and equal level of understanding from the compiler as to what the code tries to achieve - some compilers occasionally get confused by one or the other concept in some circumstances, but please ignore this for now, I'm just adding this stuff to prevent someone else from contradicting me with regards to details which will just confuse the matter vs. the general rule [Just like when we study physics at a basic level, everything works according to Newton's equations, but once we get deeper into physics, we learn about Einstein's relativity theory, which "break" Newton's rules].

    --
    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. #18
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by matsp View Post
    When it comes to speed, pointers are references are essentially equal - a reference, when it gets compiled, gives identical code to the same solution using pointers [1]. The detail differences between pointers and references are mainly in what the compiler allows you to do and what you have to type into your source file to let it know what you want to have happen.

    So a reference and pointer (at least when we pass them to functions) will have the same benefits and drawbacks:
    1. Passing a pointer/reference passes a pointer-sized element.
    2. Dereferencing such a pointer/reference will incur some overhead compared to having the object directly available.


    [1] Assuming we're not looking for strange & unusual ways of using either pointers or references, and equal level of understanding from the compiler as to what the code tries to achieve - some compilers occasionally get confused by one or the other concept in some circumstances, but please ignore this for now, I'm just adding this stuff to prevent someone else from contradicting me with regards to details which will just confuse the matter vs. the general rule [Just like when we study physics at a basic level, everything works according to Newton's equations, but once we get deeper into physics, we learn about Einstein's relativity theory, which "break" Newton's rules].

    --
    Mats

    Well it seems that way.

    It seems references must rely on pointers. soo...


    Off Topic:

    Why do they call you the kernel hacker? :P

    Ironic since the kernel handles the memory. xD
    Last edited by bobbelPoP; 08-04-2008 at 06:20 AM.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobbelPoP View Post
    Off Topic:
    Why do they call you the kernel hacker? :P

    Ironic since the kernel handles the memory. xD
    Well, technically it is myself that calls me a Kernel Hacker, but in the last many years, I have spent most of my time dealing with code that one way or another operates within or in close conjunction with OS kernels, working on Real-time OS's [at a commercial level at least three different ones], Linux and Windows. In Windows I worked for a graphics card producer, writing the Windows Driver for the graphics card [mostly dealing with Low-Level driver stuff, rather than the more interesting 3D side of things], in other work I have been working either for a processor manufacturer or the OS manufacturer.

    --
    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. #20
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by matsp View Post
    Well, technically it is myself that calls me a Kernel Hacker, but in the last many years, I have spent most of my time dealing with code that one way or another operates within or in close conjunction with OS kernels, working on Real-time OS's [at a commercial level at least three different ones], Linux and Windows. In Windows I worked for a graphics card producer, writing the Windows Driver for the graphics card [mostly dealing with Low-Level driver stuff, rather than the more interesting 3D side of things], in other work I have been working either for a processor manufacturer or the OS manufacturer.

    --
    Mats
    Awesome.

    I have messed with the windows kernel functions. :P Like WriteProcessMemory ( writes memory to a process ) and ReadProcessMemory ( reads the processes memory ) from the winapi ( msdn helps ) but that's about it. xD

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    They aren't Windows kernel functions...
    Kernel functions are those functions that can only be called from code running inside the kernel (like drivers).
    Or in other words: low level operating system functions.
    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. #22
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by Elysia View Post
    They aren't Windows kernel functions...
    Kernel functions are those functions that can only be called from code running inside the kernel (like drivers).
    Or in other words: low level operating system functions.
    kay. :P that is all.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    They aren't Windows kernel functions...
    Kernel functions are those functions that can only be called from code running inside the kernel (like drivers).
    Or in other words: low level operating system functions.
    Sort of correct - but I would call them Kernel API functions, and whilst ReadProcessMemory() in itself doesn't call directly into the kernel [it goes through a API translation layer], it will directly cause kernel code to run and perform the requested operation [if allowed].

    Some API calls may of course not enter the kernel at all - they may be part of the user-mode side of the API and fulfill the entire functionality in user-mode.

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

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Speed of your parameter passing will be negligible compared to actually running a database query.

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    Speed of your parameter passing will be negligible compared to actually running a database query.
    Yes, quite likely. But saving time [with zero loss of functionality] doesn't hurt, does it?

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

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    No, I'd just rather have my engineers focussed on optimizing their DB queries, caching of results, threading, etc. than trivial issues like this.

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    No, I'd just rather have my engineers focussed on optimizing their DB queries, caching of results, threading, etc. than trivial issues like this.
    Sure. Particularly "remembering" the result from a previous query rather than redoing it a few lines further on in the code can have dramatic effect on performance, compared to the "a few dozen clock-cycles" which you'd save on passing something with const reference.

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

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    No, I'd just rather have my engineers focussed on optimizing their DB queries, caching of results, threading, etc. than trivial issues like this.
    Sutter and Alexandrescu's advice to "don't pessimize prematurely" should be applied here: sure, there are larger gains to be had by optimising database queries and choosing appropriate indices, but appropriately choosing to use pass by reference instead of pass by value neither requires considerable thought nor results in code obfuscation, so it might as well be done.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from Laserlight (excellent) reply, I would also point out that missing the const reference on a rather long string that may actually be passed along SEVERAL TIMES before it reaches the actual database interface can have a noticeable effect on performance too - sure it's nothing like halving the number of DB queries for a whole application, but copying a kilobyte of query string say a dozen times between the user-interface-code and the actual query will be significantly slower than passing 4 bytes of reference a dozen times. Having the const reference there in the first place will prevent someone else from "messing up" the interface where the string is passed from one object to the next.

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

  15. #30
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I don't disagree with either reply, just hoping that the OP realizes the importance of profiling to find real bottlenecks, and that they're usually tied to the design, algorithms, etc.

Page 2 of 3 FirstFirst 123 LastLast
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, 11:53 AM
  5. Using pointers - asterisks needed ?
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-28-2002, 05:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21