This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: No compiler error for zero sized array

Tool/software: TI C/C++ Compiler

Hello,

I have some problem with a missing error the compiler shall issue.

We are using CGT version 7.4.23 with CCS 8.1.0.11 on a C6418.

When I was debugging I found out that a constructor of an class is called where no instances should exist.

The class was defined like this

in .h file:

class A

{

static A _instances[];

}

in .cpp file:

A A:_instances[APP_DEFINE_A_INSTANCE_CNT];

For the application I was compiling APP_DEFINE_A_INSTANCE_CNT was set to 0.

Inside of the constructor only member vars were set.

Before main() is reached all static objects are initialized. Therefore the constructor was called. But since no instance was existing, it was overwriting some other data.

What is the reason I don't get the compiler error?

Is it a wrong CGT configuration?

I also tested:

int a[0];

But this is compiled without error, too. 


I need your help. I expect to get a compiler error. Is this assumption wrong?

Thank you for your help!
Kind Regards

Bernhard Seiz

  • Hello Bernhard,

    <<< int a[0];
    <<< But this is compiled without error, too.

    Which C++ standard do you use?
    The latest C++14 (See 8.3.4 on page 184 of N3690) mentions array size as a simple expression (not constant-expression).

    Are you sure that "it was overwriting some other data"?
  • Hi Tom,

    thanks for your response!

    From the wiki

    http://processors.wiki.ti.com/index.php/TI_Compilers_and_Industry_Standards

    the C6000 version 7.4.23 compiler uses C++03.

    Yes I am sure that it was overwriting other data. I started debugging to find a misbehaviour of my application. Then I saw that a global variable that is initialized durind declaration, was overwritten somewhere. It is never touched in our code. So I set up a hardware watchpoint for write access and found the constructor of a class which instances were defined in an array of size 0. In the constructor a value 0 was assigned to a member var. This member var address (Expressions View) was at the same address of my global variable.

    I realized that I have enabled the 

    --relaxed_ansi
    compiler switch

    does this have an effect?

    Bernhard

  • Hi Bernhard,

    I do not have C6418 or similar and I am unable to recreate your issue.
    You need to wait for someone from TI, probably.
    I have some pain with C++14 and that is why I have replied to you.
  • Dear Tom,

    I appreciate any comment! Thanks!

    There is always a chance to change the point of view what can lead to other questions that solve an issue.

    Hope you progress on your issue.

    Bernhard

  • When I try to compile code similar to ...

    Bernhard Seiz said:

    in .h file:

    class A

    {

    static A _instances[];

    }

    in .cpp file:

    A A:_instances[APP_DEFINE_A_INSTANCE_CNT];

    For the application I was compiling APP_DEFINE_A_INSTANCE_CNT was set to 0.

    ... or ...

    Bernhard Seiz said:
    int a[0];

    I always get an error diagnostic similar too ...

    "file.cpp", line 6: error: the size of an array must be greater than zero

    To understand what happened, I need a test case which allows me to reproduce the same behavior.  Please submit a test case as described in the article How to Submit a Compiler Test Case.

    Thanks and regards,

    -George

  • Dear George,

    I sent you a test case a moment ago. I was following the instruction you provided a link to.

    Sorry for the delay, I was very busy.

    Looking forward to your feedback

    Bernhard

    Edit:

    I added a line

    int a[0];

    to the file to trigger the compiler error.

  • You use the build option --relaxed_ansi .  When this option is used, zero length arrays are allowed.  The only way to get an error diagnostic is to remove --relaxed_ansi.

    Thanks and regards,

    -George

  • Dear George,

    thank you very much! You're right.

    I could have tested before. Sorry for that.

    Kind Regards

    Bernhard