logo
down
shadow

Can I get a pointer to a pointer pointing to nullptr, is it valid


Can I get a pointer to a pointer pointing to nullptr, is it valid

Content Index :

Can I get a pointer to a pointer pointing to nullptr, is it valid
Tag : cpp , By : turret
Date : January 12 2021, 08:33 AM

like below fixes the issue I am having some doubts about the following code: ,
My concern is that could cause UB, as it is a nullptr

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Pointer is pointing to 0x1 - is checking for null valid?


Tag : cpp , By : Mikael
Date : March 29 2020, 07:55 AM
help you fix your problem Probably not. The NULL value is indicated in the source by 0 so checking if 0x1 is NULL will return false.
The way to recover (technically, recovery is not plausible, you really need to prevent) would be to assume that 0x1 is as bad as NULL and not try to use it in that case, something like:
if ((p == 0) || (p == 0x1))
    return;
// Otherwise use p.

Nullptr and checking if a pointer points to a valid object


Tag : cpp , By : Helpful Dude
Date : March 29 2020, 07:55 AM
With these it helps In a couple of my older code projects when I had never heard of smart pointers, whenever I needed to check whether the pointer still pointed to a valid object, I would always do something like this... , In C, anything that's not 0 is true. So, you certainly can use:
if (ptrToObject) 
    ptrToObject->doSomething();
delete ptrToObject;
assert(ptrToObject);
ptrToObject = nullptr;
assert(!ptrToObject);    
#include <iostream>
#include <memory>

void report(std::shared_ptr<int> ptr) 
{
    if (ptr) {
        std::cout << "*ptr=" << *ptr << "\n";
    } else {
        std::cout << "ptr is not a valid pointer.\n";
    }
}

int main()
{
    std::shared_ptr<int> ptr;
    report(ptr);

    ptr = std::make_shared<int>(7);
    report(ptr);
}
#include <iostream>
#include <memory>

void observe(std::weak_ptr<int> weak) 
{
    if (auto observe = weak.lock()) {
        std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
    } else {
        std::cout << "\tobserve() unable to lock weak_ptr<>\n";
    }
}

int main()
{
    std::weak_ptr<int> weak;
    std::cout << "weak_ptr<> not yet initialized\n";
    observe(weak);

    {
        auto shared = std::make_shared<int>(42);
        weak = shared;
        std::cout << "weak_ptr<> initialized with shared_ptr.\n";
        observe(weak);
    }

    std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
    observe(weak);
}

pointer to a pointer, which is pointing to a memory block, which pointer should be freed?


Tag : c , By : gopal
Date : March 29 2020, 07:55 AM
around this issue It doesn't matter - both temp_array and array point to the same memory block. I would prefer temp_array as then the realloc and free pointers match. Depending on your working code, for protection you could consider assigning both pointers to NULL to prevent free-ing the memory twice. free(NULL) is safe - no operation is performed.
Regarding the initial alloc of one integer - is that necessary? From the code shown, an int defined on the stack would be preferable.

Does setting a pointer to nullptr affect other pointers pointing to the same address?


Tag : cpp , By : mckasty
Date : March 29 2020, 07:55 AM
This might help you
Since todel->parent->left == todel that means that by setting todel->parent->left to nullptr, I'm just as well setting todel to nullptr.

Check pointer's nullability: !pointer or pointer == nullptr?


Tag : cpp , By : AJacques
Date : March 29 2020, 07:55 AM
Any of those help Both are equivaluent because nullptr is guaranteed to be converted to false when converted to a boolean.
From N4296:
Related Posts Related QUESTIONS :
  • is it safe to use the same mutex with lock_gard and without it in other parts of code
  • How to decode MAP Invoke messages using asn1c generated code
  • How do you write multiple lines in a .txt with recursion?
  • Member function with strange type causing callback function mismatch
  • Visual Studio optimisations break SDL graphical output
  • How to use less memory in Sieve_of_Eratosthenes
  • Covariance in Callback Parameters C++
  • switch may fall through (no it may not)
  • Compilation fails calling Cocoa function from C++
  • How to handle classes with differently named member functions in algorithms?
  • Convert QString to QJsonArray
  • Data exchange finished in CPropertyPage::OnOK?
  • Template member specialization in template class
  • Is it not possible to assign a struct to an index of a vector?
  • Why is empty unordered_map.find not returning end()?
  • Template argument deduction for inheriting specializations
  • dlopen undefined reference
  • Member function of class with template arguments and default arguments outside class
  • Is it possible to implement a non-owning "slightly smart" pointer on top of standard weak pointers?
  • how to configure the AcquireCredentialsHandleA correctly
  • Using private versions of global extern variables with OpenMP
  • Eigen Block wrong amount of columns and rows
  • Memory alignment rules in inheritance
  • Is nullptr falsy?
  • tm_wday returns a large integer outside 0-6 range
  • Scope a using declaration, inside a header
  • How to specify constructor's template arguments inside a new expression?
  • Sort an array via x86 Assembly (embedded in C++)?? Possible?
  • How to Replace only Part of the Variable using #define
  • How do you compare the performace of valarrays vs built-in arrays?
  • Is it normal for C++ static initialization to appear twice in the same backtrace?
  • c++ generate a good random seed for psudo random number generators
  • Why isn't my operator overloading working properly?
  • Getting meaningful error messages from fstream's in C++
  • C++: Converting Julian dates to Gregorian
  • Could someone explain this interesting behaviour with Sleep(1)?
  • Is it possible to roll a significantly faster version of modf
  • Updating pointer using signals and slots
  • How are classes more secure than structures?
  • finding "distance" between two pixel's colors
  • C++ Greatest Number Verification
  • Why does my token return NULL and how can I fix it?(c++)
  • C++ enforce conditions on inherited classes
  • what happened if an exception is not captured?
  • Redundant naming in C/C++ typedefs/structs
  • question about STL thread-safe and STL debugging
  • killing a separate thread having a socket
  • Returning the size of available virtual memory at run-time in C++
  • Parallel computing for integrals
  • How do I force my std::map to deallocate memory used?
  • C++ Templates: implicit conversion, no matching function for call to ctor
  • Adding python script to c++ project
  • C++ private pointer "leaking"?
  • Initializing Primitive Array to One Value
  • how is push_back implemented in STL vector?
  • C++ Profiling: KiFastSystemCallRet
  • Multiple rows with a single INSERT in SQLServer 2008
  • Use super class's address/pointer in initialization list
  • double fork using vfork
  • Convert char pointer (char*) to struct
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com