Reading new, filled array gives segfault - c++

I've been banging my head hard over this...I create a pointer in main(), which I pass on to another function. Inside that function, the pointer is used to create a new array (using the new operator), the array is filled, and the function ends.
If I then try to access elements in the new array in the caller, I get a segfault. Since the new operator was used, I expect the new array to be on the heap and thus not cleared by it going out of scope...I really don't get this. What am I overlooking? I also don't know precisely what to google for, so no luck there yet.
I can get it to work if I let the function return the pointer (instead of passing it), but I don't want to do that because eventually I'd like my function to create a few such newly created arrays. So what am I missing here?
Here is a minimal working example:
#include <iostream>
#include <stdio.h>
bool getData(double *myData)
{
myData = new double[2];
if (!myData)
return false;
myData[0] = +4.53;
myData[1] = -3.25;
return true;
}
int main()
{
double *myData = NULL;
if (!getData(myData))
fprintf(stderr, "Could not get data.\n");
std::cout << myData[0] << std::endl;
std::cout << myData[1] << std::endl;
delete [] myData;
}

Root Cause of the Crash:
When you pass a pointer to the function by value. An copy of the pointer gets passed to the function. Further You allocate memory to the copy of pointer passed through main, this pointer is not same as the one you access in main, it is an copy. The pointer myData in main was never allocated any memory, so eventually you are dereferencing a NULL pointer which results in a Undefined Behavior and an crash.
Suggested Solution 1:
Pass the pointer by Reference:
bool getData(double *&myData)
^
And you are good to go.This is the C++ way of doing it.
Another Solution:
You could also do:
bool getData(double **myData)
^
{
*myData = new double[2];
//so on
}
while calling it as:
getData(&myData);
^
A word of caution:
new does not return NULL in case of failure to allocate memory. It throws a std::bad_alloc exception. So you need to handle that exception or in case you want to check for null you should use the nothrow version of new.

The pointer argument to getData() is passed by value, not by reference. This means you're pushing the value (== the address the pointer points to) on the stack and call getData. Inside getData you overwrite this value with the return value from new[]. This value is no longer valid after returning from the function as it only existed on the stack.
Try to pass a reference or pointer to the pointer:
bool getData(double *&myData)
{
myData = new double[2];
if (!myData)
return false;
myData[0] = +4.53;
myData[1] = -3.25;
return true;
}

You have to pass a double** as myData, to initialize correctly your array. Currently, your getData function creates an array and stores its value in the copied parameter, so myData in main is not modified. Your must pass the pointer of myData and modify it with
bool getData(double** myData)
{
*pmyData = new double[2];
...
}
and call getData in main:
getData(&myData);

The myData given as parameter to getData function is passed on stack as a copy. When modifying that value in getData function you actually modify the value from the stack.
When you return to the main function everything is as it was before (except a memory leak).
The quickest solution would be to change the getData function like this:
bool getData(double *&myData)
and you're all set.

You pass the pointer into your function by value, not reference. Try this:
bool getData(double* &myData)
{
...
}
The difference is that myData is now a reference to the pointer in main, not a copy of it that gets destroyed when the function exits.

Related

Basic C++ segfault when using new and passing a pointer

Can somebody please explain the segfault here:
class foo
{
private:
some_class *test_;
void init_some_class(some_class*);
void use_class();
}
foo::foo()
{
//test_ = new some_class(variables, variables); //THIS WOULD WORK
}
void foo::init_some_class(some_class *tmp)
{
tmp = new some_class(variables,variables);
}
void foo::use_class()
{
test_->class_function() //THIS SEGfaults
}
I would call the funtion via init_some_class(test_); If I use new in the constructor then the test_->class_function() works fine. It only seems to segfault when I use new outside of the class constructor and try and pass the pointer through the function
When you write in init_some class() :
tmp = new some_class(variables,variables);
you are in fact storing the new pointer in the parameter that is passed by value. But this parameter is local to the function and lost as soon as the function returns.
So if you call somewhere init_some class(test_) the value of test_ is transferred to tmp, but the changed tmp remains local to the function. You therefore get a segfault beause test_ remains uninitialized.
Possible solutions:
A simple solution to the described use case could be to pass the parameter by reference:
void foo::init_some_class(some_class *& tmp) // note the &
{
tmp = new some_class(variables,variables);
}
With this definition, when calling init_some class(test_), the original test_ pointer gets modified.
Another solution could be to have the init_some_class() change directly the test_ member. You'd then no longer need a parameter.

pointer won't go back to it's reference from a function after return (stays null) C++

