| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Understanding malloc...
Hello,
Here are some Q's about memory allocation in C++; Q1. Code:
#include<iostream>
using namespace std;
void memAlloc(int *i)
{
i=(int*)malloc(sizeof(int));
}
int main()
{
int *i;
memAlloc(i);
*i = 100;
cout << *i << endl ;
return 0;
}
Memory is NOT allocated in this case to i. WHY? Does end of scope of function memAlloc mean deallocation of mem automatically? But i thought when we use malloc we have to explicitly deallocate memory? Q2. Code:
#include<iostream>
using namespace std;
void memAlloc(int *i)
{
free(i);
}
int main()
{
int *i;
i=(int*)malloc(sizeof(int));
memAlloc(i);
*i = 100;
cout << *i << endl ;
return 0;
}
Memory doesnt seem to have been freed here cause when i cout *i 100 is sucessfully printed Thanks Neville |
|
#2
|
||||
|
||||
|
Q1: I see no reason why that wouldn't work fine. Are you sure that there isn't any memory being allocated? Perhaps try casting malloc() as a void pointer instead?
Q2: The free() function only deallocates the memory, it doesn't clear or reassign it. Thus, sometimes the freed memory will immediately be filled with values for another process, but sometimes they will remain untouched. Thus, after you deallocate memory, it may or may not retain it's value immediately after being freed.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
Q1: A nice C-problem caused by pointers. A never ending story, and probably the reason why other languages does not have pointers at all
The anser is 'easy'. In your function, the variable i is a copy of your pointer. Therefore the memory is allocated and i is set to the pointer. However, since it is a 'copy of' it is not returned to main(); Solution (notice the changes in bold): Code:
void memAlloc(int **i)
{
*i=(int*)malloc(sizeof(int));
}
int main()
{
int *i;
memAlloc(&i);
*i = 100;
cout << *i << endl ;
return 0;
}
Seems wrong? Just remember how to change an int value in a function: Code:
void change(int *i)
{
*i=256;
}
int main()
{
int i = 3;
change(&i);
cout << i << endl ;
return 0;
}
Offcourse in C++ this is much more readable since you can 'pass by reference': Code:
void change(int& i)
{
i=256;
}
int main()
{
int i = 3;
change(i);
cout << i << endl ;
return 0;
}
And your problem in C++, with some validation (ALWAYS do this in C!!!!!!! Also when you're 100% sure that it would work!!!!): Code:
void memAlloc(int& *i)
{
i=(int*)malloc(sizeof(int));
return i;
}
int main()
{
int *i=NULL;
if(memAlloc(i) != NULL)
{
*i = 100;
cout << *i << endl ;
}
return 0;
}
|
|
#4
|
|||
|
|||
|
Q2: B-Com is correct on this point. A good debugger environment would give you a message/warning on this point.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Understanding malloc... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|