Negative array indexes

This is a discussion on Negative array indexes within the C++ Programming forums, part of the General Programming Boards category; I'm trying to translate some pseudocode that starts indexing at -1. Will the following code work like I expect it ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Negative array indexes

    I'm trying to translate some pseudocode that starts indexing at -1. Will the following code work like I expect it to?

    Code:
    int main() {
        int* a = new int[10];
        a = &a[1];
        a[-1] = 4;
        delete (a - 1);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Assuming you don't use the same letter twice. (I.e., you could do int *b = &a[1] and go from there.)

    (ETA: I should say, you might get warnings from your compiler (I think for example gcc warns on indices it can guarantee are always negative), but since your pointer, if used correctly, will always refer to valid memory everything is defined.)

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    More over, why would you want to do that? There might be data there that you don't want to mess with and could end up with errors or corruption.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by computerquip View Post
    More over, why would you want to do that? There might be data there that you don't want to mess with and could end up with errors or corruption.
    Look again at what he's doing here:
    Code:
    a = &a[1];
    Now his array indexes go from -1 to 8 instead of 0 to 9.

    Don't know why anyone would want to do that though.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by cpjust View Post
    Don't know why anyone would want to do that though.
    If you are implementing a lookup table that maps values in the range [x, y], then you don't want to subtract x from each lookup -- it's more efficient to just adjust the base pointer such that the x'th element coincides with the zero'th element of the actual array.

    Of course, merely possessing an out-of-bounds pointer is technically undefined, but not something I'd worry about in practice.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01:44 AM

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