Initialization of const objects.

Discussion in 'C++' started by zouyongbin, Nov 7, 2006.

  1. zouyongbin

    zouyongbin Guest

    The code segment like below will fail at compilation:

    int t = 5;
    const int ix = t;
    char cx[ix] = "hell";

    while that like below is correct:

    int t = 5;
    const int ix = 5;
    char cx[iy] = "hell";

    Can anybody give me a explanation about what is really happening here?

    Thanks.
    zouyongbin, Nov 7, 2006
    #1
    1. Advertising

  2. zouyongbin

    Kavya Guest

    zouyongbin wrote:
    > The code segment like below will fail at compilation:
    >
    > int t = 5;
    > const int ix = t;
    > char cx[ix] = "hell";
    >
    > while that like below is correct:
    >
    > int t = 5;
    > const int ix = 5;
    > char cx[iy] = "hell";
    >
    > Can anybody give me a explanation about what is really happening here?
    >


    I am not sure but I think since t is not a compile time constant that
    is why you are getting error in first code whereas 5 is a compile time
    constant due to which you are not getting error in 2nd case.
    Kavya, Nov 7, 2006
    #2
    1. Advertising

  3. zouyongbin

    Salt_Peter Guest

    zouyongbin wrote:
    > The code segment like below will fail at compilation:
    >
    > int t = 5;
    > const int ix = t;
    > char cx[ix] = "hell";


    This works fine, although rather strange.
    char cx[] = "hell";
    makes sense, and you can get its size with:
    size_t size = sizeof(cx)/sizeof(char);

    >
    > while that like below is correct:
    >
    > int t = 5;
    > const int ix = 5;
    > char cx[iy] = "hell";


    the above does not compile, iy is not declared nor defined.

    >
    > Can anybody give me a explanation about what is really happening here?
    >


    If your asking about the const int, there is no problem there:
    const int ix(5); // will do

    try:

    #include <iostream>

    int main()
    {
    char* p_s = "hell"; // pointer to a literal
    std::cout << p_s << std::endl;
    }
    Salt_Peter, Nov 7, 2006
    #3
  4. zouyongbin

    zouyongbin Guest

    Salt_Peter wrote:
    > zouyongbin wrote:
    > > The code segment like below will fail at compilation:
    > >
    > > int t = 5;
    > > const int ix = t;
    > > char cx[ix] = "hell";

    >
    > This works fine, although rather strange.
    > char cx[] = "hell";
    > makes sense, and you can get its size with:
    > size_t size = sizeof(cx)/sizeof(char);


    My compiler is gcc4.1.2, and I got a compile error here.

    > >
    > > while that like below is correct:
    > >
    > > int t = 5;
    > > const int ix = 5;
    > > char cx[iy] = "hell";

    >
    > the above does not compile, iy is not declared nor defined.


    Sorry, it's a type error. The "iy" should be "ix" here.

    What I am confused here is that whether "ix" is a constant seems to be
    influenced by what is assigned to "ix". If "ix" is assigned by a
    constant, like 5, it act like a constant. But if "ix" is assigned by a
    non-constant such as "t" here, it can not be set as the dimension of an
    array.
    I think that is weird. Is there any trick here?

    Thanks.
    zouyongbin, Nov 7, 2006
    #4
  5. zouyongbin

    Ian Collins Guest

    zouyongbin wrote:
    > Salt_Peter wrote:
    >
    >>zouyongbin wrote:
    >>
    >>>The code segment like below will fail at compilation:
    >>>
    >>>int t = 5;
    >>>const int ix = t;
    >>>char cx[ix] = "hell";

    >>
    >>This works fine, although rather strange.
    >>char cx[] = "hell";
    >>makes sense, and you can get its size with:
    >>size_t size = sizeof(cx)/sizeof(char);

    >
    >
    > My compiler is gcc4.1.2, and I got a compile error here.
    >
    >
    >>>while that like below is correct:
    >>>
    >>>int t = 5;
    >>>const int ix = 5;
    >>>char cx[iy] = "hell";

    >>
    >>the above does not compile, iy is not declared nor defined.

    >
    >
    > Sorry, it's a type error. The "iy" should be "ix" here.
    >
    > What I am confused here is that whether "ix" is a constant seems to be
    > influenced by what is assigned to "ix". If "ix" is assigned by a
    > constant, like 5, it act like a constant. But if "ix" is assigned by a
    > non-constant such as "t" here, it can not be set as the dimension of an
    > array.


    That's because it isn't a compile time constant.

    --
    Ian Collins.
    Ian Collins, Nov 7, 2006
    #5
  6. zouyongbin

    Salt_Peter Guest

    zouyongbin wrote:
    > Salt_Peter wrote:
    > > zouyongbin wrote:
    > > > The code segment like below will fail at compilation:
    > > >
    > > > int t = 5;
    > > > const int ix = t;
    > > > char cx[ix] = "hell";

    > >
    > > This works fine, although rather strange.
    > > char cx[] = "hell";
    > > makes sense, and you can get its size with:
    > > size_t size = sizeof(cx)/sizeof(char);

    >
    > My compiler is gcc4.1.2, and I got a compile error here.
    >
    > > >
    > > > while that like below is correct:
    > > >
    > > > int t = 5;
    > > > const int ix = 5;
    > > > char cx[iy] = "hell";

    > >
    > > the above does not compile, iy is not declared nor defined.

    >
    > Sorry, it's a type error. The "iy" should be "ix" here.
    >
    > What I am confused here is that whether "ix" is a constant seems to be
    > influenced by what is assigned to "ix". If "ix" is assigned by a
    > constant, like 5, it act like a constant. But if "ix" is assigned by a
    > non-constant such as "t" here, it can not be set as the dimension of an
    > array.
    > I think that is weird. Is there any trick here?
    >
    > Thanks.


    Thats the compile time constant issue, fix:

    int main()
    {
    int t(5);
    const int ix(t);
    char* p_cx = new char[ix];
    delete [] p_cx;
    }

    or better yet, a std::string or std::vector.
    Salt_Peter, Nov 7, 2006
    #6
  7. zouyongbin wrote:
    > The code segment like below will fail at compilation:
    >
    > int t = 5;
    > const int ix = t;
    > char cx[ix] = "hell";
    >
    > while that like below is correct:
    >
    > int t = 5;
    > const int ix = 5;
    > char cx[iy] = "hell";


    (Assuming you meant 'ix' here).

    > Can anybody give me a explanation about what is really happening here?


    In C++ language only integral constant expressions (ICEs) can be used to specify
    size in array type declarations. I won't include the full definition of ICE here
    (look it up), but what applies to this case is that if you want an object of
    'const int' type to act as an ICE, you have to make sure it that it itself is
    initialized with an ICE.

    In the second case this requirements is met. 'ix' is initialized with an
    explicit '5'' '5' is an ICE. This means that 'ix' is itself an ICE and can be
    used to declare array types.

    In the first case the above requirement is not met. 'iy' is initialized with 't'
    and 't' is not an ICE, meaning that 'iy' is not an ICE. Therefore, 'iy' cannot
    be used to declare array types.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 7, 2006
    #7
    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. Replies:
    11
    Views:
    1,086
  2. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,089
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    870
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    526
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page