memory allocation in subroutine


 
Thread Tools Search this Thread
Top Forums Programming memory allocation in subroutine
# 1  
Old 05-28-2009
memory allocation in subroutine

Hi everyone,

I'm not new to C programming, but I'm having question regarding the memory allocation of a pointer variable which, for instance, will be declared in main(), but its memory will be allocated in subroutine.

To clearify my question, I provide a small working example:
Code:
#include <stdlib.h>

void subroutine(void **ptr);

main()
{
  void **ptr  = NULL;
 
  subroutine(&ptr);

  return 0;
}

void subroutine(void **ptr)
{
   *ptr = (void*) malloc (10);
}

Why do I have to declare that variable as a pointer to a pointer?
Can someone explain that example to me? Why I can't simply pass the address of "ptr" to the subroutine by "subroutine(ptr);". Why do I have to apply an additional address operator? What is the result of "&ptr", because I thought "ptr" is already the address.

That is not resonable to me.

From my understanding that is what I would have done:
Code:
#include <stdlib.h>

void subroutine(void *ptr);

main()
{
  void *ptr  = NULL;
 
  subroutine(ptr);

  return 0;
}

void subroutine(void *ptr)
{
   ptr = (void*) malloc (10);
}

Thanks for your help!

Regards,
Maik
# 2  
Old 05-28-2009
The problem with your latter version is that the variable in main() won't be modified at all. In the subroutine, only the ptr local to the subroutine will be modified. To allow the ptr in main() to be modified, you are required to take a pointer to that variable, and thus requiring '&' on the calling side and an additional '*' on the callee side. This is true regardless of the type of the variable. It just happens that the type this time is void*. If the variable is an "int", then the parameter in the subroutine signature will be "int*".

One thing you need to keep in mind, is that parameters are passed by value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory allocation problem

I am using ubuntu. I have written a program to calculate prime factors. it works perfectly fine till entered number is less than 9989 (or so ) but when one enters a number higher than that, for example 15000, it does not work. Can anyone guide me whats the problem ? although new codes are welcome,... (2 Replies)
Discussion started by: Abhishek_kumar
2 Replies

2. Programming

C++/ROOT Memory Allocation?

Hello, I am new to C++ programming, so I'm still getting a feel for things. I recently wrote a simple C++ program (to be used as a ROOT Macro) to conduct a statistical analysis of a varied version of the Monty Hall problem (code below). Basically, the programs runs a few simple calculations to... (7 Replies)
Discussion started by: Tyler_92
7 Replies

3. Programming

memory allocation for string in C

hi in the following code, how the memory is allocated for a1 which holds the values of a2 after cpy function call. #include <stdio.h> #include <string.h> void cpy(char* d, const char* s){ while(*d++=*s++); } main(){ char* a1; char* a2="done"; cpy(a1,a2); ... (3 Replies)
Discussion started by: mprakasheee
3 Replies

4. Shell Programming and Scripting

memory allocation to a variable

hello all.. i'm a beginner in shell scripting. I need to know what is really happening when we are creating a variable in shell scripting? how memory is allocated for that variable? (3 Replies)
Discussion started by: aarathy
3 Replies

5. Programming

Memory Allocation Query

When we dynamically allocate the memory say 100 integers say int *x = new int(1000); then does entire chunk of memory gets allocated at once after the completion of the statement? I mean will the the concept of page fault come into picture over here? (3 Replies)
Discussion started by: rupeshkp728
3 Replies

6. Programming

Dynamic Memory Allocation

Hello Guys I have a small confusion in the dynamic memory allocation concept. If we declare a pointer say a char pointer, we need to allocate adequate memory space. char* str = (char*)malloc(20*sizeof(char)); str = "This is a string"; But this will also work. char* str = "This... (2 Replies)
Discussion started by: tene
2 Replies

7. Programming

Memory allocation in C

Hi Experts I need some help in static memory allocation in C. I have a program in which I declared 2 variables, one char array and one integer. I was little surprised to see the addresses of the variables. First: int x; char a; printf("%u %u\n', &x, a); I got the addresses displayed... (2 Replies)
Discussion started by: unx_freak
2 Replies

8. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

9. HP-UX

HP-UX memory usage allocation

Hi all, I have a HP-UX Server with 4 gigabytes of physical RAM. When I use the 'Glance' utility to see what my memory utilization is, my memory usage shows up maxed out at 99%. I shut off all the known processes that I'm running on that box and the memory utilization is still at 78% (with Swap... (3 Replies)
Discussion started by: dehuang83
3 Replies

10. UNIX for Dummies Questions & Answers

memory allocation

I would like to know how I could allocate some more memory to a process. Please note that I am not the root user. (1 Reply)
Discussion started by: sagar
1 Replies
Login or Register to Ask a Question