I have a segmentation fault in the code below, but after I changed it to pointer to pointer, it is fine. Could anybody give me any reason?
void memory(int *
The first example does not work because the pointer parameter is not the pointer in main, it just holds the same value.
A pointer stores the address at which the data is stored. Passing a pointer to a function means giving it the address of the data. However, here you have no address for the data until calling malloc
. So instead you need to pass the address of the pointer (i.e. pointer to pointer). This allows memory
to take the address of the pointer p
and set p
to point to the area of memory it allocates for the data.
You need a pointer to pointer because you need to return the value of the newly allocated pointer.
There is three ways to return that value (2 if you use C):
The third way has definitely my preference.
(and not, I won't admit you could also use a global, it's not a fourth way but an horror).
memory(p, 10); //get the address of the pointer
this just send the p value not the adress of the p. it sends (*p). if addrees of p 123 and its value 50 it send 50 to the function.
and at the memory function it makes a new pointer with new adress like 124 and it contains 50; and it will allocate the memory and write the start of the allocated memory to 124 not 123 so p at the main still contains 50. So you did nothing!. You can control this with this code.
#include<iostream>
#include<conio.h>
#include<exception>
using namespace std;
void memory(int* p, int size) { //*************pointer to pointer` is a must**********
try{
p = new int[size] ;
p[0]=100;
} catch(bad_alloc &e) {
cout<<e.what()<<endl;
}
}
int main()
{
int *p = 0; ******you can do you should do this*******
p = new int ;
/* p = malloc( sizeof(int)); // this just change the value inside of p it is unnecessary
and you will lose a adress from memory :) and possibly it will be unsafe if you free p may, you just
free one adress*/
/*before using a pointer always allocate it memory else
you ll get frequent system crashes and BSODs*/
memory(p, 10); //get the address of the pointer//you are getting nothing
cout <<p[0];
//we set p[0] to 100 but it will write a garbage value here.
//because the value inside of p didn't change at the memory function
//we sent just the value inside of p.
getch();
return 0;
}
Because you're wanting to get a pointer value back from the operations done in the function. malloc
allocates memory and gives you an address for that memory.
In your first example, you store that address in the local argument variable p
, but since it's just the argument, that doesn't make it back to the main program, because C/C++ are pass-by-value by default - even for pointers.
Main Function malloc
p p allocated
+---+ +---+
| 0 | | 0 | A
+---+ +---+
becomes...
p p allocated
+---+ +---+
| 0 | | ------------> A
+---+ +---+
and thus when main reads p, it gets 0, not A.
In your working code, you follow the pointer passed to an address, and that address gives you the location of the pointer variable in the main program. You update the pointer value at that address, which the main program can then look up the value of to use as its memory location - thus passing the address returned by malloc
back to the main program for use.
Main Function malloc
p p allocated
+---+ +---+
| 0 |<------- | A
| | | |
+---+ +---+
becomes...
p p allocated
+---+ +---+
| |<------- |
| ----------------------> A
+---+ +---+
and thus when main reads p, it gets A.
In C, you have only pass-by-value. So your function memory() gets it own local-only copy of p. malloc inside memory() assigns only to the copy of p that is local to the function. When memory() returns to main(), the copy of p from main is unchanged. In C++, you solve this by using pass-by-reference, like this:
void memory(int*& p, int size)
In C, you use double pointers to achieve similar results.