im using my own linked list made with stuct. The struct has 2 ints and 1 pointer to another struct, next.
I'm using the LL (linked list) but in one of the functions a certain pointer won't change.
AddChain( &MakeChain(..values..), added )
this is how i call the function, i send her a new chain to link to the big one, and if there is no big one aka it's NULL so it will replace it. In this call added is a NULL ptr to a chain struct.
void AddChain(PolyChain* pol, PolyChain* main) // adds the new piece to the chain
{
PolyChain *current = main, *back = NULL;
if (main == NULL)
{
pol->next = NULL;
main = pol;
return;
}
... \\ there is continuation of the function but it wont get so far without main one
}
Now, as you can see if the main chain is a NULL I make to to reference to the same thing as the new chain I got. I'm running in the debugger and pol HAS a value and after the line:
main = pol;
main realy changes to point to what pol is pointing to. BUT after the return in AddChain, added which is the main in AddChain is still a NULL. It didn't get the value back from the function, it didn't change the pointer of added as like it was by value and not by reference, but it was by reference.
What's causing this problem?
EDIT: MakeChain returns a PolyChain and AddChain gets PolyChain*, this is why i used &.
This happens because you pass your pointer PolyChain* main by value. A simple way of visualizing "passing by value" is to think about it as "passing by copy": a copy of the pointer is made specifically for the call of AddChain. That is why any change to the pointer inside AddChain remains local to AddChain.
You could fix this problem in three different ways:
Take PolyChain*& main by reference - this is the simplest solution. because nothing else needs to change.
Take PolyChain** main by pointer - in this case AddChain needs to dererefence main, i.e. use *main instead of main, and take a pointer when passing main to AddChain
Return PolyChain* with the new value of main - in this case the caller must make an assignment of the result back to the pointer passed for main.
void AddChain(PolyChain* pol, PolyChain* main)
...
main = pol;
return;
In that code, main is a local copy of whatever pointer was passed in, so assigning to main has no effect on the pointer outside the function.
You didn't give enough information for me to deduce your intent, so it is likely (but not clear) that you could fix the problem by passing the pointer by reference:
void AddChain(PolyChain* pol, PolyChain*& main)
You are changing a copy of the pointer, since you are passing the pointer by value. If you want to change the pointer that comes from the calling scope you will have to pass a pointer to the pointer or a reference to the pointer:
void AddChain(PolyChain*& pol, PolyChain*& main) {...}
You must use pointer to pointer to modify pointer value inside the function
void AddChain(PolyChain* pol, PolyChain** main)
than in your code:
void AddChain(PolyChain* pol, PolyChain** main) // adds the new piece to the chain
{
PolyChain *current = *main, *back = NULL;
if (main == NULL)
{
pol->next = NULL;
*main = pol;
return;
}
... \\ there is continuation of the function but it wont get so far without main one
}
to pass value to the function just call
AddChain(pol, &main);
where pol and main are regular pointers to PolyChain type.
or with simple values:
PolyChain* pMain = &main;
AddChain(&pol, &pMain);
NOTE: This practics are extremaly error prone so you must be extra careful for managing pointers at this manner.

Passing char* by value

Will the following code work? -
void doSomething(char* in)
{
strcpy(in,"mytext");
}
Here's how the function is being called:
doSomething(testIn);
OtherFn(testIn);
The char* in is used in other places in the code... and we are passing it by value to the function doSomething. I understand when we pass by value, a copy of the string stored in char* is copied within the function. So, when we do a strcpy, will it copy to the local copy or to the char* in that was passed in as an argument?
My understanding is we need to do: doSomething(char* &in). Is that right?
When you want to modify just the contents of what the pointer points to, use:
doSomething(char* in)
So, yes,
void doSomething(char* in)
{
strcpy(in,"mytext");
}
will work just fine as long as in points to enough memory to hold "mytest" and a terminating null character.
There are times when you want to modify where the pointer points to, for example, by allocating new memory. Then, you need to pass a reference to a pointer.
void doSomething(char*& in)
{
in = new char[200];
strcpy(in,"mytext");
}
and use it as:
char* s = NULL;
doSomething(s);
// Now s points to memory that was allocated in doSomething.
// Use s
// make sure to deallocate the memory.
delete [] s;

