473,480 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Mar 21 '07 #1
3 1849
On Mar 21, 7:45 pm, "william" <william.m...@gmail.comwrote:
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?
<snip>
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.

Mar 21 '07 #2
Fr************@googlemail.com wrote:
>
On Mar 21, 7:45 pm, "william" <william.m...@gmail.comwrote:
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?

<snip>
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.
/* 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 */

--
pete
Mar 21 '07 #3
On 21 Mar 2007 12:45:39 -0700, "william" <wi**********@gmail.com>
wrote:
>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
[i][j] use the expression array[i*first_dimension+second_dimension].
*/
Remove del for email
Mar 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

72
3526
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
18
2417
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
50
2786
by: Joseph Casey | last post by:
Greetings. I have read that the mistake of calling free(some_ptr) twice on malloc(some_data) can cause program malfunction. Why is this? With thanks. Joseph Casey.
16
2293
by: Pedro Graca | last post by:
I have a file with different ways to write numbers ---- 8< (cut) -------- 0: zero, zilch,, nada, ,,,, empty , void, oh 1: one 7: seven 2: two, too ---- >8 -------------- ...
74
4580
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
1
1451
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as...
94
4633
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
4
3690
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
14
3808
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.