newbie-functions vs reference

This is a discussion on newbie-functions vs reference within the C++ Programming forums, part of the General Programming Boards category; Hello, well I've been making my way through tutorials and such, and everything seems pretty clear so far except for ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    newbie-functions vs reference

    Hello, well I've been making my way through tutorials and such, and everything seems pretty clear so far except for one thing-pointers and references. Am i just tired? are they really as complicated as they sound? do they sort them selves out later? i heard stuff about "linked-lists"?

    I also wondering, when do i use a pointer? So far the only uses i know for it are pointing to a variable, pointing to an object, and using a pointer to point to a variable from a function. SO do i always use a pointer? sometimes? i assume good programmers DO use them? any comments/opinions would be appreciated

    Chris

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    They are an important part of C++ and you will see later why they are needed. For now, just reread the chapter and try to understand them. As for the explaining part, I am not to good at that so hopefully someone else can help you there.

  3. #3
    UNBANNED OneStiffRod's Avatar
    Join Date
    Jan 2002
    Posts
    669
    To understand pointers and references you need to understand how a variable is transported. When you pass a variable to a function through an argument that variable is copied so you end up having 2 copies of that var in memory, however, only for a short period of time as the other copy only lasts as long as the function scope.

    But when you reference (&) a variable no copy is made so you are directly accessing the variable, this is more efficient but is succeptable to having the var corrupted if your function or program errors.

    When you use a pointer(*) you are using the address of the variable, it's physical location in memory, and to get at the data within you must de-reference the pointer to change the value of it. This is safer than referencing(&) but since it has to be de-referenced to get to the data is less efficient.

    It's hard to come up with a rule of thumb when using any of them but I generally use a reference in my function arguments when I have GLOBAL vars or Var heavy classes, or predefined Class objects. For my User Defined Classes, when I have to create a variable or use it as a function argument I use a pointer.

    Keep reading until you get to Classes, then pointers and dereferencing will make more sense to you.

    A linked-list is like an array only it can expand and grow, you can add or remove from a linked-list to make it as large or small as you need. It uses pointers to point to the next and previous instances of the elements. It can be thought of like a Chain(link) with each link in the chain having a reference to where the next and previous links are, this way it can grow to any size suited at runtime. Using arrays forces you to predefine the size of the array you need.

    Using pointers and referencing becomes important when you program large applications and also heavy processing apps like video games. The more memory you can conserve for more important processes the better, this way you don't have unnecessary instances of vars and Classes floating in memory at the same time.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. A general quaetion on portability
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-20-2006, 03:48 AM
  3. problem with the library
    By kris.c in forum C Programming
    Replies: 21
    Last Post: 07-10-2006, 08:29 PM
  4. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM
  5. compiler problem
    By sofarsoclose in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 11:39 AM

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