Thread: Pointer Dynamics

  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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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...):
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM