H
Hassan Iqbal
Hi,
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
for(i=0;i<n;i++) /* i and n are integers */
p= malloc(n*sizeof p);
then in that case is this command sufficient to free all the allocated
memory:
free(p);
or do i need to free it like this:
for(i=0;i<n;i++)
free(p);
free(p);
(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */
int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */
in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.
thanks,
hassan
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
for(i=0;i<n;i++) /* i and n are integers */
p= malloc(n*sizeof p);
then in that case is this command sufficient to free all the allocated
memory:
free(p);
or do i need to free it like this:
for(i=0;i<n;i++)
free(p);
free(p);
(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */
int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */
in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.
thanks,
hassan