Skip to main content
Spelling and grammar
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

You might ask: why does this matter? Apart from some esothericesoteric machines where pointers work differently depending on the type of object they point to, static analysis tools might also complain if your code is not correct according to C++'sthe C++ abstract machine's memory model.

Unless the constructor of T is marked noexcept, you must consider the possiblitypossibility that construction of any object of type T might cause an exception to be thrown. If so, you must make sure that you destroy any objects you have constructed so far.

You might ask: why does this matter? Apart from some esotheric machines where pointers work differently depending on the type of object they point to, static analysis tools might also complain if your code is not correct according to C++'s abstract machine's memory model.

Unless the constructor of T is marked noexcept, you must consider the possiblity that construction of any object of type T might cause an exception to be thrown. If so, you must make sure that you destroy any objects you have constructed so far.

You might ask: why does this matter? Apart from some esoteric machines where pointers work differently depending on the type of object they point to, static analysis tools might also complain if your code is not correct according to the C++ abstract machine's memory model.

Unless the constructor of T is marked noexcept, you must consider the possibility that construction of any object of type T might cause an exception to be thrown. If so, you must make sure that you destroy any objects you have constructed so far.

added 614 characters in body
Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180

Is it useful?

A Buffer is a very limited memory pool: you can only have one active allocation at a time. You might sometimes avoid reallocating memory, but new and delete are not that expensive. So your main() function could be replaced with:

int main() {
    …
    std::cout << "Test on SomeClass\n";
    SFloat* ps = new SFloat[N0];
    …
    // reallocating, possibly using existing storage, for a different type and size
    delete[] ps;
    float* pf = new float[N1];
    …
}

The comment is still valid: delete followed by new could very well reuse the same memory. Of course, raw new and delete should be avoided, std::vector is the appropriate thing to use here.

Is it useful?

A Buffer is a very limited memory pool: you can only have one active allocation at a time. You might sometimes avoid reallocating memory, but new and delete are not that expensive. So your main() function could be replaced with:

int main() {
    …
    std::cout << "Test on SomeClass\n";
    SFloat* ps = new SFloat[N0];
    …
    // reallocating, possibly using existing storage, for a different type and size
    delete[] ps;
    float* pf = new float[N1];
    …
}

The comment is still valid: delete followed by new could very well reuse the same memory. Of course, raw new and delete should be avoided, std::vector is the appropriate thing to use here.

added 586 characters in body
Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180

Missing exception handling

Unless the constructor of T is marked noexcept, you must consider the possiblity that construction of any object of type T might cause an exception to be thrown. If so, you must make sure that you destroy any objects you have constructed so far.

C++17 has some functions to help with this, like for example std::uninitialized_value_construct(). If you cannot use it yet in a C++14 code base, it might still be helpful to look at how it is implemented.

Destructors should never throw an exception, so you can mark the destructor of Buffer noexcept.

Use std::uintptr_t instead of std::size_t when aligning

Use std::uintptr_t instead of std::size_t when aligning

Missing exception handling

Unless the constructor of T is marked noexcept, you must consider the possiblity that construction of any object of type T might cause an exception to be thrown. If so, you must make sure that you destroy any objects you have constructed so far.

C++17 has some functions to help with this, like for example std::uninitialized_value_construct(). If you cannot use it yet in a C++14 code base, it might still be helpful to look at how it is implemented.

Destructors should never throw an exception, so you can mark the destructor of Buffer noexcept.

Use std::uintptr_t instead of std::size_t when aligning

Source Link
G. Sliepen
  • 69.5k
  • 3
  • 75
  • 180
Loading