0

I work on a custom container and I manually allocate some heap memory:

template<typename element_type> class MyClass{ element_type* m_data = nullptr; std::size_t rows, columns; // assume that they have been initialized inline MyClass(){ this->m_data = new element_type[(this->rows)*(this->columns)]{}; this->m_data_end = this->m_data+(this->rows)*(this->columns); } }; 

When I perform an analysis of my code, XCode (and LLVM-Clang under the hood) tells me that this->m_data = new element_type[(this->rows)*(this->columns)]{}; is storing garbage value (uninitialized values). I don't understand what I am doing wrong with this example. Any clarification would be much appreciated.

To me, I was doing what was advised in this accepted answer: https://stackoverflow.com/a/67967472/23051105.

As requested by the comments, I will provide more details on the implementation:

template<typename element_type> class Parent{ std::size_t rows, columns; element_type* m_data = nullptr; element_type* m_data_end = nullptr; inline Parent(const std::size_t rows, const std::size_t columns) : rows(rows), columns(columns){} }; template<typename element_type> class MyClass : public Parent<element_type> { inline void createInternStorage(){ if (this->m_data != nullptr || this->m_data_end != nullptr) this->deleteInternStorage(); this->m_data = new element_type[(this->rows)*(this->columns)]{}; this->m_data_end = this->m_data+(this->rows)*(this->columns); } inline void deleteInternStorage(){ if (this->m_data != nullptr) delete[] this->m_data; this->m_data = nullptr; this->m_data_end = nullptr; } inline MyClass(const std::size_t rows, const std::size_t columns) : Parent<element_type>(rows, columns) { this->createInternStorage(); } }; 

Please keep in mind that the program is fully operational, it initializes the right amount of memory and everything appears to me totally fine. I'm just confused with this analyzer message.

6
  • 1
    The data members row and columns are remain uninitialized. And you're using those unitialized variables in (this->rows)*(this->columns) leading to undefined behavior. Commented Jun 29, 2024 at 15:23
  • 2
    please show a minimal reproducible example, are you definitely initialising rows and columns before using them? I presume by the assume that they have been initialized there is some initialisation code somewhere that you've omitted? Is there a path through your code where they aren't initialised? Commented Jun 29, 2024 at 15:26
  • Change std::size_t rows, columns; to std::size_t rows{}, columns{}; ... did the problem go away? Commented Jun 29, 2024 at 15:35
  • 2
    This needs an actual complete minimal reproducible example. There is no point in guessing from partial code snippets or untested reductions of the code. There are too many unknowns. First of all, the static analyzer will not emit a diagnostic just for leaving objects uninitialized. It is only a bug that will be diagnosed if you actually have a (potential) access to the uninitialized object. But you aren't showing any point where that could be at all. Whether or not all of the allocated array will be initialized also depends on element_type. Commented Jun 29, 2024 at 15:54
  • 2
    This is almost a minimal reproducible example (thanks!), but "reproducible" means that someone should be able to compile and run it exactly as you've posted. So please add the necessary headers and a main function which calls the relevant code in a way that reproduces the error. Also please state the exact commands you are using to do the analysis, and the versions of the compiler and analysis tools that you are using, as well as the exact text of the warning they report. Commented Jun 29, 2024 at 16:17

1 Answer 1

0

I don't understand what I am doing wrong with this example.

Before entering the constructor's body all data members are initialized(default). This means that rows and columns remain unitialized and have indeterminate values. And so you're using these unintialized variables when you wrote:

//-------------------------------------vvvv---------vvvvvvv----->these are uninitialized this->m_data = new element_type[(this->rows)*(this->columns)]{}; 

Thus, the program will have undefined behavior.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, I edited the first post to give more details!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.