Results 1 to 10 of 10

Thread: newbie to pointers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    UK
    Posts
    164

    newbie to pointers

    Hi I am relativly new to C++ and wonder if someone can explain the concept of pointers (which I sort of understand) and why they are so useful.

    Sorry if this seems like a dumb question,

    Thanks,

    Alex
    ASP, SQL, VB6, Java Script and dubious guitar playing skills.

  2. #2
    A pointer is a variable that doesn't store objects but rather a location in memory. For example (and this might be wrong, I haven't done C++ pointers for a year):
    Code:
    SomeClass *x;
    x = new SomeClass();
    x->SomeMethod();
    delete x;
    That declares a pointer, allocates space for it to hold SomeClass, calls the pointer's value's SomeMethod method, and frees up memory by deallocating memory for the pointer.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    UK
    Posts
    164
    Why would you want to use a pointer?

    Thanks,

    Alex
    ASP, SQL, VB6, Java Script and dubious guitar playing skills.

  4. #4
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106

    Assume

    Assume that you have a string "Hello World". You want to pass this to a function UCase().

    Instead of passing the whole string you can pass a pointer to the string. When you pass a pointer to a string, the pointer contains the address on the first char (H in this case). The function will read each char until it reaches the Null terminator (Used in C/C++ to determine the end of a string in memory)

    As a further note. VB always passes a string as a pointer even when you use ByVal.

    Hope you understand the above, if not I will clarify for you.
    ****

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Stuff you have to read:
    http://newdata.box.sk/bx/c/htm/ch08.htm
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    UK
    Posts
    164
    Thanks for the replies.

    That link Kedaman is good - this is a detailed and difficult subject for me, I think it will take a couple of read throughs!

    FantastichenEin can you not just pass the string to the function rather than passing the pointer to it? Is it more efficent passing the pointer?

    I think the article kedaman sent said that pointers are sometimes used to allow you to access variables outside a functions scope but surely there are other ways around this like static?

    In VB to change a value that is passed to the function you use the by ref, are pointers c++ way of doing this? Surely you could just assign the value returned from the function?

    Sorry dont know if I am making much sense!

    Thanks for help,

    Alex
    ASP, SQL, VB6, Java Script and dubious guitar playing skills.

  7. #7
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    I don't think it is possible in C++ to pass a string to a function (I may be wrong about this).

    It is more efficient to pass a pointer as a pointer is only a Long whereas a string is Number of chars.

    When using byRef Vb just passes a pointer to the string.
    In VB to change a value that is passed to the function you use the by ref, are pointers c++ way of doing this? Yes

    When using ByVal in vb, Vb first copies the string to a new memory location, and then passes to address of the new location. This means that the origional string cannot be modified by the function
    ****

  8. #8
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    I don't think it is possible in C++ to pass a string to a function (I may be wrong about this).
    Because it's not really a string, but an array of characters, so you pass a pointer to the first char in the array.

    Another use a pointers and strings is this:

    Suppose you have an array of characters that represent the a file name and its full path. ("C:\folder\folder2\file.xml"). Suppose you want just the short file name ("file.xml"). You could use a pointer to the 'f' char in the file name.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Pointers make passing arrays very simple:
    Code:
    void test(int arr[], int size)
    {
        for(int i = 0; i < size; i++)
        {
            arr[i] = i;
        }
    }
    
    // now you have no way of passing the array back to the calling
    // function.  You can't do "return arr"
    // if you call test() from main and pass it an array,
    // then the array will be unchanged after the function
    // returns.
    // you also can't declare:  void test(int &arr[], int size);
    
    // but with pointers:
    
    void main()
    {
        int * array = new int[5];
        test(array, 5);
    
        // now array = {0, 1, 2, 3, 4}
        
        delete[] array;
    }
    You ALWAYS need to delete pointers after you use them,
    otherwise you have allocated memory that never gets
    deallocated causing a memory leak.

    /

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Although, any good OS should clean itself up after running every program...this basically means that memory leaks are only critical if your program will be run for a long period of time.

    For example, under Windows:
    Code:
    int main() {
        int *x = new int[5000];
    
        return 0;
    }
    ...is fine, and the memory will be freed when the RTL winds up the call stack.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width