#### Archived

This topic is now archived and is closed to further replies.

# C scope question

## Recommended Posts

Since C doesn't have garbage collection or anything, I thought the following code would fill my array with AlienShip structures:
	for(i = 0; i < NUM_ALIENS; i++)
{
AlienShip alien_ship;
alien_ship.x = rand();
alien_ship.y = rand();
alien_ships[ i ] = &alien_ship;
// alien_ships defined as:
// AlienShip *alien_ships[NUM_ALIENS]

}

The results were somewhat unpredictable. Later, when referencing alien_ships[ i ]->x, (or y), the results were usually garbage. The solution was to do this:
	for(i = 0; i < NUM_ALIENS; i++)
{
alien_ships[ i ] =(AlienShip *)malloc(sizeof(AlienShip));
alien_ships[ i ]->x = rand();
alien_ships[ i ]->y = rand();
}

So I fixed my problem, but I am a little disturbed that the first bit of code didn't work. If I want to point to a local variable that will soon lose scope, do I always have to completely copy it? Maybe I'm just rusty on pointers. ------------- SpaceRook Latest Project: Paper Shredder Visit my homepage for other projects! (Edit: changed it so the array indices showed up -- Kylotan.) Edited by - Kylotan on February 7, 2002 12:49:57 PM

##### Share on other sites
If you assign an index of your array to point to a local variable, isn''t it logical that the transient memory would become garbage? The memory might be reused (with overlap) in any order.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

##### Share on other sites
Local variables are created on the stack. At the end of their scope (where the first closing curly bracket is encountered) the stack top is brought back down to where it was upon entry into the open curly bracket. From there, the stack will move up again (inevitably) when new local variables are created, destroying whatever was there.

Local scope is just that: local scope. So, in effect, a form of garbage collection is happening predictably and consistently at the end of a code block.

___________________________________

Edited by - bishop_pass on February 6, 2002 11:57:05 PM

##### Share on other sites
quote:
Original post by SpaceRook
So I fixed my problem, but I am a little disturbed that the first bit of code didn't work. If I want to point to a local variable that will soon lose scope, do I always have to completely copy it? Maybe I'm just rusty on pointers.

Point to local variables all you want. Just don't expect the pointers to have any validity after the variable you are pointing to loses scope.

Consider using static variables. They maintain their value after losing scope, but are only accesible from within the scope that they were created in.

___________________________________

Edited by - bishop_pass on February 6, 2002 12:04:18 AM

##### Share on other sites
I believe this is what Oluseyi said, but at first glance I
didn''t get it cause I had seen the problem but worded it

use the original code but lose the ''&''.

• ### Forum Statistics

• Total Topics
628384
• Total Posts
2982390

• 10
• 9
• 15
• 24
• 11