Re: [Dev-C++] #DEFINE versus cont variables in array inicializations
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2003-05-31 11:08:35
|
Hello Maxim!
Maxim Yemelyanov wrote:
> Hello, OROSZI!
Balázs :) This is my first name. We just write it the opposite way.
> I just dont know how function stack works. When I call foo(), where its stack
> starts? Just on caller's stack's free space?
>
> Because what I thought was:
> 1) getC(1) creates a local object at address xxxx (on its stack), this object
> will be destroyed at the end of f().
No, it will be destroyed at the end of getC(1). But here is where gcc
optimises. It does not destroy it, instead it simply "returns" it.
> 2) getC(2) creates a local object _at_the_same_ address xxxx.
No, why would it? It is a different call, different local variable. We
would be in BIG trouble if gcc would behave like that...
> 3) Since both objects exist until end of f(), this can cause a trouble.
See above, like that, there is no problem :)
> But when I tried a simple test (added output to getC), I saw that two local
> objects are created at different addresses.
Of course. It is two DIFFERENT function calls. I was talking about the
local variable, and the returned object. Try this:
Store the address of the local variable in getC(x), and then check the
returned object's address too. Like this:
C* local_address;
C* returned_address;
getC(int)
{
C c(1);
local_address = &c;
return c;
}
main()
{
C c;
c = getC(1);
returned_address = &c;
/* Here, local_address and global_address "should" be the same */
/* TRY IT! */
}
> OB> I don't understand you clearly...
> That's because I don't see the whole picture (in all details). Thanks, your
> answer have cleared some areas of the topic to me :-)
> Moreover, since most of time I used pure C, such things as C++ classes and
> references, passed between functions (and optimization of all this stuff) is
> still not enough clear to me.
> That's why I sometimes ask stupid questions. Sorry :-\
Ahh. No problem :) I am not enough familiar with C++ yet either. :)
> What are these 10%? When I should be careful about optimization?
I don't know. I haven't got any trouble with optimization yet. I have
seen LARGE projects, that use -O3 optimisation, and they work fine. So I
think you can safely enable it. GCC is getting better and better, and
GCC 3.2 can be considered modern enough not to f**k up the program.
--
Greetings,
Balázs
|