Skip to main content

Passing a pointer by reference

I wanted to create an array of a certain class but its size was not determined until run time. Therefore I had to of course use a pointer. Now because it was always good practice to initialize a pointer to NULL, I did so and I passed this initialized pointer to a function. This function would then new the pointer inside, seemed OK but this made the program crash.

I used the function AllocateArray1in the gist. Why did it crash?
The pointer is passed by value and when the function exits the pointer being passed is not quite initialized.

To verify this I used the test case 3 where I created an array, deleted it but didn't assign it back to NULL, this made sure that the pointer is still pointed to a certain memory address.
The works because although the memory is deallocated we can still access the values using raw pointers, although this might crash some times due to access violation.

The other option of course, which I used was to pass the pointer by reference. AllocateArray2 ensures that the same memory address is passed.

#include <iostream>
using namespace std;
class MyClass {
public:
int iMyVar;
};
void AllocateArray1(MyClass* pMyClassPointer) {
std::cout << "Function 1 called" << std::endl;
pMyClassPointer = new MyClass[10];
for (int i = 0; i < 10; i++)
pMyClassPointer[i].iMyVar = i;
}
void AllocateArray2(MyClass* & rpMyClassPointer) {
std::cout << "Function 2 called" << std::endl;
rpMyClassPointer = new MyClass[10];
for (int i = 0; i < 10; i++)
rpMyClassPointer[i].iMyVar = i;
}
void DontAllocateArray1(MyClass* pMyClassPointer) {
std::cout << "Function 3 called" << std::endl;
for (int i = 0; i < 10; i++)
std::cout << pMyClassPointer[i].iMyVar << std::endl;
}
void DontAllocateArray2(MyClass* & pMyClassPointer) {
std::cout << "Function 4 called" << std::endl;
for (int i = 0; i < 10; i++)
std::cout << pMyClassPointer[i].iMyVar << std::endl;
}
int main(int argc, char** argv) {
// use only 1 test case
MyClass *pPointer = NULL;
// test case 1
AllocateArray1(pPointer); // sgementation fault
// test case 2
AllocateArray2(pPointer); // works
// test case 3 // works
pPointer = new MyClass[10];
delete pPointer;
AllocateArray1(pPointer);
// test case 4
pPointer = new MyClass[10];
DontAllocateArray1(pPointer); // works
// test case 5
pPointer = new MyClass[10];
DontAllocateArray2(pPointer); // works
// this shsould always be there
for (int i = 0; i < 10; i++)
std::cout << pPointer[i].iMyVar << std::endl;
}

Comments

Popular posts from this blog

Detaching a process from terminal - exec(), system(), setsid() and nohup

Linux processes are created by fork() and exec(). The very first process of POSIX systems is init and subsequent processes are derived from the init as parent. These subsequent processes are child processes. During forking the parent process copies itself - with all I/O, address space, stack, everything. The only thing that is different is the process ID. The parent and child will have 2 different process IDs. The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system() returns after the command has been completed. system() executes a command specified in command by calling /bin/sh -c command , and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.  system() calls are often made within programs to execut...

C++ Callbacks using function pointers vs boost bind +boost function

In C, the most common uses of callbacks are as parameters to library functions like qsort , and as callbacks for Windows functions, etc. For example you might have a library that provides some sorting functions but you want to allow the library user to provide his own sorting function. Since the arguments and the return values do not change depending on the sorting algorithm, this can be facilitated in a convenient manner using function callbacks. Callbacks are also used as event listeners. onMouseClick(), onTerminalInput(), onData(), onConnectionStatus(), onRead() are probably some examples you've already seen in different libraries. The libraries have these callback functions and the users of the library are supposed to implement them. The library has a function pointer to these functions and calls them on their event loop which will invoke the code of the inherited classes of the library user. The implementation of function pointers is simple: they are just "code p...

sprintf, snprintf, strcpy, strncpy and sizeof operator

"C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking." "snprintf is safer than sprintf" What do these statements really mean? int sprintf ( char * str , const char * format , ... )  int snprintf ( char * s, size_t n, const char * format, ... );  char * strcpy ( char * destination, const char * source ); char * strncpy ( char * destination, const char * source, size_t num );   The usage is something like; char* msg1 = new char[10]; strcpy(msg1, "test"); // 1 char buffer[128]; sprintf(buffer, "%s", msg); //2 strcpy : Copies bytes until it finds a 0-byte in the source code. The string literal "test" has 4 characters and a terminating null character at end, therefore needs 5 characters at least on msg1.  Is this dangerous? Yes, because if the source message is not null terminated it will read until a null character ...