About Memory Allocation and function calls



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

.



Relevant Pages

  • Re: array verses ArrayList
    ... > I have a class that contains a collection of reference types. ... > needs to have a method that returns the collection to the caller. ... If the size cannot change, then no, an Array of MyObject is preferable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About Memory Allocation and function calls
    ... function(because the caller don't know the exact size). ... how should the caller access this array for future use? ... Allocated memory remains allocated until it is explicitly freed. ... the calling function has access to. ...
    (comp.lang.c)
  • Re: out param question
    ... an arbitrary length array of bytes.... ... You allocate a block of memory with CoTaskMemAlloc, ... it in *pData and its length in *pSize. ... The caller eventually frees the ...
    (microsoft.public.win32.programmer.ole)
  • Re: Passing arrays arguments using remoting versus calling directly
    ... .NET remoting will not copy the array's elements back to the ... > array. ... > to all Serializable objects. ... > to the caller so they can see the changes made, ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: dynamic mem alloc q
    ... I seek to dynamically allocate memory of an array of unsigned ... memory each of those boxes is going to take. ... But just as soon as a figure out longs, ...
    (comp.lang.c)