Pointer of class local variable? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can a local variable's memory be accessed outside its scope?
Is there worrying thing to do a code such (getIDs() returns a pointer):
class Worker{
private:
int workerID;
int departID;
int supervisorID;
public:
Worker()
{
workerID=0;
departID=0;
supervisorID=0;
name="anonymous";
workerAddress="none";
}
void setIDs(int worker, int depart, int supervisor)
{
workerID=worker;
departID=depart;
supervisorID=supervisor;
}
int* getIDs()
{
int id[3];
id[0]=workerID;
id[1]=departID;
id[2]=supervisorID;
return id;
}
};
And then, use it such:
Worker obj;
obj.setIDs(11,22,33);
cout<<(*obj.getIDs())<<endl;
cout<<++(*obj.getIDs())<<endl;
cout<<++(++(*obj.getIDs()))<<endl;
I am wondering about that because the compiler shows:
Warning 1 warning C4172: returning address of local variable or
temporary
Your int id[3] is allocated on a stack and gets destroyed when your int* getIDs() returns.
You're return a pointer to a variable that gets destroyed immediately after getIDs() returns. The pointer then becomes dangling and is practically useless as doing anyting with it is undefined behaviour.
Suppose you defined your class like this:
class Worker{
private:
int IDs[3];
public
// ...
int* getIDs() { return IDs; }
};
This partially solves your problem, as the pointer remains valid as long the Worker object is in scope, but it's still bad practice. Example:
int* ptr;
while (true) {
Worker obj;
obj.setIDs(11,22,33);
ptr = obj.getIDs();
cout << *ptr; // ok, obj is still alive.
break;
} // obj gets destroyed here
cout << *ptr; // NOT ok, dereferencing a dangling pointer
A better way of solving this is to implement your custom operator << for your class. Something like this:
class Worker {
private:
int workerID;
int departID;
int supervisorID;
public:
// ...
friend ostream& operator<<(ostream& out, Worker w);
};
ostream& operator<<(ostream& out, const Worker& w)
{
out << w.workerID << "\n" << w.departID << "\n" << w.supervisorID;
return out;
}
Even if this would work, it wouldn't be good practice to do it this way in c++ unless there is some profound reason why you want pointers to int. Raw c-syle arrays are more difficult to handle than, for instance, std::vectors, so use those, like
std::vector<int> getIDs(){
std::vector<int> id(3);
id[0]=workerID; id[1]=departID; id[2]=supervisorID;
return id;
}
If you're worried about the overhead: this is likely to be optimized away completely by modern compilers.
A local (also caled automatic) variable is destroyed once you leave the function where it is defined. So your pointer will point to this destroyed location, and of course referencing such a location outside the function is incorect and will cause undefined behaviour.
The basic problem here is that when you enter a function call, you get a new frame on your stack (where all your local variables will be kept). Anything that is not dynamically allocated (using new/malloc) in your function will exist in that stack frame, and it gets destroyed when your function returns.
Your function returns a pointer to the start of your 3-element-array which you declared in that stack frame that will go away. So, this is undefined behavior.
While you may get "lucky/unlucky" and still have your data around where the pointer points when you use it, you may also have the opposite happen with this code. Since the space is given up when the stack frame is destroyed, it can be reused - so another part of your code could likely use the memory location where your three elements in that array is stored, which would mean they would have completely different values by the time you dereferenced that pointer.
If you're lucky, your program would just seg-fault/crash so you knew you made a mistake.
Redesign your function to return a structure of 3 ints, a vector, or at the very least (and I don't recommend this), dynamically allocate the array contents with new so it persists after the function call (but you better delete it later or the gremlins will come and get you...).
Edit: My apologies, I completely misread the question. Shouldn't be answering StackOverflow before my coffee.
When you want to return an array, or a pointer rather, there are two routes.
One route: new
int* n = new int[3];
n[0] = 0;
// etc..
return n;
Since n is now a heap object, it is up to YOU to delete it later, if you don't delete it, eventually it will cause memory leaks.
Now, route two is a somewhat easier method I find, but it's kind of riskier. It is where you pass an array in and copy the values in.
void copyIDs(int arr[3] /* or int* arr */)
{
arr[0] = workerID;
/* etc */
}
Now your array is populated, and there was no heap allocation, so no problem.
Edit: Returning a local variable as an address is bad. Why?
Given the function:
int* foo() {
int x = 5;
return &x; // Returns the address (in memory) of x
} // At this point, however, x is popped off the stack, so its address is undefined
// (Garbage)
// So here's our code calling it
int *x = foo(); // points to the garbage memory, might still contain the values we need
// But what if I go ahead and do this?
int bar[100]; // Pushed onto the stack
bool flag = true; // Pushed onto the stack
std::cout << *x << '\n'; // Is this guaranteed to be the value we expect?
Overall, it is too risky. Don't do it.

Program Crashes on NULL Pointer object

I am trying a code which Goes like this:-
class test{
int test_int;
public:
virtual int show()
{
return test_int;
}
void set_int(int data){
std::cout<<"received data "<< data <<endl;
test_int = data;
}
};
int main()
{
test *ptr=new test();
ptr=NULL;
ptr->set_int(5);
return 0;
}
Now the problem i am facing is my program after printing the data which i am sending through set_int function got printed but the program crashes just after the completition of the function(set_int).
Am i doing any mistake that is not according to the language standards?
TIA.
Am i doing any mistake that is not according to the language standards?
Yes, you are.
You may not call member functions on a pointer that does not point to a valid object of that type. A null pointer never points to a valid object.
The trivial fix here is to remove the line ptr=NULL;. That way ptr still points to an object when the member function is invoked. This also allows you to fix the memory leak by deleting the pointer later. As a sidenote: avoid manual memory management.
You have pointer to test (test*) set to dynamicaly allocated memory representing instance of that class.
Right after that, you wrote "Nah, I do not need it anymore" and you forget where that newly allocated memory was.
Finally, you are trying to access an object on address 0, which is an invalid operation and will cause runtime error.
You probably meant to do this
int main()
{
test *ptr = new test();
ptr->set_int(5);
// ptr = NULL; // wont free the memory allocated by new
delete ptr; // memory deallocation
ptr = NULL; // now we can safely forget that address (which is now invalid anyways)
return 0;
}