?
Solved

Explaination

Posted on 2006-05-30
5
Medium Priority
?
463 Views
Last Modified: 2010-04-15
Hi all,

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

void check( int * &ptr)
{
      int *tmp;
      
      tmp = (int*)malloc(sizeof(int));
      *tmp= 100;
      ptr= tmp;

      printf("in check is %d\n",*tmp);

}

void main ()
{
      int* t;

      check(t);

      printf("value is %d\n",*t);
}
/*
in check is 100
value is 100
Press any key to continue
*/

I just wonder that the tmp in chec() is a local variable. So have can its value still valid for t in main() , even though i know ptr = tmp. Please explain me the
check (int * &ptr) vs check( int *ptr). More examples are appreciated.
 Thanks.
0
Comment
Question by:valleytech
  • 2
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16794487
Hi valleytech,
> Please explain me the check (int * &ptr).
That is not C code, and instead that is C++ code.
In C++ that's a reference to a pointer, and it allows it to change the calling functions variable as to what it's pointing to.
You can't do that with C unless you use a pointer to a pointer.

Cheers!
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 800 total points
ID: 16794493
C way of doing it:
void check( int * *ptr)
{
    int *tmp;
     
     tmp = (int*)malloc(sizeof(int));
     *tmp= 100;
     *ptr= tmp; //Add asterick to dereference the pointer to a pointer **********************************

     printf("in check is %d\n",*tmp);
}

void main ()
{
     int* t;

     check(&t); //Pass pointer to a pointer **********************************

     printf("value is %d\n",*t);
}
0
 

Author Comment

by:valleytech
ID: 16794502
Hi Axter,
 How can the local variable still there after exit the check(). So far I know that local variable only still avaible if i use static.
 One more thing, if  i use
   printf("value is %d\n",t) ---> it doesn't work. COuld you please explain line by line? Thanks a lot.
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 1200 total points
ID: 16794672
Hi valleytech,

In function check(), tmp is a local (often called stack) variable.  It is created when the function is called and released when the function exits.

Within check() you assign a buffer (large enough to hold an integer value) from the heap and store the pointer in tmp.  Note that the buffer is allocated from the heap, NOT the stack.  When the function exits, tmp is discarded, but the buffer that you assigned from the heap is kept.  You return the pointer to that variable to main().  Well, actually, you set the pointer in main() from the passed parameter.

From within a function (any function) you can call any sequence of other functions which can then call any other sequence of functions.  When all of them return to your function the stack will look exactly like it did when you called the first function.  (Actually, the contents of the unassigned portion of the stack have no valid value and can be considered "random".)  But the heap can take on any shape.  You may have allocated no space from it, a millions small buffers or any quantity in between.  You could have even freed some buffers so that you have less assigned than when the function started.

So that's the key.  STACK space is the portion of memory that is used for temporary variable that are created and destroyed when functions are called and exit.  HEAP space is the portion of memory that you specifically allocate and stays assigned until you explicitly free it or the program exits.


Good Luck!
Kent
0
 

Author Comment

by:valleytech
ID: 16795406
Wow,
It's clear now Kdo. Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question