Dynamic Arrays

This is a discussion on Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; I was playing with ripper's code and if I change it so that it outputs the address instead of the ...

  1. #16
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    I was playing with ripper's code and if I change it so that it outputs the address instead of the value by taking the * off of pIncre, then pIncre++ increments the address by 4,


    Value pInt[0] is 007D0C70
    Value pInt[1] is 007D0C74
    Value pInt[2] is 007D0C78
    Value pInt[3] is 007D0C7C
    Value pInt[4] is 007D0C80
    Value pInt[5] is 007D0C84
    Value pInt[6] is 007D0C88
    Value pInt[7] is 007D0C8C
    Value pInt[8] is 007D0C90
    Value pInt[9] is 007D0C94
    Press any key to continue

    Why is this?
    How does it process arithmetic operations on a pointer?


    I want to make sure I've got pointers straight, cause they are weird to me. For example:

    int *pointerName;

    My understanding of a pointer was that the "int" is related to the type of data that is in the memory location. And the "*" says that pointerName is a variable that can store a alphanum value such as "007D0C70".
    , and you use the * operator to evaluate it as the value at that memory address like:
    int x=5;
    cout<<*&x;//outputs 5



    PS. Is each element taking up four addresses?
    How much memory is in an address, a byte?
    Last edited by WebSnozz; 10-04-2001 at 12:37 AM.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #17
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Each int is 4 bytes.

  3. #18
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    Ok, that's kinda what I thought.
    But I'd think pIncre++ would take the memory address and add 1 to it. How does the ++ operator know to add 4 instead of 1?
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  4. #19
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    When you add a value to a pointer in C++, you actually adds the value times the size of the datatype the pointer points at. If you want to add one to a pointer you can cast it like this:

    int *pnt = new int;
    pnt = (int*)(((char*)pnt)+1)

    This is one of the reasons I like assembler better than C++, it's more logical and you see exactly what is happening.
    // Gliptic

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 12:32 PM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 08:34 PM

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