142

References might be implemented by storing the address. Usually Java references will be implemented as pointers, but that's not required by the specification. They may be using an additional layer of indirection to enable easier garbage collection. But in the end it will (almost always) boil down to (C-style) pointers being involved in the implementation of (...


97

Since I’m the one who published the polemic “don’t use f*cking pointers” I feel that I should comment here. First of all, as a polemic it obviously represents an extreme viewpoint. There are definitely legitimate uses of (raw) pointers. But I (and many professional C++ programmers) maintain that these cases are exceedingly rare. But what we really mean is ...


91

There are 28192 possible different 1K blocks. Storing them all would take 28202 bits of storage. Since the universe contains only about 1080 (or ~2266) particles, it's a safe bet that it isn't possible to store them all, and you don't have to wonder about whether it would save time or not. But there is, in fact a more interesting way of answering this. You ...


81

It didn't take 12 bytes, it only took 8. However, the default alignment for an 8 byte long int on this platform is 8 bytes. As such, the compiler needed to move the long int to an address that's divisible by 8. The "obvious" address, da54dc8c, isn't divisible by 8 hence the 12 byte gap. You should be able to test this. If you add another int prior to the ...


70

A "raw" pointer is unmanaged. That is, the following line: SomeKindOfObject *someKindOfObject = new SomeKindOfObject(); ... will leak memory if an accompanying delete is not executed at the proper time. auto_ptr In order to minimize these cases, std::auto_ptr<> was introduced. Due to the limitations of C++ prior to the 2011 standard, however, it's ...


64

The type of a pointer variable is .. pointer. The operations you're formally allowed to do in C are to compare it (to other pointers, or the special NULL / zero value), to add or subtract integers, or to cast it to other pointers. Once you accept undefined behaviour, you can look at what the value actually is. It will usually be a machine word, the same ...


48

Why? Because, although consistent terminology is generally good for the entire profession, language designers don't always respect the language use of other language designers, particularly if those other languages are perceived as competitors. But really, neither use of 'reference' was a very good choice. "References" in C++ are simply a language construct ...


46

When you have an array, you can set up a pointer to point to an element of the array: int a[10]; int *p = &a[0]; Here p points to the first element of a, which is a[0]. Now you can increment the pointer to point to the next element: p++; Now p points to the second element, a[1]. You can access the element here using *p. This is different from Java ...


44

You're overcomplicating things. Addresses are just integers, period. Ideally they're the number of the referenced memory cell (in practice this gets more complicated because of segments, virtual memory etc.). Hexadecimal syntax is a complete fiction that exists only for the convenience of programmers. 0x1A and 26 are exactly the same number of exactly ...


38

In my opinion, it doesn't make any sense to use C and want to avoid pointers. If you do so, then you'd better use another language. Pointers are unavoidable in C. This is what make C so powerfull and also what make C a pain in the ass sometime. C is meant to be used with pointers. Arrays are pointers, functions are pointers, memory allocation work through ...


37

Incrementing pointers is idiomatic C++, because pointer semantics reflect a fundamental aspect of the design philosophy behind the C++ standard library (based off of Alexander Stepanov's STL) The important concept here, is that the STL is designed around containers, algorithms, and iterators. Pointers are simply iterators. Of course, the ability to ...


36

First let's consider why it is useful to have strings that are immutable. Consider the following sketch: void Safe(string s) { if (!SecurityCheck(s)) { throw new SecurityException(); } Dangerous(s); } Method Safe checks to see whether string s is "safe". Perhaps by "safe" we mean "the user is permitted to access this database table", or "the ...


35

So isn't this a memory overhead? Sure, an extra address (generally 4/8 bytes depending on processor). How is this compensated? It is not. If you need the indirection necessary for pointers, then you get to pay for it. Are pointers used in time critical low memory applications? I haven't done much work there, but I would assume so. Pointer access is ...


34

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 ...


32

void and void* are two different things. void in C means exactly the same thing as it does in Java, an absence of a return value. A void* is a pointer with an absence of a type. All pointers in C need to be able to be dereferenced. If you dereferenced a void*, what type would you expect to get? Remember C pointers don't carry any runtime type ...


30

this is (like nullptr) a constant pointer; the pointed data is const if and only if this appears in the body of a const member function. You cannot change a constant pointer, like you cannot change a constant literal like 23. So assignment to this like this = p; // WRONG is prohibited for the same reasons assignment to nullptr is forbidden: nullptr = ...


25

Like so many things, the answer is of the form "Because X and Y are different things with different purposes". In this case, the designers of both languages assumed that the users of their languages had a very different set of goals. For C, the primary use case was "portable assembly language", which really means getting down into the nitty-gritty of how ...


23

Other languages don't "drop pointers entirely," they just restrict what you can do with them, give them a syntax that looks like non-pointer variables, handle some operations on them behind the scenes, and call them something else, like a reference or object. If you ever have to make a distinction between assigning a copy of something or not, you are ...


23

Here's some actual disassembled code from a virtual function call and a non-virtual call, respectively: mov -0x8(%rbp),%rax mov (%rax),%rax mov (%rax),%rax callq *%rax callq 0x4007aa You can see that the virtual call requires three additional instructions to look up the correct address, whereas the address of the non-virtual call can be ...


22

No, absolutely not. If that were what invariably happens, we could use that to our advantage and specify it in the standard; known behaviour, even if it is a crash, is virtually always better than unknown behaviour. But instead, the system response depends on details of the implementation, of the previous actions of the program, on the state of the runtime ...


21

No I wouldn't describe that as a dangling pointer, but it is a memory leak because you do not free the first malloc before the second malloc. A dangling pointer would typically be a pointer which points to something which e.g. has already been freed float f,*pf; pf = malloc(sizeof(float)); *pf = 2.6; free(pf); f = *pf; /*uhoh pf no longer points to valid ...


21

The rationale was the development of alternatives to pointers. Under the hood, any pointer/reference/etc is being implemented as an integer containing a memory address (aka pointer). When C came out, this functionality was exposed as pointers. This meant that anything the underlying hardware could do to address memory could be done with pointers. This ...


20

There's plenty of excellent answers that cover the unfortunate symptoms of null, so I'd like to present an alternative argument: Null is a flaw in the type system. The purpose of a type system is to ensure that the different components of a program "fit together" properly; a well-typed program can't "off the rails" into undefined behavior. Consider a ...


20

As others have already pointed out, you have 2^8192 possibilities for a 1k block. This means you would need 8192 bits to encode the address of a block if all blocks addresses are encoded with the same amount of bits, so your addresses would be 1k long. You wouldn't have gained anything except adding a layer of indirection so you wouldn't gain any performance....


19

In general, programming constructs and techniques are commonly considered to be 'bad' when there are 'better' alternatives available for a particular task. The use of a pointer may be technically correct in a lot of places, but it's rare in C++ for a situation to arise where the use of a raw pointer doesn't have a better alternative. Most of the time, using ...


19

Perhaps it would be more useful to think of void as the return type. Then your second method would read "a method that returns an untyped pointer."


18

Slower than what? Virtual functions solve a problem that cannot be solved by direct function calls. In general, you can only compare two programs which compute the same thing. "This ray tracer is faster than that compiler" doesn't make sense, and this principle generalizes even to small things like individual functions or programming language constructs. ...


Only top voted, non community-wiki answers of a minimum length are eligible