Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pointers - passing to functions

Posted on 1998-09-18
4
Medium Priority
?
234 Views
Last Modified: 2010-04-01
I am trying to pass pointers to and from a function.  I have accomplished what I wanted to do, but I think I might have a potential problem.

In My Main Function I created a pointer to type int.  This pointer is going to point to several integer values obtained from function GetChainWheelSizes.

The two variables declared in main are going to be used throughout the entire program in several functions.  

Question : In My function I declare a pointer and allocate memory for it to point at.  When this function is done, I return the pointer.  When I free the memory at the end of the program am I freeing the memory that I allocated in the function since I returned the pointer???

Question : Since I have to use iNumberChainWheels and pChainWheelSizes throughout the program is there a better way to do this??


int main()
 {
 
   int* pChainWheelSizes = NULL ;   // pointer to point to several integers
   int iNumberChainWheels = 0 ;       // gets user input of how many ints stored in pointer

   pChainWheelSizes = GetChainWheelSizes( &iNumberChainWheels );

// Test To See Whats In  pChainWheelSizes

   printf("\nAfter Function Call");
   for ( iLoopVariable = 0 ; iLoopVariable < iNumberChainWheels ; iLoopVariable++ )
   {
      printf("%d ", pChainWheelSizes[iLoopVariable]) ;
    }

 free( pChainWheelSizes );    // is this really freeing the memory allocated in the function?

return 0 ;

}


/*  This function takes iNumberChainWheels whihc is zero.  I only pass it as a pointer so I can use this variable throughout the rest of the program.  I am returning a pointer that holds all the integers entered by the user. */

int * GetChainWheelSizes( int* iNumberChainWheels )
{

 int iLoopVariable = 0 ;
 int *pChainWheelSizes ;
 

   printf("\nNumber Of Chainwheels: ");
   scanf("%d", iNumberChainWheels);
 
   if ( *iNumberChainWheels == 0 )
  return 0;

 if ( ( pChainWheelSizes = (int *) malloc ( sizeof( int ) * (*iNumberChainWheels)) ) == NULL )
 {
  printf(" \n Unable To Allocate Memory For Chain Wheels.");
  return 0;
 }

 printf("\nPlease Provide Chainwheel sizes in ascending order: ");
   
 for ( iLoopVariable = 0 ; iLoopVariable < *iNumberChainWheels ; iLoopVariable++ )
 {
        printf("\nPlease Provide Chainwheel sizes #%d : ", iLoopVariable + 1);
  scanf("%d", &pChainWheelSizes[iLoopVariable]) ;
 }



 // Test To See Whats In  pChainWheelSizes
    for ( iLoopVariable = 0 ; iLoopVariable < *iNumberChainWheels ; iLoopVariable++ )
 {
  printf("%d ", pChainWheelSizes[iLoopVariable]) ;
 }

    return ( pChainWheelSizes ) ;  

}
0
Comment
Question by:larockd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Accepted Solution

by:
bobbym earned 60 total points
ID: 1173137
Generally speaking you have no problem with this pointer. After all this is the concept of dynamic memory allocation :). As long as you do not change the pointer the free() function will work correctly.
  Well.. regarding the second question you may consider implementing a small class that wraps the pointer and the size so that when the object is destroyed in its destructor it will free the allocated memory. Then declare this object as global and you do not have problems. Like that :

class CWrap
{
public:
CWrap()
{
 m_pInt = NULL;
 m_nSize = 0;
}

CWrap(int nSize)
{
  m_nSize = nSize;
  m_pInt = (int *) malloc ( sizeof( int ) * (*m_nSize)) ;

  if (m_pInt == NULL)
    m_nSize = 0;

}
~CWrap()
{
   if (m_pInt == NULL)
   {
       free(m_pInt);
       m_pInt = NULL
   }
}

int *m_pInt;
int m_nSize;
};

Thus you guarantee that you will not ommit the free call and have leaks (cause the compiler takes care for you).

Hope that helped :))
0
 

Expert Comment

by:bobbym
ID: 1173138
Ooooppps a small bug in the destructor :)) should be !=
0
 
LVL 22

Expert Comment

by:nietod
ID: 1173139
>> Question : Since I have to use iNumberChainWheels
>> and pChainWheelSizes throughout the program is there
>> a better way to do this??
Beginners are often discouraged from using global variables because there is a big temptation to use them inappropriately.  This leads to incomprehensible code, nasty bugs, and program designs that can't be expanded on in many ways.  However, there are times when globals are appropriate.  This might be one.  You should make these global variables and then not pass them as parameters/return values, if the following is true

a)  The program needs only one copy of these values.
b)  The values are needed in many different procedures in the program.  
c)  The values have a significance that is global to the the program.  That is, they tend to affect the way the program works overall, not just one part of it.
0
 

Author Comment

by:larockd
ID: 1173140
Thanks Bobby, and Nietod
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

610 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