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

This is a discussion on New to c++/c and dont understand the use of memory pointers within the C++ Programming forums, part of the General Programming Boards category; I have read 3 or 4 times how to use memory pointers but comming from a BASIC/JAVA back ground I ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    39

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

    I have read 3 or 4 times how to use memory pointers but comming from a BASIC/JAVA back ground I have no idea why they are used.

    I can understand perfectly HOW they are used but no basic c++ books/tutorials have given a reason WHY they are used.

    They only reason I have got is that in the earlier days of programming dirrect memory managment saved a few instructions making a program execute faster. Is this still true today? And is there any other reasons and how does this have an advantage over how JAVA for example manages memory?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    You have total control of how much memory you can will use, you have to clean up the memory yourself afterwards.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I can understand perfectly HOW they are used but no basic c++ books/tutorials have given a reason WHY they are used.
    When you call a function, the function makes a copy of whatever arguments you send it. For instance, if you send an array of size 100,000 to a function, the function has to copy the whole array for the function. That is inefficient. However, if you send the function a memory address of where the data is located, the function does not have to make a copy of the all the values in the array. Instead, the function makes a copy of the address(which is of minimal size), and that is more efficient than copying the whole array--it requires less memory and it's faster.

    In C++, arrays are automatically passed by address to avoid that copying. So when you send the name of an array to a function, you are really sending the function an address of where the data is located. However, in C++ when you send objects of a class to a function, the objects are copied. That is different from Java. In Java, when you send an object to a function you send the function a reference to an object. A reference in Java is actually the address in memory where the object is located. In Java, you cannot pass an object to a function and have the function copy the object--as will happen in C++. In C++, you have to use a pointer or a reference type to avoid copying when you send objects to functions.

    Java also uses garbage collection to destroy objects automatically. Garbage collection flags objects that are no longer being used as candidates for destruction. The timing of when they get destroyed is not up to the programmer--the garbage collection facility determines that. In C++, objects created using 'new' stay in existence until they are deleted by the programmer, and when they are deleted by the programmer, they are immediately destroyed. However, if the programmer loses a reference to an object created with 'new', then there is no way to programmatically destroy it anymore, and it creates a memory leak. If your program creates enough leaks it can eat up all the system memory and cause the program to crash.

    The advantage of C++'s memory management is that it can be much, much faster than Java's.
    Last edited by 7stud; 12-16-2005 at 01:54 PM.

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

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

  6. #6
    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?

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

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