Thread: [Dev-C++] What is a static function
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <vil...@ya...> - 2001-08-02 14:00:29
|
I don't understand what is like to declare a function as "static".
For example:
static void initBV() {
if (!inittedBV)
{
int i;
for (i=0; i<ArraySize(bV); i++)
bV[i] = -1;
bV['a'] = bV['A'] = A_B_V;
bV['b'] = bV['B'] = B_B_V;
bV['c'] = bV['C'] = C_B_V;
bV['d'] = bV['D'] = D_B_V;
bV['e'] = bV['E'] = E_B_V;
inittedBV = TRUE;
}
}
What is that code doing?
If I "malloc" something inside the function, what happens? Is the space
able to be freed outside of the function?
How is that "int i" variable managed, is it created always but not used
unless the "if" condition is true?
Can I put this kind of conditional declaration of functions in a non-static
function?
I don't know much of it, you see! :)
_______________________________________________________________
Do You Yahoo!?
Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
http://messenger.yahoo.es
|
|
From: <vil...@ya...> - 2001-08-02 13:19:33
|
--- Albert Vilella Bertran <vil...@ya...> escribió: > I don't
understand what is like to declare a function as "static".
> For example:
>
> static void initBV() {
> if (!inittedBV)
> {
> int i;
> for (i=0; i<ArraySize(bV); i++)
> bV[i] = -1;
> bV['a'] = bV['A'] = A_B_V;
> bV['b'] = bV['B'] = B_B_V;
> bV['c'] = bV['C'] = C_B_V;
> bV['d'] = bV['D'] = D_B_V;
> bV['e'] = bV['E'] = E_B_V;
> inittedBV = TRUE;
> }
> }
>
> What is that code doing?
> If I "malloc" something inside the function, what happens? Is the space
> able to be freed outside of the function?
> How is that "int i" variable managed, is it created always but not used
> unless the "if" condition is true?
> Can I put this kind of conditional declaration of functions in a non-static
> function?
>
> I don't know much of it, you see! :)
>
>
> _______________________________________________________________
> Do You Yahoo!?
> Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
> http://messenger.yahoo.es
>
_______________________________________________________________
Do You Yahoo!?
Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
http://messenger.yahoo.es
|
|
From: <vil...@ya...> - 2001-08-02 16:46:58
|
> I don't
> understand what is like to declare a function as "static".
> > For example:
> >
> > static void initBV() {
> > if (!inittedBV)
> > {
> > int i;
> > for (i=0; i<ArraySize(bV); i++)
> > bV[i] = -1;
> > bV['a'] = bV['A'] = A_B_V;
> > bV['b'] = bV['B'] = B_B_V;
> > bV['c'] = bV['C'] = C_B_V;
> > bV['d'] = bV['D'] = D_B_V;
> > bV['e'] = bV['E'] = E_B_V;
> > inittedBV = TRUE;
> > }
> > }
> >
> > What is that code doing?
> > If I "malloc" something inside the function, what happens? Is the space
> > able to be freed outside of the function?
> > How is that "int i" variable managed, is it created always but not used
> > unless the "if" condition is true?
> > Can I put this kind of conditional declaration of functions in a non-static
> > function?
> >
> > I don't know much of it, you see! :)
> >
> >
> > _______________________________________________________________
> > Do You Yahoo!?
> > Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
> > http://messenger.yahoo.es
> >
>
> _______________________________________________________________
> Do You Yahoo!?
> Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
> http://messenger.yahoo.es
>
_______________________________________________________________
Do You Yahoo!?
Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
http://messenger.yahoo.es
|
|
From: Ioannis V. <no...@ya...> - 2001-08-02 17:29:06
|
static functions or global variables have scope only at the compilation unit (in normal computers: file) where they are defined. They can not be seen outside of their compilation unit. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Albert Vilella Bertran > Sent: Thursday, August 02, 2001 2:48 PM > To: dev...@li... > Subject: [Dev-C++] What is a static function >=20 >=20 > I don't understand what is like to declare a function as=20 > "static". For example: >=20 > static void initBV() { > if (!inittedBV) > { > int i; > for (i=3D0; i<ArraySize(bV); i++) > bV[i] =3D -1; > bV['a'] =3D bV['A'] =3D A_B_V; > bV['b'] =3D bV['B'] =3D B_B_V; > bV['c'] =3D bV['C'] =3D C_B_V; > bV['d'] =3D bV['D'] =3D D_B_V; > bV['e'] =3D bV['E'] =3D E_B_V; > inittedBV =3D TRUE; > } > } >=20 > What is that code doing? > If I "malloc" something inside the function, what happens? Is=20 > the space able to be freed outside of the function? How is=20 > that "int i" variable managed, is it created always but not=20 > used unless the "if" condition is true? Can I put this kind=20 > of conditional declaration of functions in a non-static function? >=20 > I don't know much of it, you see! :) >=20 >=20 > _______________________________________________________________ > Do You Yahoo!? > Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu=20 > gente - http://messenger.yahoo.es >=20 > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users >=20 |
|
From: Tomas D. <do...@vo...> - 2001-08-02 15:17:51
|
Hi Albert,
Well, functions declared with static memory class
can't be used in (called from) another module (file) then the one they ar=
e
declared in. It is the same with variables.
Have a look at this...
static void f(void); // this global function and
static int n; // this global variable
// are visible only in the module they are
declared in
Have you got the idea? An easy stuff...
If you want to know what is all this for, just ask.
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of
> Albert Vilella Bertran
> Sent: Thursday, August 02, 2001 2:48 PM
> To: dev...@li...
> Subject: [Dev-C++] What is a static function
>
>
> I don't understand what is like to declare a function as "static".
> For example:
>
> static void initBV() {
> if (!inittedBV)
> {
> int i;
> for (i=3D0; i<ArraySize(bV); i++)
> bV[i] =3D -1;
> bV['a'] =3D bV['A'] =3D A_B_V;
> bV['b'] =3D bV['B'] =3D B_B_V;
> bV['c'] =3D bV['C'] =3D C_B_V;
> bV['d'] =3D bV['D'] =3D D_B_V;
> bV['e'] =3D bV['E'] =3D E_B_V;
> inittedBV =3D TRUE;
> }
> }
>
> What is that code doing?
> If I "malloc" something inside the function, what happens? Is the spac=
e
> able to be freed outside of the function?
yes, you can use the memory block and free it, too, if you bring the hand=
ler
of the memory block out of your function.
> How is that "int i" variable managed, is it created always but not use=
d
> unless the "if" condition is true?
it is created always, I deeply believe
functions like this are completely build before they are called
> Can I put this kind of conditional declaration of functions in a
> non-static
> function?
yeah!
>
> I don't know much of it, you see! :)
>
Heh.
Have a good time.
>
> _______________________________________________________________
> Do You Yahoo!?
> Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu gente -
> http://messenger.yahoo.es
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
|
|
From: Dave H. <dhe...@ch...> - 2001-08-02 19:05:12
|
I may be able to help you out some...
Any function that is declared "static" cannot be called by any routine th=
at
is not contained in the same source file. If you were to have two files,
"main.cpp" and "bv.cpp", in your project, and initBV() was contained in
"bv.cpp" and declared as "static", functions in "main.cpp" would not be a=
ble
to call it.
It boils down to whether or not the function handle "initBV" ends up in t=
he
global symbol table used by the linker. If it's declared "static", the
handle won't be in the table, and the linker won't be able to resolve it.
This is good for limiting scope of callability when you don't have C++
classes with private elements. You might compare it to a private method =
in
a class.
The function itself exists and is compiled just as any other function.
Consequently, the variable "i" will be put on the stack with any other lo=
cal
variables.
Hope this helps,
Dave
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Albert
Vilella Bertran
Sent: Thursday, August 02, 2001 7:48 AM
To: dev...@li...
Subject: [Dev-C++] What is a static function
I don't understand what is like to declare a function as "static".
For example:
static void initBV() {
if (!inittedBV)
{
int i;
for (i=3D0; i<ArraySize(bV); i++)
bV[i] =3D -1;
bV['a'] =3D bV['A'] =3D A_B_V;
bV['b'] =3D bV['B'] =3D B_B_V;
bV['c'] =3D bV['C'] =3D C_B_V;
bV['d'] =3D bV['D'] =3D D_B_V;
bV['e'] =3D bV['E'] =3D E_B_V;
inittedBV =3D TRUE;
}
}
What is that code doing?
If I "malloc" something inside the function, what happens? Is the space
able to be freed outside of the function?
How is that "int i" variable managed, is it created always but not used
unless the "if" condition is true?
Can I put this kind of conditional declaration of functions in a non-stat=
ic
function?
I don't know much of it, you see! :)
_______________________________________________________________
Do You Yahoo!?
Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu gente -
http://messenger.yahoo.es
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Vassili <v.v...@bt...> - 2001-08-03 02:41:03
|
If you do use malloc in a function then you should return the pointer
(unless you free the memory in the function).
You see, once the function is finished with, it's basically destroyed and
you will have memory allocated but with nothing connecting it. When you call
the function again it allocates more memory, and slowly, slowly you chew
your memory away.
This is basically oop, you create an object, then you destroy it.
best of luck
Vas.
----- Original Message -----
From: "Albert Vilella Bertran" <vil...@ya...>
To: <dev...@li...>
Sent: Thursday, August 02, 2001 1:47 PM
Subject: [Dev-C++] What is a static function
> I don't understand what is like to declare a function as "static".
> For example:
>
> static void initBV() {
> if (!inittedBV)
> {
> int i;
> for (i=0; i<ArraySize(bV); i++)
> bV[i] = -1;
> bV['a'] = bV['A'] = A_B_V;
> bV['b'] = bV['B'] = B_B_V;
> bV['c'] = bV['C'] = C_B_V;
> bV['d'] = bV['D'] = D_B_V;
> bV['e'] = bV['E'] = E_B_V;
> inittedBV = TRUE;
> }
> }
>
> What is that code doing?
> If I "malloc" something inside the function, what happens? Is the space
> able to be freed outside of the function?
> How is that "int i" variable managed, is it created always but not used
> unless the "if" condition is true?
> Can I put this kind of conditional declaration of functions in a
non-static
> function?
>
> I don't know much of it, you see! :)
>
>
> _______________________________________________________________
> Do You Yahoo!?
> Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
> http://messenger.yahoo.es
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|