About Memory Allocation and function calls

W

william

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
 
F

Francine.Neary

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?

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())?

Yes, of course.
because I think it's
still only belong to the function being called----rules of scope
suppose to work.

Just pass the pointer malloc() gives you back to the caller.
 
P

pete

Yes, of course.


Just pass the pointer malloc() gives you back to the caller.

/* BEGIN maze.c */

#include<stdio.h>
#include<stdlib.h>

char **caller(int num_of_row, int num_of_col);
char **function(int num_of_row, int num_of_col);

int main(void)
{
char **maze;
int index = 3;
int index2 = 5;

maze = caller(index, index2);
if (maze != NULL) {
while (index-- != 0) {
free(maze[index]);
}
free(maze);
}
return 0;
}

char **caller(int num_of_row, int num_of_col)
{
char **maze;
int index, index2;

maze = function(num_of_row, num_of_col);
if (maze != NULL) {
for (index = 0; index != num_of_row; ++index) {
for (index2 = 0; index2 != num_of_col; ++index2) {
printf("maze[%d][%d] is %d\n",
index, index2, maze[index][index2]);
}
}
}
return maze;
}

char **function(int num_of_row, int num_of_col)
{
char **maze;
int index, index2;

maze = malloc(num_of_row * sizeof *maze);
if (maze == NULL) {
return NULL;
}
for (index = 0; index != num_of_row; ++index) {
maze[index] = malloc(num_of_col * sizeof *maze[index]);
if (maze[index] == NULL) {
while (index-- != 0) {
free(maze[index]);
}
free(maze);
return NULL;
}
}
for (index = 0; index != num_of_row; ++index) {
for (index2 = 0; index2 != num_of_col; ++index2) {
maze[index][index2] = (char)(index + index2);
}
}
return maze;
}

/* END maze.c */
 
B

Barry Schwarz

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:)

Allocated memory remains allocated until it is explicitly freed. In
order for the calling function to know the address of the allocated
memory, the called function must make it available. There are several
techniques to make it available. Two popular ones are: it can be
returned with a return statement and it can be stored in a variable
the calling function has access to (such as a global variable).

In addition to the address of the allocated memory, the calling
function also needs to know the number of dimensions (always two in
your example) and the size of each.

Now that we know the called function must make three values available
to the calling function, return seems like a poor choice. However, if
the calling function passes some object addresses to the called
function, the called function can store the appropriate values in
those objects. Consider the following stripped down code just to
illustrate the technique.

int called_function(char **ptr_to_ptr, size_t *dim1, size_t *dim2){
*ptr_to_ptr = malloc(total_size_of_array);
*dim1 = size_of_first_dimension;
*dim2 = size_of_second_dimension;
return some_status_value;
}

int main(void){ /* calling function */
char *array;
size_t first_dimension;
size_t second_dimension;
inst status;
status = called_function(&array, &first_dimension,
&second_dimension);
/* If status indicates success, use the array. To access element
[j] use the expression array[i*first_dimension+second_dimension].
*/


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top