Skip to content

Should memory allocation check if the pointer is NULL? #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pelesh opened this issue Oct 18, 2023 · 0 comments
Open

Should memory allocation check if the pointer is NULL? #19

pelesh opened this issue Oct 18, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@pelesh
Copy link
Collaborator

pelesh commented Oct 18, 2023

At several places in sparse matrix classes we check if the pointer is nullptr before we allocate memory and assign it to that pointer. Here is one such example:

        if (d_col_data_ == nullptr) {
          mem_.allocateArrayOnDevice(&d_col_data_, n_ + 1); 
        }
        if (d_row_data_ == nullptr) {
          mem_.allocateArrayOnDevice(&d_row_data_, nnz_current); 
        }
        if (d_val_data_ == nullptr) {
          mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); 
        }

Perhaps we should consider throwing exception when the pointer is not nullptr? This way potential bugs may remain undetected, because if the pointer is not a null, this does not necessarily mean that the allocated memory is of the size we need. In such case we may end up with undefined behavior such as writing past the end of array. More defensive programming would be to send an error message and throw an exception in situations like this.

CC @rothpc @maksud

@pelesh pelesh added the enhancement New feature or request label Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants