C++ check if pointer is valid? - c++

I'm having problems with valid pointers in C++. I'm using one object in different threads, so I can't just set the pointer to NULL and return. Here's what I'm trying:
int main()
{
char *ptr = new char[1024]; //assume PTR = 0x12345678
changePtr(ptr); //after calling this,
//ptr is not NULL here.
return 0;
}
void changePtr(char *ptr)
{
delete [] ptr; //ptr = 0x12345678
ptr = NULL; //ptr = NULL
}
How can I change ptr to NULL for both functions?

change the signature of changePtr to:
void changePtr(char **ptr)
{
delete [] *ptr; //ptr = 0x12345678
*ptr = NULL; //ptr = NULL
}
And call it using:
changePtr(&ptr);

In C++, use reference parameter:
void changePtr(char *&ptr) {
delete [] ptr; //ptr = 0x12345678
ptr = NULL; //ptr = NULL
}
In C, you need to pass pointer to pointer, which is basically same thing with less pretty syntax.
You do not need to change the calling code. But you must give a modifiable variable as argument when calling, can't give for example NULL or nullptr, same as you can't do &NULL.

If you really want to manage memory in such a complex, error-prone way, then pass a reference to, rather than a copy of, the caller's pointer:
void changePtr(char *&ptr)
// ^
Much better would be to use a smart pointer; they are designed so that it's very difficult to leave them dangling when the target is deleted:
int main()
{
std::unique_ptr<char[]> ptr(new char[1024]); //assume PTR = 0x12345678
changePtr(ptr); //after calling this,
//ptr is empty here.
return 0;
}
void changePtr(std::unique_ptr<char[]> & ptr)
{
ptr.reset();
}
although if I wanted a dynamic array, I'd avoid new altogether and use std::vector.

I have seen a great many questions about how to check a pointer for validity. A large number of these questions have been about Windows. There may not be a general way to check in C++, but for a Windows specific solution the following seems to work on my system:
#include <windows.h>
#include <stdio.h>
int main(int argc, char **argv)
{
MEMORY_BASIC_INFORMATION lpBuffer;
int cActualBytes;
cActualBytes = VirtualQuery(&main, &lpBuffer, sizeof(lpBuffer)); // Can we get info about main?
if (!cActualBytes)
{
printf("Nope, you can't do that \n");
return 2;
}
if (cActualBytes != sizeof(lpBuffer))
{
printf("Surprise! Expected %d bytes, got %d\n", sizeof(lpBuffer), cActualBytes);
}
printf("Information for main\n");
printf("---------------------------\n");
printf("C reports pointer %p, Virtual Alloc sees it as %p\n",&main,lpBuffer.BaseAddress);
return 0;
}

Related

return pointer with new operator. Where to put delete?

Im fairly new to C++.
So I learned that new allocates memory and returns a pointer of my datatype. But can I use this in a function and return the pointer? If so then where should I place the delete operator?
Is the following code legal?
int *makeArray(int size)
{
int *result = new int[size];
delete[] result;
return result;
}
int main()
{
int *pointer = makeArray(10);
/* code ... */
return 0;
}
It is compiling and working but logically it makes no sense because I deleted my array.
So I tried the following:
int *makeArray(int size)
{
int *result = new int[size];
return result;
}
int main()
{
int *pointer = makeArray(10);
/* code ... do some stuff with pointer */
delete[] pointer;
return 0;
}
Is this safe or does it cause a memory leak? Is there a better way of returning the pointer?
I tried both versions and they are compiling and working but I'm sure at least one of them if not both are unsafe.
Is the following code legal?
int *makeArray(int size)
{
int *result = new int[size];
delete[] result;
return result;
}
int main()
{
int *pointer = makeArray(10);
/* code ... */
return 0;
}
Definitely not! This is Undefined behavior because you return a deleted pointer. After using the delete operator you're telling your OS that it can release the memory and use it for whatever it wants. Reading or writing to it is very dangerous (Program crashing, Bluescreen, Destruction of the milky way)
int *makeArray(int size)
{
int *result = new int[size];
return result;
}
int main()
{
int *pointer = makeArray(10);
/* code ... do some stuff with pointer */
delete[] pointer;
return 0;
}
Is this safe or does it cause a memory leak?
Yes this is the correct way of using the new and delete operator. You use new so your data stays in memory even if it gets out of scope. Anyway it's not the safest code because for every new there has to be a delete or delete[] you can not mix them.
Is there a better way of returning the pointer?
YES. It's called Smart Pointer. In C++ programmers shouldn't use new but smart pointer at all. There is a C++ community Coding Guideline to avoid these calls Guideline R11

Function with pointer as parameter - what does the function expect by calling?

I saw this example:
void pass_by_value(int* p)
{
//Allocate memory for int and store the address in p
p = new int;
}
void pass_by_reference(int*& p)
{
p = new int;
}
int main()
{
int* p1 = NULL;
int* p2 = NULL;
pass_by_value(p1); //p1 will still be NULL after this call
pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory
return 0;
}
if i call the function pass-by-value, shouldnt it expect something like "&p" instead of p?
The pass-by-value is messed up anyway, the value passed to the function would be lost when the call completes and the memory would leak. It's legal code, it's just useless. If not using references or function return value the function would need to take a pointer-to-pointer (really, the compiler is likely to produce the same code for both the pointer-to-pointer and reference-to-pointer cases - barring inlining - it's just a bit cleaner to use the reference):
void foo(int ** p)
{
*p = new int;
}
int main()
{
int * p = nullptr;
foo(&p);
delete p;
}

Copying "this" pointer to a buffer

I need to copy the address of this pointer to a buffer and re-typecast it elsewhere. I am aware that I can do it, if I do it outside the class. But,here I specifically need to use some member function as given in the sample code provided here:
#include <iostream>
#include <cstring>
class MyClass
{
public:
MyClass(const int & i)
{
id = i;
}
~MyClass()
{
}
void getAddress(char* address)
{
memcpy(address, this, sizeof(this));
}
void print() const
{
std::cout<<" My id: "<<id<<std::endl;
}
private:
int id;
};
int main()
{
MyClass myClass(100);
std::cout<<"myClass: "<<&myClass<<std::endl;
myClass.print();
// Need to copy the address to a buffer, sepcifically using a function as follows and retrieve it later
char tmp[128];
myClass.getAddress(tmp);
// retreiving the pointer
MyClass* myClassPtr = (MyClass*) tmp;
std::cout<<"myClassPtr: "<<myClassPtr<<std::endl;
myClassPtr->print();
return 0;
}
My Output:
myClass: 0x7fff22d369e0
My id: 100
myClassPtr: 0x7fff22d369f0
My id: 100
As we can see two addresses are different. However, both function are printing the id correctly. I wonder how this happens! I also need the correct method.
Correct version of getAddress():
void getAddress(char* address)
{
const MyClass* tmp = this;
memcpy(address, &tmp, sizeof(this));
}
Update:
// retreiving the pointer
// MyClass* myClassPtr = (MyClass*) tmp; - wrong
MyClass* myClassPtr = *( (MyClass**) tmp );
You are not copying the value of the this pointer, but the data that the pointer is pointing to (memcpy expects the address of the data to copy).
The correct syntax would be:
MyClass* ptr = this;
memcpy(address, &ptr, sizeof(ptr));
That is why you get the same/correct output at the end.
Then, the assignment
MyClass* myClassPtr = (MyClass*) tmp;
is wrong too, you set the pointer to the same address as the char buffer, not to the address that is stored in the char buffer.
A possible solution could look like this:
MyClass* myClassPtr;
memcpy( &myClassPtr, tmp, sizeof( MyClass*));
Finally: I'd like to know what exactly you want to do, that you want to implement something like this? Maybe this is the solution for this problem, but I have a strong feeling that this is the wrong solution for the real problem ...

Program crashes (Dynamic memory allocation)

I just started reading about dynamic memory allocation and for some reason my program crashes.
void getSpace(int *ptr)
{
ptr = new int [100];
}
void getData(int *ptr)
{
cin >> *(ptr);
cin >>*(ptr+1);
}
int main()
{
int *ptr = NULL;
getSpace(ptr);
getData(ptr);
}
But for some reason, this one doesn't. It seems like if I declare the pointer at the same function as where I get the array into the heap size I wanted, it doesn't crashes.
int* ptr is local copy of pointer, so you only allocate the memory inside scope of getSpace, and you assign it into ptr variable, but it is local to the getScope, and so the main function does not see the allocation. You should either do int** ptr and do *ptr = new int[100]; or int*& ptr and then ptr = new int[100];
Change to:
void getSpace(int **ptr)
{
*ptr = new int [100];
}
and call it:
getSpace(&ptr);

Is it passing by pointer?

void func(char* buf) { buf++;}
Should I call it passing by pointer or just passing by value(with the value being pointer type)? Would the original pointer passed in be altered in this case?
This is passing by value.
void func( char * b )
{
b = new char[4];
}
int main()
{
char* buf = 0;
func( buf );
delete buf;
return 0;
}
buf will still be 0 after the call to func and the newed memory will leak.
When you pass a pointer by value you can alter what the pointer points to not the pointer itself.
The right way to do the above stuff would be
ALTERNATIVE 1
void func( char *& b )
{
b = new char[4];
}
int main()
{
char* buf = 0;
func( buf );
delete buf;
return 0;
}
Notice the pointer is passed by reference and not value.
ALTERNATIVE 2
Another alternative is to pass a pointer to a pointer like
void func( char ** b )
{
*b = new char[4];
}
int main()
{
char* buf = 0;
func( &buf );
delete buf;
return 0;
}
Please note I am not in any way advocating the use of naked pointers and manual memory management like above but merely illustrating passing pointer. The C++ way would be to use a std::string or std::vector<char> instead.
The pointer will not be altered. Pass by pointer means pass an address. If you want the pointer altered, you have to pass a double pointer a deference it once.
foo( char **b)
{
*b = NULL;
}
The pointer itself is being passed by value (the memory being pointed at is being passed by pointer). Changing the parameter inside the function will not affect the pointer that was passed in.
To implement reference semantics via "passing a pointer", two things must happen: The caller must take the "address-of" the thing to be passed, and the callee must dereference a pointer.
Parts of this can be obscured by the use of arrays, which decay to a pointer to the first element - in that sense, the array content is always "passed by reference". You can use an "array-of-one" to obscure the dereferencing, though.
This is the straight-forward apporoach:
void increment_me(int * n) { ++*n; } // note the dereference
int main() { int n; increment_me(&n); } // note the address-of
Here's the same in disguise:
void increment_me(int n[]) { ++n[0]; } // no visible *
int main() { int n[1]; increment_me(n); } // or &