Pointers... Geez

This is a discussion on Pointers... Geez within the C++ Programming forums, part of the General Programming Boards category; I'm a newbie so don't yell. I'm advancing up to Pointers now. And it is so confusing. I know pointers ...

  1. #1
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24

    Pointers... Geez

    I'm a newbie so don't yell. I'm advancing up to Pointers now. And it is so confusing. I know pointers have something to do with memory addressing, but thats about it. Can anyone help me out? Just to make this easier to understand.
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    A pointer is a type of variable that contains a memory adress. That memory adress refers to a block of memory. Consider these examples:
    Code:
    char c;
    char* pc = &c;
    
    int i;
    int* pi=&i;
    
    void* pv= malloc(100);
    When I declare 'c', c will be a variable of type char, or in other words, will be 1 byte big (traditionaly, but can be different). When I declare pc, that variable will be a pointer to that character. The block of memory that pc points to will, then, be 1 byte big.
    When I declare 'i', i will be 4 bytes big (traditionaly, again...), and pi will point to a 4 bytes memory block.
    Now that you're thinking in memory blocks, what about assigning a block of 100 bytes to a pointer? That's done with the call to malloc, which return a pointer, and it's stored in pv.
    Now why void*? Or why int*? Or why char*?
    The difference bettween these types is the way there treated, because there all (traditionally) 32 bits long, or in other words, contain a memory adress of 32 bits.
    In that adress one will find the block of memory with something stored in it.
    The types are used to when de-referencing the pointer, getting something from the pointed memory block.
    If you have a memory block at 0x001000, acessing to that memory with ptr[0], ptr[1], ptr[2], being ptr a pointer to char ( char* ), would match, respectively, reading memory from position 0x001000, 0x001001, 0x001002, because chars are 1 byte big. But if the pointer is to ints acessing ptr[0], ptr[1], ptr[2] would result int acessing 0x001000, 0x001004, 0x001008, because ints are 4 bytes big!
    And doing this
    Code:
    int *ptr=.../*something*/;
    int i= *ptr
    will read 4 bytes from that memory position and assign then to the variable. If you're using chars it would be read only 1 byte.

    IMPORTANT NOTE: I used 32 bits for size as example, but these may change due to computer achitecture, or compiler implementation. Variables size, or memory adresses size aren't defined by any programming standard. Only by hardware's. The same for int and char sizes! These also vary.

    Now look at a little example
    Code:
    #include<iostream>
    int main(){
        int my_int = 0x002b2b43;
        char *my_ptr_char1 = (char*)&my_int;
        char *my_ptr_char2 = "C++";
    
        std::cout << "The memory from the int var: "<<my_ptr_char1<<"\n";
        std::cout << "The declared string: "<<my_ptr_char2<<"\n";
        return 0;
    }
    Hope this clears a bit

  4. #4
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24
    I kinda get it. Pointers are like arrows that points to how much memory a variable allocates? like if I declare a "short" variable, the pointer will carry its memory slot?
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    I kinda get it. Pointers are like arrows that points to how much memory a variable allocates
    Yes, if you assign a pointer to a memory adress of a variable. If instead you allocate size of n bytes to that pointer, the pointer then will not only refer to a single variable, but a block of memory of several aligned and consecutive variables
    like if I declare a "short" variable, the pointer will carry its memory slot?
    I didn't get the short part.. If you declare a variable, that variable will be somewhere in your memory. That variable will contain it's value, and will have also a constant memory adress. Then you can read that adress with the '&' operator and store it in a pointer to manipulate it however you may want. A pointer is nothing more than a variable, which content is a memory adress of another variable.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    int contains an integer

    double contains a floating point value

    string contains letters

    bools contain true/false

    pointers contain &addresses
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24
    I kinda get it now. Malloc is a pointer that points to a block with 100 bytes right? and whenever I wanna delete a pointer just enter this "delete ptr"?
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  8. #8
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    NOOOOOOOOOOOOO!!!
    malloc is a function that returns a block of n bytes, being n the parameter you send.
    So
    Code:
    int *pi = malloc(4);
    char *pc = malloc(1);
    char *pc = malloc(34);
    void *pi = malloc(3456);
    So we'll have respectively, blocks of 4 bytes, 1 yte, 34 bytes and 3456 bytes...
    AND your learning C++. Memory allocations in C++ are done with the new operator. And pointers are deleted with the delete operator. This operator frees the memory previously allocated.
    So the above code could be
    Code:
    int *pi = new int[1];
    char *pc = new char[1];
    char *pc = new char[34];
    void *pi = new char[3456];
    malloc is only a function, such as calloc, which allocate memory. These functions are from the C standard, not C++, but I used them only as example.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    While malloc and calloc are supported in C++ (and of course free() too), as xErath has pointed out the preferred method of allocating extended blocks of memory is using the new keyword, and the C++ equivalent of free() is delete[] if you want to get rid of a block of memory. Be careful not to mix malloc() with delete[], and same goes for new and free() - they're not compatible.

    Code:
    char* c = new char[100];  //allocate enough memory to hold 100 char's
    char* d = new char[200];  //allocate enough memory to hold 200 char's
    int* e = new int[100];  //allocate enough memory to hold 100 int's
    
    delete[] c;  //Be sure to free any memory that you allocated earlier!
    delete[] d;
    delete[] e;
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Lonewolf
    Join Date
    Nov 2004
    Location
    St. Louis, MO
    Posts
    24
    Oh okay. Pointers just allocates memory for variables. I get it now. Thanks alot guys!
    COMPILER: Borland 5 ( trying to get MV C++ ); LANGUAGE(s): English, Vietnamese, and basic German;
    Current IDE: Dev C++ 5 (Beta)

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Pointers just allocates memory for variables.
    Ahhhhh nononono, pointers just point to locations in memory. new and [b]delete[] are keywords that CREATE blocks of memory (or delete), and then give you the memory address of that block of memory so that you can then store the ADDRESS of the memory in a pointer.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The pointer itself is just an address (nothing more). The new operator allocates memory of the given size, initializing the data block if appropriate, and simply returns the address of the newly created object (or first of the newly created objects in the case of new[]).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Code:
    char text[]="happiness";
    when the compiler stores data it picks a location in memory for simplicity the text[] array starts at 0000;
    Address Data
    |0000 | h |
    |0001 | a |
    |0002 | p |
    |0003 | p |
    |0004 | i |
    |0005 | n |
    |0006 | e |
    |0007 | s |
    |0008 | s |
    |0009 | ! |
    |000A | \0 |
    |000B | ? | <---our pointer

    When you create a pointer the computer reserves space in memory to store it. our pointer
    is located at 000B It could be anywhere but to make it simple we put it here. It contains
    undefined data yet we haven't initialized it. Now what we put in the data field would be
    the address of one of the other variables so we would put address 0000 to get to the data
    h:
    |000B |0000| h |
    Now to access the next letter we simply increment the pointer by the size of the type
    (char=1byte) so we add 1 to the address stored in our pointer it now points to a;
    000B| 0001| a |


    if we were working with an integer array (integers are 4bytes) then the address would be
    multiples of 4
    Code:
    int numberarray[5]={10,300,40,64,27};
    Address Data
    |0000 | 10 |
    |0004 | 300 |
    |0008 | 40 |
    |000F | 64 |
    |0014 | 27 |
    |000B | ? | <---our pointer

    The computer handles the addressing for different types(int char) automatically so you
    don't have to worry about it all you do is increment the pointer by 1 and it figures out the
    rest.

    Whatever address we store in the pointer the data located at the address is what we are
    able to access.
    [code]
    char text[]="happiness";
    char*charptr=text; <--- this is saying put the address
    of the start of text[] array in the pointer charptr;

    char *charptr;

    charptr=&text[0]; <--- this is also saying put the address of the first element of text[]
    array in the pointer charptr;

    char*charptr=text;

    and

    char *charptr;
    charptr=&text[0];

    are equalivlent statements they accomplish the same thing. It is just a matter of how you
    want to look at it.

    The first one is saying set the pointer to point to the array the other is saying set the
    pointer to point to the first element in the array. The first one is treateing is like a block of
    memory and will always point to the first element, and the second a single memory location
    that just happens to be the first one in the array..

    The second method allows you to point directly to a specific element inside the array.

    so you use the & to make it point to the data structure( ie variable, array, class, function etc).
    now to get the data u use the * to access the actual data stored at the address stored in your pointer.

    Code:
    cout<<*charptr<<"\n"; would print the letter
    cout<<charptr<<"\n"; would print the address of the letter.
     
    *charptr='g'; would change the data from a to g
    Last edited by manofsteel972; 11-14-2004 at 07:42 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  14. #14
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by Kai_Legends
    I'm a newbie so don't yell. I'm advancing up to Pointers now. And it is so confusing. I know pointers have something to do with memory addressing, but thats about it. Can anyone help me out? Just to make this easier to understand.
    I won't yell. but aside from all the help you've received on the board, you should also learn the board is a
    resource itself.. pointers are usally the most confusing ascpect of C programming. I'd assume C++ as well, so don't feel bad, even vetrans its said get confused on it sometimes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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