Thread: Variables | Pointers in arguments really needed?

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    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

  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.

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