problem in memory allocation for a pointer
-
Hi all, I am getting seg fault when trying this piece of code. I'm not getting what is went wrong here. void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int *ptr = NULL; fun(ptr); *ptr = 5; //seg fault cout<<*ptr; }
Well of course, you are passing a pointer to a function (a certain address, which is NULL). In the function, you assign a new address to this pointer but the pointer is only a copy of the original, so, your original pointer is not affected. To make things more easier, think that a pointer is just a variable that holds an address. When you pass this variable to a function, the function makes a copy of it. It still points to the same address but if you change the address, you only modify the local copy.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Well of course, you are passing a pointer to a function (a certain address, which is NULL). In the function, you assign a new address to this pointer but the pointer is only a copy of the original, so, your original pointer is not affected. To make things more easier, think that a pointer is just a variable that holds an address. When you pass this variable to a function, the function makes a copy of it. It still points to the same address but if you change the address, you only modify the local copy.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
ok then what change is required here. so that i can i can put some value into memory allocated in fun() function.
Exactly the same as if it was a variable: pass it by reference.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Exactly the same as if it was a variable: pass it by reference.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Like this??? void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int ptr; fun(&ptr); ptr = 5; cout<<ptr; } I dont think so.. probably you mean something else.. that i am not getting.
modified on Monday, September 29, 2008 8:40 AM
Use &. void fun(int*&ptr) { } then in the main function fun(ptr) Is it working now?
-
Like this??? void fun(int* ptr){ ptr = (int*)malloc(sizeof(int)); } int main(){ int ptr; fun(&ptr); ptr = 5; cout<<ptr; } I dont think so.. probably you mean something else.. that i am not getting.
modified on Monday, September 29, 2008 8:40 AM
Well, you need something a bit more elaborated (plain
C
):void myAlloc(int ** ptr)
{
*ptr = (int *) malloc(sizeof(int));
}
int main()
{
int * pMyInt;
myAlloc( &pMyInt);
*pMyInt = 5;
printf("%d\n", *pMyInt);
free( pMyInt)
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Well, you need something a bit more elaborated (plain
C
):void myAlloc(int ** ptr)
{
*ptr = (int *) malloc(sizeof(int));
}
int main()
{
int * pMyInt;
myAlloc( &pMyInt);
*pMyInt = 5;
printf("%d\n", *pMyInt);
free( pMyInt)
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
ok then what change is required here. so that i can i can put some value into memory allocated in fun() function.