Pointer Dynamics

This is a discussion on Pointer Dynamics within the C++ Programming forums, part of the General Programming Boards category; I am using Turbo c++ compiler i created an char pointer line this char *variable; variable=new char; and i was ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Pointer Dynamics

    I am using Turbo c++ compiler i created an char pointer line this



    char *variable;
    variable=new char;




    and i was able to input and get back the data like an array say


    variable[0]
    variable[1]
    variable[2]
    variable[3]
    .
    .
    .
    .
    variable[n-1]
    variable[n]



    So this is working like an dynamic array.. but why is this... i shouldnt be able tyo do this right.. since variable is a char and not a char array...... please help..

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Yes, it will. You are accessing the memory after the address of the char you created.

    This is bad. When you write variable[2] you are writing into memory which doesn't belong to variable, which comprises only one byte. While C won't stop you from doing it, believe me you don't want to do.

    Here's what you should do:

    // Declare & create 21 bytes on heap
    char* variable = new char[21];


    N.B. variable now points to the first character
    I.e. variable = &variable[0]

    Now you can safely write in the array up to and including position 20.

    Don't forget to destroy the array when you're finished. Like so

    delete[] variable;

    The double square brackets indicate to the compiler that the pointer points to an array.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    The operating system "should" stop you from moving into non-program memory, but since you were accessing your own stack, it was undetected. However, when I did the same thing with windows, (using a const far char * for maximum effect) I was allowed to roll through and print the integral values of each byte several megabytes in depth, so I suppose that safeguard is only as good as the OS implementing it BTW I would love to see someones result on another OS (Unix, maybe?)...


    int main(int argc, char *argv[])
    {

    unsigned const char protected_memory[10] = {0};

    unsigned const char far* next;

    next = protected_memory;

    while(next){

    printf("Address: %i \nValue: %i\nChar Rep: %c\n\n",
    (int)next, *next, (*next > 127 || *next == 0) ? '-' : *next);

    Sleep(500); //...replace with your OS's version...

    next++;
    }

    return 0; //..not!
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    I just realized that printing the values causes the program to fault quicker. You might try printing the addresses for a while before printing the values in order to attempt to "sneak past" the OS...here's a great controllable version of the above program (for Windows machines...):
    Attached Files Attached Files
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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