http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

TheGrumpyProgrammer



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Pointing to the wrong thing......



"*** I freed the space just before I return a pointer from the function...
and I belive therein lies my problem....."

Therein lies your problem is correct :) When you free() the space, you are
saying to the computer/compiler "I am done with this." After that, the
results are undefined as to what happens to the space and/or access
violations regarding that space.

Only free() space when you are really done with it, since often compilers,
especially in debug mode, will write over that space immediately.

The likely reason that a, b, and c all point to the same space is that when
for free() the space then malloc() it again, the same address is being
reused by the (smart) allocation system.

Andrew Bortz

-----Original Message-----
From: gameprogrammer-owner@gameprogrammer.com
[mailto:gameprogrammer-owner@gameprogrammer.com]On Behalf Of Josiah
Avery
Sent: Sunday, May 07, 2000 7:30 PM
To: gameprogrammer@gameprogrammer.com
Subject: Re: Pointing to the wrong thing......



>Hi,
>ok i'm not a expert programmer, so if i get anything wrong
>don't shout at me :)

*** nor am I. I've done more low level type stuff then high.

>Isn't it bad to return the address of a local varible...
>once the function ends the varible will be taken from the
>stack and the address would no longer be valide.
>

**** Hmmm, I belive you are correct, but I am doing something different. As
I have learnt, the following things happen when a subroutine is called.

1. Things such as the stack pointer, and PC value, branch address etc are
saved (pushed).

2. Parameters passed to the function are pushed onto the stack

3. Memory is allocated for any local varibles within the subroutine, the
starting address of the return value(s) is calcuated and space is allocated
for return value(s).

4. The function executes; return values are placed into the afforementioned
address spaces.

5. Everything is popped from the stack and the program returns to the
address initaly pushed from the PC.

*** so in my function, I am returning a pointer to this array of memory
elsewhere. I know this is correct because the function does so.

>As ur other post stated you did actully malloc
>the space?
>
>lilke
>
>int * results = (int *)malloc(sizeof(int));
>
>right?

*** I used the C++ convention for allocating memory ( new & delete ops )

>did you then at the end of the function free the
>memory?

*** I freed the space just before I return a pointer from the function...
and I belive therein lies my problem.....

>wouldn't that cause the memory location that the
>function returned become invalid?

*** I am not sure. I whould say that that makes sense... sorta, but the
functions works for when called once..... I think you have sparked something
though.....

>would then it also mean the next time
>you call the function it allocates the same
>peice of memory, therefore getting the
>results u showed?

*** I think so....

>Also is it strictly nessarey to use the
>pointers...it mayde the function very messy...

*** Actualy, using pointers made it much more neat. I could have combined
the two functions into 1, but one of them includes a version of Breshehans

bad spelling ) line algo, which takes up a bit of space.... I opted to break
up the function to reduce overhead a bit.

Thanks a lot.

Josiah.



>Evan,
>
>
>****
>*Evan Marchant
>*ICQ#15055534
>*"Once one problems solved, ten are around the corner"
>****
>=================================================================
>The GameProgrammer.Com mailing list is for the open discussion
>of any topic related to the art, science, and business of
>programming games. This list is especially tolerant of beginners.
>We were all beginners once
>
>To SUBSCRIBE or UNSUBSCRIBE please visit:
>http://gameprogrammer.com/mailinglist.html

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html