About Memory Allocation and function calls
- From: "william" <william.maji@xxxxxxxxx>
- Date: 21 Mar 2007 12:45:39 -0700
My situation is here:
an array of two dimension can only be defined locally within a
function(because the caller don't know the exact size ). Then the
question is: how should the caller access this array for future use?
The code is:
**********************
caller()
{
...
function();
other code also wish to access the array elements in function, how
should I do?
}
function()
{
calculation I want to encapsulate within this function.........
...
got the size: 'num_of_row' and 'num_of_col'
char maze[num_of_row][num_of_col];
...
code to fill the array
return;
}
I consider to use malloc() to allocate memory for this array, and
return the pointer to this run time allocated memory back to the
caller, however, another question arise: can the caller possibly
access this local memory(got from malloc())? because I think it's
still only belong to the function being called----rules of scope
suppose to work.
Can any one explain the memory allocation when compiling (in stack)
and at run time (heap)?
And what kind of rules are on top of these two different kinds of
resources? Which should C programmer should pay attention to?
I am kind of lost into these concepts when I debug my program:-)
Best
Ji
.
- Follow-Ups:
- Re: About Memory Allocation and function calls
- From: Barry Schwarz
- Re: About Memory Allocation and function calls
- From: Francine . Neary
- Re: About Memory Allocation and function calls
- Prev by Date: Re: K&R2, section1.6, solution TOO complicated (was: K&R2, section1.6, exercise 1-13)
- Next by Date: Re: system() function and current working directory
- Previous by thread: email message parser wrote in C available (not in C++)
- Next by thread: Re: About Memory Allocation and function calls
- Index(es):
Relevant Pages
|