Table with negative index.

Discussion in 'C++' started by klays@o2.pl, Apr 18, 2008.

  1. Guest

    Hi all,

    I have created char array, when I use tab[-10] compiler shouts out:
    error: size of array 'tab' is negative

    I tried to cheat compiler and a little modify this code.

    Look at the code:

    void check(int lenght, int index) {
    char tab[lenght];

    tab[index] = 'a';
    printf("Character: %c, Sizeof: %d", tab[index], sizeof(tab));
    }

    Call:
    check(-10, -2);

    Output:
    Character: a, Sizeof: -10

    My questions are:
    answer = Is it correct initializing array?

    if (YES == answer) {

    Why do compiler allow to this instruction?
    }
    else
    {
    Why not ?
    }

    Please, explain me it.

    Thanks in advance
    Klays
    , Apr 18, 2008
    #1
    1. Advertising

  2. writes:

    > Hi all,
    >
    > I have created char array, when I use tab[-10] compiler shouts out:
    > error: size of array 'tab' is negative
    >
    > I tried to cheat compiler and a little modify this code.
    >
    > Look at the code:
    >
    > void check(int lenght, int index) {
    > char tab[lenght];


    This will initialize the array at run-time.

    > tab[index] = 'a';
    > printf("Character: %c, Sizeof: %d", tab[index], sizeof(tab));
    > }
    >
    > Call:
    > check(-10, -2);
    >
    > Output:
    > Character: a, Sizeof: -10
    >
    > My questions are:
    > answer = Is it correct initializing array?


    No.

    > if (YES == answer) {
    >
    > Why do compiler allow to this instruction?
    > }
    > else
    > {
    > Why not ?
    > }
    >
    > Please, explain me it.


    Arrays cannot have negative size.

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Apr 18, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 18, 2:05 pm, Pete Becker <> wrote:
    > On 2008-04-18 05:45:09 -0400, said:
    >
    >
    >
    > > Look at the code:

    >
    > > void check(int lenght, int index) {
    > > char tab[lenght];

    >
    > This is not legal in C++. The size of an array has to be a compile-time
    > constant. You'll have to ask your compiler vendor what it means.
    >
    > --
    > Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    > Standard C++ Library Extensions: a Tutorial and Reference
    > (www.petebecker.com/tr1book)


    Hi Pete,

    I have tried to compile this code for gcc/g++ version 4.1.1 and I saw
    that compiler does not raise objection for g++. Maybe I should set
    some compiler flags so that compile clear C++. The second issue is:
    how tab array is placed in memory for positive and negative size of
    tab ( i.e tab[-6] and tab[6]).

    Regards
    Klays
    , May 7, 2008
    #3
  4. James Kanze Guest

    On May 7, 1:54 pm, wrote:
    > On Apr 18, 2:05 pm, Pete Becker <> wrote:
    > > On 2008-04-18 05:45:09 -0400, said:


    > > > Look at the code:


    > > > void check(int lenght, int index) {
    > > > char tab[lenght];


    > > This is not legal in C++. The size of an array has to be a
    > > compile-time constant. You'll have to ask your compiler
    > > vendor what it means.


    > I have tried to compile this code for gcc/g++ version 4.1.1
    > and I saw that compiler does not raise objection for g++.
    > Maybe I should set some compiler flags so that compile clear
    > C++.


    Like most compilers, g++ doesn't compile C++ (only something
    vaguely similar) unless you give it special options. In the
    case of g++, -std=c++98 -pedantic are necessary, at least.

    > The second issue is: how tab array is placed in memory for
    > positive and negative size of tab ( i.e tab[-6] and tab[6]).


    The size is an size_t, which is an unsigned type, so a
    declaration like "tab[ -6 ]" is going to be one mighty big
    array. (Since the memory actually used is on the stack, it's
    hard to imagine it not overflowing the stack.)

    In C++, of course, the expression must be an integral constant >
    0, or the compiler should complain.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 9, 2008
    #4
  5. Jim Langston Guest

    wrote:
    > Hi all,
    >
    > I have created char array, when I use tab[-10] compiler shouts out:
    > error: size of array 'tab' is negative
    >
    > I tried to cheat compiler and a little modify this code.
    >
    > Look at the code:


    I'm guessing here, since most of this is undefined behavior and is a
    compiler extention.

    > void check(int lenght, int index) {
    > char tab[lenght];


    The parameter for an array should be unsigned. You are passing it a signed
    value (int). It may be that it is silently converted to unsigned.

    > tab[index] = 'a';
    > printf("Character: %c, Sizeof: %d", tab[index], sizeof(tab));
    > }
    >
    > Call:
    > check(-10, -2);


    Most likely this is not producing an array of -10 entries, but 4294967286
    entries or so (my math might be off). Whatever -10 is in 2's complement
    converted to a signed int. Then it is not checking array element -2 but
    4294967294 or so. Your printf is treating the output of sizeof as a signed
    int, but it actually produces a size_t which is unsigned int.

    > Output:
    > Character: a, Sizeof: -10
    >
    > My questions are:
    > answer = Is it correct initializing array?


    Define "correct". If you mean a very large array because of the conversion
    from signed to unsigned, maybe. Again, it depends on your definition of
    "correct".

    > if (YES == answer) {
    >
    > Why do compiler allow to this instruction?


    Because it is a compiler extention and you are passing a signed value to
    something that expects an unsgined value.

    > }
    > else
    > {
    > Why not ?
    > }



    --
    Jim Langston
    Jim Langston, May 9, 2008
    #5
  6. Guest

    Hello,

    You are right. It is really unsigned int type (size_t type)
    when we use -std=c++98 -pedantic compiler flag then compiler
    raise the alarm. Thanks for detail explanation.

    Regards,
    Klays
    , May 10, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem_eda
    Replies:
    5
    Views:
    7,812
    Pieter Hulshoff
    Oct 11, 2004
  2. karunakar
    Replies:
    0
    Views:
    5,803
    karunakar
    Feb 16, 2005
  3. Pat
    Replies:
    0
    Views:
    10,745
  4. Mantorok Redgormor

    arrays: negative index values

    Mantorok Redgormor, Sep 18, 2003, in forum: C Programming
    Replies:
    2
    Views:
    464
    Richard Bos
    Sep 18, 2003
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    266
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page