15 Mar, 2014, Davenge wrote in the 1st comment:
Votes: 0
I know what a static is, so please skip that part.

From an optimization stand-point, as that seems to be our buzz word lately, whats the benefit of using static int methods vs. int methods.
15 Mar, 2014, Pymeus wrote in the 2nd comment:
Votes: 0
Since the static keyword means different things in different contexts, could you provide some more context? What language are you using and is this method a class member function?
15 Mar, 2014, Davenge wrote in the 3rd comment:
Votes: 0
C
15 Mar, 2014, Sharmair wrote in the 4th comment:
Votes: 0
I am going to assume C++ as that is the forum and by your use of methods and the title return codes assume you are
talking about static member functions of a class.

First, it is not really the return value (or type) that is static, but the function itself. Static member functions differ from
normal member functions in not having a this pointer. This makes it so you can not access non static member functions
and data members in the class directly. As a static member function is not tied to an object like a non static function you
can also call is like:
ClassName::StaticFunction();

In fact, in the case where you call it like a normal function:
Object.StaticFunction();
ObjPointer->StaticFunction();

The left side Object or ObjPointer is not evaluated (like if it is a function that returns an object or object pointer).

As you can only use them with the restrictions I mentioned I suppose that might be considered more optimized when you
can use them. The use cases tend to be things that relate to the class as a whole and not specific objects (like creating
new objects of the class, getting counts of the number of objects of the class etc).
15 Mar, 2014, Davenge wrote in the 5th comment:
Votes: 0
Sharmair said:
I am going to assume C++ as that is the forum and by your use of methods and the title return codes assume you are
talking about static member functions of a class.


C, but the lesson on the C++ side of things is nice, too.
15 Mar, 2014, Sharmair wrote in the 6th comment:
Votes: 0
I see after I posted that you say C, and I guess though method is normally used referring to class functions, I suppose
you could be misusing the term to mean just a function.

A function or data declared as static at global scope is visible only from that file. It is normally used for things used
only within the one C file so you don't clash with names that are used in the rest of the C files. Also, as the symbols
are internal to the C file, they do not make it into the obj (o) files and link does not even know about them (or have
to search them for resolving references).
15 Mar, 2014, Davenge wrote in the 7th comment:
Votes: 0
I see.
15 Mar, 2014, plamzi wrote in the 8th comment:
Votes: 0
You can also have static variables inside functions, and they'll be visible only inside that function. Unlike other function vars, they will be initialized only once, and they'll persist on each call. An example use would be initializing a seed for a random number generator.
16 Mar, 2014, quixadhal wrote in the 9th comment:
Votes: 0
In C, a static function is only visible inside the module it was declared in, meaning the compiler won't let you call it from elsewhere. In that context, think of it as "private". For variables, in C, there isn't any real concept of private/public (if you have the pointer, you can use it regardless), but as planzi said, static causes the variable to retain its value after the function exits.

Back in the old days, there were three types of data storge. Normal "auto" variables were allocated on the program stack, and went away when the function exited, as they were popped off as part of that return process. Dynamically allocated memory was taken from the "heap" storage. On most systems, memory was allocated in chunks (pages) of two types. The program code itself was put in code segments while the dynamically allocated memory was put in data segments. A static variable was allocated space in the code segment, by the compiler, right alongside the actual code itself.

That's also why buffer overruns in static variables can cause strange behavior and crashes more easily than dynamically allocated buffer overruns… in either case, running over the edge of a segment causes a segmentation fault, but in the case of a code segment, it can actually overwrite machine code, which can have unpredictable results if a function is called from the now corrupted space.
16 Mar, 2014, Davenge wrote in the 10th comment:
Votes: 0
quixadhal said:
In C, a static function is only visible inside the module it was declared in, meaning the compiler won't let you call it from elsewhere. In that context, think of it as "private". For variables, in C, there isn't any real concept of private/public (if you have the pointer, you can use it regardless), but as planzi said, static causes the variable to retain its value after the function exits.

Back in the old days, there were three types of data storge. Normal "auto" variables were allocated on the program stack, and went away when the function exited, as they were popped off as part of that return process. Dynamically allocated memory was taken from the "heap" storage. On most systems, memory was allocated in chunks (pages) of two types. The program code itself was put in code segments while the dynamically allocated memory was put in data segments. A static variable was allocated space in the code segment, by the compiler, right alongside the actual code itself.

That's also why buffer overruns in static variables can cause strange behavior and crashes more easily than dynamically allocated buffer overruns… in either case, running over the edge of a segment causes a segmentation fault, but in the case of a code segment, it can actually overwrite machine code, which can have unpredictable results if a function is called from the now corrupted space.


Good information here. I was aware of static variable and was thinking that a static method was similar in regards to the memory used for the return value. I did not realize it was an encapsulation of that method to the .c file.
17 Mar, 2014, quixadhal wrote in the 11th comment:
Votes: 0
The only caveat to remember here is that it will only prevent the compiler from seeing direct attempts to call that function. If you manage to obtain a pointer to it, you can still call it through the function poitner (IE: a dispatch table).
0.0/11