Home > Backend Development > C++ > body text

Is Your C/C Code Making Unwarranted Assumptions?

Mary-Kate Olsen
Release: 2024-10-31 04:22:31
Original
410 people have browsed it

 Is Your C/C   Code Making Unwarranted Assumptions?

Demonstrating Unwarranted Assumptions in C/C

How can this educational tool be improved?

To enhance the effectiveness of this tool, consider the following suggestions:

  • Incorporate conditional compilation: Use conditional compilation to test assumptions based on specific platform characteristics. For example, if your test program relies on specific platform-dependent features, you can use #ifdef or #if __GNUC__ to tailor the tests to different compiler environments.
  • Expand the test cases: Include a wider variety of test cases to cover more common assumptions made by programmers. Examples could include:

    • "Floating-point arithmetic is associative"
    • "The size of a void* is the same as the size of the smallest addressable unit"
    • "The sign of a negative number is always -"
  • Automate the tests: Create a script or automated tool to execute the tests and collect the results across different platforms. This will streamline the testing process and make it easier to maintain the database of platform differences.

Which tests would be good and how should they look like?

1. Character Assumptions:

  • Test: Verify that 'A' == 65 on all platforms.
  • Code:

    <code class="c">EXPECT("00 we have ASCII",('A'==65));</code>
    Copy after login
    Copy after login
  • Test: Check if the assumption that 'a' < 'A' is valid.
  • Code:

    <code class="c">EXPECT("02 big letters come before small letters",('A'<'a'));
    Copy after login
  • Test: Test if the size of char is always 8 bits.
  • Code:

    <code class="c">EXPECT("04 a char is 8 bits",CHAR_BIT==8);
    Copy after login

2. Integer Assumptions:

  • Test: Examine if the assumption that integers use two's complement representation and wrap around is valid.
  • Code:

    <code class="c">EXPECT("06 integers are 2-complement and wrap around",(int_max+1)==(int_min));
    Copy after login
  • Test: Check if the assertion that the size of an int is the same as the size of a void* is true on all platforms.
  • Code:

    <code class="c">EXPECT("05 int has the size of pointers",sizeof(int)==sizeof(void*));
    Copy after login

3. Pointer Assumptions:

  • Test: Verify that sizeof(void*) is always greater than or equal to sizeof(void(*)()).
  • Code:

    <code class="c">EXPECT("10 void* can store function pointers",sizeof(void*)>=sizeof(void(*)()));</li>
    </ul>
    <p><strong>4. Execution Assumptions:</strong></p>
    <ul>
    <li>
    <strong>Test:</strong> Test if the stack always grows downwards.</li>
    <li>
    <p><strong>Code:</strong></p>
    <blockquote><pre class="brush:php;toolbar:false"><code class="c">EXPECT("12 the stack grows downwards",check_grow(5,0)<0);</code>
    Copy after login
  • Test: Examine the assumption that the evaluation of expressions is always left to right.
  • Code:

    <code class="c">EXPECT("00 we have ASCII",('A'==65));</code>
    Copy after login
    Copy after login

Would you run the tests on the platforms you can get your hands on and post the results?

We encourage community participation in testing the assumptions on different platforms. Please share your results along with the following information:

  • Platform details (operating system, compiler version, architecture)
  • Any differences observed from the expected results
  • Why the differences might occur (e.g., compiler optimizations, platform-specific implementation)

The above is the detailed content of Is Your C/C Code Making Unwarranted Assumptions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template