Click here to Skip to main content
15,914,163 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Allocate memory inside function ?? Pin
fx920014-Mar-07 22:57
fx920014-Mar-07 22:57 
AnswerRe: Allocate memory inside function ?? Pin
prasad_som14-Mar-07 22:56
prasad_som14-Mar-07 22:56 
GeneralRe: Allocate memory inside function ?? Pin
fx920014-Mar-07 23:07
fx920014-Mar-07 23:07 
QuestionRe: Allocate memory inside function ?? Pin
prasad_som14-Mar-07 23:12
prasad_som14-Mar-07 23:12 
AnswerRe: Allocate memory inside function ?? Pin
fx920014-Mar-07 23:22
fx920014-Mar-07 23:22 
AnswerRe: Allocate memory inside function ?? Pin
prasad_som14-Mar-07 23:28
prasad_som14-Mar-07 23:28 
GeneralRe: Allocate memory inside function ?? Pin
fx920014-Mar-07 23:37
fx920014-Mar-07 23:37 
GeneralRe: Allocate memory inside function ?? Pin
Roger Stoltz15-Mar-07 0:43
Roger Stoltz15-Mar-07 0:43 
What I think you need to understand after having read your previous posts, is that when you call a function the arguments passed are copies on the stack.
Have a look here[^] if you're not sure what the stack is and how it is used.

Let's examine this with some code examples.
A function called AddOne is used for increasing a variable by the value of 1.
void AddOne( int nTheVariable )
{
    nTheVariable = nTheVariable + 1;
}

void main()
{
    int nMyValue = 0;
    printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0."
    AddOne( nMyValue );
    printf( "The value is %d.\n", nMyValue ); // Also prints "The value is 0."
}
When AddOne() is declared as above, the value of nMyValue is copied to the stack and read by AddOne(). When the function returns is has indeed increased its local copy (nTheVariable) by one, but nMyValue still remains the same since it was a copy of it that was passed to the function.
If you want to alter the value of nMyValue, you have to pass its location as argument to AddOne(). "Location" in this aspect means "the address of".
In code it would look like this:
void AddOne( int* pnTheVariable )
{
    // Here we've got a copy of the address of the variable so
    // we can modify it at its original location 
    *pnTheVariable = *pnTheVariable + 1;
}

void main()
{
    int nMyValue = 0;
    printf( "The value is %d.\n", nMyValue ); // Prints "The value is 0."
    AddOne( &nMyValue ); // Pass a copy of the address of nMyValue
    printf( "The value is %d.\n", nMyValue ); // Prints "The value is 1."
}

In your case you want to allocate memory and you always assign it to a pointer.
To be able to alter the address assigned to a pointer, you have to pass a copy of the address of the pointer to the function. This is still the same as above, but now you want to alter the value of a pointer.
In code it would look something like this if you want to allocate memory for three ints:
void Allocate( int** ppnTheMemory )
{
    *ppTheMemory = (int*)malloc( 3 * sizeof( int ) );
}

void main()
{
    int* pnMyValues = NULL;
    Allocate( &pnMyValue ); // Pass the address of the pointer
    if( pnMyValues )
    {
        free( pnMyValues );
        pnMyValues = NULL;
    }
}

The above means that your original code where you allocate memory for the structure inside the fill() function should look something like this:
void fill( struct mystruct** ppTheStruct );

void main()
{
    struct mystruct* pMyStruct = NULL;
    fill( &pMyStruct ); // Pass the address of the pointer
    ...
    if( pMyStruct )
    {
        free( pMyStruct ); // Don't forget to return what you've borrowed ;-)
        pMyStruct = NULL; // Not necessary but considered good practice
    }
}

void fill( struct mystruct** ppMyStruct )
{
    *ppMyStruct = (struct mystruct*)malloc( sizeof( struct mystruct ) );
    ....
}

Hope this makes things a little more clear for you.
Good luck.


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown

AnswerRe: Allocate memory inside function ?? Pin
fx920014-Mar-07 22:56
fx920014-Mar-07 22:56 
GeneralRe: Allocate memory inside function ?? Pin
fx920014-Mar-07 23:15
fx920014-Mar-07 23:15 
QuestionRun exe from mfc Pin
jeepoo14-Mar-07 21:25
jeepoo14-Mar-07 21:25 
AnswerRe: Run exe from mfc Pin
prasad_som14-Mar-07 21:38
prasad_som14-Mar-07 21:38 
AnswerRe: Run exe from mfc Pin
ThatsAlok14-Mar-07 22:42
ThatsAlok14-Mar-07 22:42 
GeneralRe: Run exe from mfc Pin
Hamid_RT15-Mar-07 19:18
Hamid_RT15-Mar-07 19:18 
GeneralRe: Run exe from mfc Pin
ThatsAlok15-Mar-07 20:37
ThatsAlok15-Mar-07 20:37 
GeneralRe: Run exe from mfc Pin
Hamid_RT16-Mar-07 0:40
Hamid_RT16-Mar-07 0:40 
GeneralRe: Run exe from mfc Pin
ThatsAlok16-Mar-07 0:43
ThatsAlok16-Mar-07 0:43 
GeneralRe: Run exe from mfc Pin
Hamid_RT16-Mar-07 0:59
Hamid_RT16-Mar-07 0:59 
GeneralRe: Run exe from mfc Pin
ThatsAlok16-Mar-07 1:03
ThatsAlok16-Mar-07 1:03 
GeneralRe: Run exe from mfc Pin
Hamid_RT16-Mar-07 1:15
Hamid_RT16-Mar-07 1:15 
AnswerRe: Run exe from mfc Pin
David Crow15-Mar-07 2:59
David Crow15-Mar-07 2:59 
Questionhow to lunch 32 bit exe from 64 bit process Pin
aaaan14-Mar-07 21:00
aaaan14-Mar-07 21:00 
AnswerRe: how to lunch 32 bit exe from 64 bit process Pin
toxcct14-Mar-07 22:11
toxcct14-Mar-07 22:11 
GeneralRe: how to lunch 32 bit exe from 64 bit process Pin
aaaan14-Mar-07 23:17
aaaan14-Mar-07 23:17 
QuestionRe: how to lunch 32 bit exe from 64 bit process Pin
David Crow15-Mar-07 3:01
David Crow15-Mar-07 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.