Pointer is pointing to 0x1 - is checking for null valid?
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?
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?
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:
|