Thread: New to c++/c and dont understand the use of memory pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39
    Thanks 7stud, so basically its quicker than automatic garbage collection?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    8

    garbage collection

    It is faster but also more convenient. For example you don't want garbage collection when your program is in the middle of a time sensitive operation. I program in Visual Basic.net in addition to c++. VB.net garbage collection works alot like java garbage collection. The main point is that doing things manually is almost always better but people tend to put fixing memory leaks at the bottom of the list so to speak. So in languages like Java and .net they force "good" pratices which solves a number of issues but also has costs in the area of speed.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by disks86
    The main point is that doing things manually is almost always better
    Any programmer who believes this should be banned from ever writing anything more complicated then hello world.

    Doing things manually is almost always worse. Compilers are there to do work for you, not create work.

    This is why we have things like smart pointers.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I'd say the most common reasion to use pointers is to get-around the fact that a function can only return one value.

    And, like 7stud said, when you pass a variable into a function, you only pass it's value, not the variable itself. If you change a variable inside a function, you are NOT changing the original value. You are only changing the local copy, which may not be what you want!

    Most books introduce pointers with a swap(X,Y) function... You have two variables and you need to swap their values. Because of the above limitations, you cannot change two values without using pointers (or references*). With the addresses passed into the function, the function can affect more than one variable.

    The same is true with arrays. If you want your function to affect more than one element in the array, you need the address of the array.

    *In C++ (but not C) you can also use references. References are another way to "get-to" a variable outside of the function. References are generally preferred over pointers when, they can be used.

Popular pages Recent additions subscribe to a feed