In languages like C and C++, while using pointers to variables we need one more memory location to store that address. So isn't this a memory overhead? How is this compensated? Are pointers used in time critical low memory applications?
C++ Memory Management – Is the Use of Pointer Variables a Memory Overhead?
cpointers
Related Solutions
Back in those days, developers were working much closer to the metal. C was essentially a higher level replacement for assembly, which is almost as close to the hardware as you can get, so it was natural you needed pointers to be efficient in solving coding problems. However, pointers are sharp tools, which can cause great damage if used carelessly. Also, direct use of pointers open up the possibility to many security problems, which weren't an issue back then (in 1970, the internet consisted of about a few dozen machines across a couple of universities, and it was not even called like that...), but became more and more important since. So nowadays higher level languages are consciously designed to avoid raw memory pointers.
Saying that "advanced things done in VB.Net or Java are not possible in C" shows a very limited point of view, to say the least :-)
First of all, all of these languages (even assembly) are Turing complete so in theory whatever is possible in one language, is possible in all. Just think about what happens when a piece of VB.Net or Java code is compiled and executed: eventually, it is translated into (or mapped to) machine code, because that is the only thing which the machine understands. In compiled languages like C and C++, you can actually get the full body of machine code equivalent to the original higher level source code, as one or more executable files/libraries. In VM based languages, it is more tricky (and may not even be possible) to get the entire equivalent machine code representation of your program, but still eventually it is there somewhere, within the deep recesses of the runtime system and the JIT.
Now, of course, it is an entirely different question whether some solution is feasible in a specific language. No sensible developer would start writing a web app in assembly :-) But it is useful to bear in mind that most or all of those higher level languages are built on top of a huge amount of runtime and class library code, a large chunk of which is implemented in a lower level language, typically in C.
So to get to the question,
Do you think knowledge on pointers to the young people [...] is important?
The concept behind pointers is indirection. This is a very important concept and IMHO every good programmer should grasp it on a certain level. Even if someone is working solely with higher level languages, indirection and references are still important. Failing to understand this means being unable to use a whole class of very potent tools, seriously limiting one's problem solving ability in the long run.
So my answer is yes, if you want to become a truly good programmer, you must understand pointers too (as well as recursion - this is the other typical stumbling block for budding developers). You may not need to start with it - I don't think C is optimal as a first language nowadays. But at some point one should get familiar with indirection. Without it, we can never understand how the tools, libraries and frameworks we are using actually work. And a craftsman who doesn't understand how his/her tools work is a very limited one. Fair enough, one may get a grasp of it in higher level programming languages too. One good litmus test is correctly implementing a doubly linked list - if you can do it in your favourite language, you can claim you understand indirection well enough.
But if not for anything else, we should do it to learn respect for the programmers of old who managed to build unbelievable things using the ridiculously simple tools they had (compared to what we have now). We are all standing on the shoulders of giants, and it does good to us to acknowledge this, rather than pretending we are the giants ourselves.
what is the best syntax and coding style for generating a pointer to a method?
Don't. Use std::function<void()>
.
Now, on to the steps. Step 1 is technically illegal, but your compiler allowed it by implicitly taking the address for you. Step 2 is how it should be done. The name of a static function (in this case, Foo::dummy
) is a function lvalue and must have it's address taken to be legal C++ and yield a function pointer.
Step 3 simply doesn't make any sense at all. You're trying to assign ... a run-time function pointer ... to a type? That makes no sense whatsoever. And then you try to access ... a member ... of a function pointer? Does not compute.
There are two parts to the answer "What do I do when I want to X in C++?". The first part, for C veterans only, is "Forget whatever you think you learned in C". The second part is about doing X.
how can i write an array of pointers or a pointer to pointers structure to store methods ( static or non-static from real objects )
std::vector<std::function<void()>> arr;
le done.
Best Answer
Actually, the overhead does not really lie in the extra 4 or 8 bytes needed to store the pointer. Most times pointers are used for dynamic memory allocation, meaning that we invoke a function to allocate a block of memory, and this function returns to us a pointer which points to that block of memory. This new block in and of itself represents a considerable overhead.
Now, you don't have to engage in memory allocation in order to use a pointer: You can have an array of
int
declared statically or on the stack, and you can use a pointer instead of an index to visit theint
s, and it is all very nice and simple and efficient. No memory allocation needed, and the pointer will usually occupy exactly as much space in memory as an integer index would.Also, as Joshua Taylor reminds us in a comment, pointers are used to pass something by reference. E.g.,
struct foo f; init_foo(&f);
would allocate f on the stack and then callinit_foo()
with a pointer to thatstruct
. That's very common. (Just be careful not to pass those pointers "upward".) In C++ you might see this being done with a "reference" (foo&
) instead of a pointer, but references are nothing but pointers that you may not alter, and they occupy the same amount of memory.But the main reason why pointers are used is for dynamic memory allocation, and this is done in order to solve problems that could not be solved otherwise. Here is a simplistic example: Imagine you want to read the entire contents of a file. Where are you going to store them? If you try with a fixed-size buffer, then you will only be able to read files that are not longer than that buffer. But by using memory allocation, you can allocate as much memory as necessary to read the file, and then proceed to read it.
Also, C++ is an object-oriented language, and there are certain aspects of OOP like abstraction that are only achievable using pointers. Even languages like Java and C# make extensive use of pointers, they just don't allow you to directly manipulate the pointers, so as to prevent you from doing dangerous stuff with them, but still, these languages only begin to make sense once you have realized that behind the scenes everything is done using pointers.
So, pointers are not only used in time-critical, low-memory applications, they are used everywhere.