Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, Nymphodora, Quad), 923 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
redeclaring variables #183621
02/13/08 06:51
02/13/08 06:51
Joined: Feb 2007
Posts: 50
I
iuselitec Offline OP
Junior Member
iuselitec  Offline OP
Junior Member
I

Joined: Feb 2007
Posts: 50
void test()
{
int i = 10;
int i = 10;
}

if you do that with other compilers they complain about the second declaration of the variable. lite-c does not complain but what will happen? does it simply ignore the second int?

you can even do:

void test()
{
int i = 10;
float i = 10;
}

Re: redeclaring variables [Re: iuselitec] #183622
02/14/08 04:24
02/14/08 04:24
Joined: Dec 2006
Posts: 78
Nevada, USA
Futurulus Offline
Junior Member
Futurulus  Offline
Junior Member

Joined: Dec 2006
Posts: 78
Nevada, USA
A little bit of experimentation seems to indicate that it replaces the first declaration with the second. It even allocates a new block of memory for the new declaration and initializes it to zero if you don't assign it a value. Interesting, but it shouldn't cause problems--unless the second declaration is on accident, in which case you might not realize you are setting the variable to zero.

Re: redeclaring variables [Re: Futurulus] #183623
02/14/08 07:16
02/14/08 07:16
Joined: Feb 2007
Posts: 50
I
iuselitec Offline OP
Junior Member
iuselitec  Offline OP
Junior Member
I

Joined: Feb 2007
Posts: 50
but could it cause a memory leak? does the first one get freed correctly?

Re: redeclaring variables [Re: iuselitec] #183624
02/16/08 21:18
02/16/08 21:18
Joined: Dec 2006
Posts: 78
Nevada, USA
Futurulus Offline
Junior Member
Futurulus  Offline
Junior Member

Joined: Dec 2006
Posts: 78
Nevada, USA
Actually, I think it is freed correctly. If I write a function that has a multiply defined variable like that, with a script calling a function in a while loop, the addresses of the "two" variables are different (0x0012FDCC and 0x0012FDD0), but these addresses don't move up from call to call, as they would if memory were being left allocated. So I don't think you need to worry about memory leaks.
Code:
int quit()
{
sys_exit(NULL);
}

int no_leaks()
{
int i = 0;

int *p = &i;

int i = 100;

printf("First:\naddr=%p", p);
printf("Second:\naddr=%p", &i);

return 0;
}

int main()
{
on_esc = quit;
while(1)
{
no_leaks();
wait(-5);
}
}



Re: redeclaring variables [Re: Futurulus] #183625
02/18/08 10:50
02/18/08 10:50
Joined: Feb 2007
Posts: 50
I
iuselitec Offline OP
Junior Member
iuselitec  Offline OP
Junior Member
I

Joined: Feb 2007
Posts: 50
thanks! we could also try it with bigger structs and see if memory raises in the task manager.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1