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


72

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


56

It may be a good idea to avoid pointers, using object copies and references whenever possible, like you do. Continue this practice. However there are a certain number of things that can go awfully wrong, if you're not extremely careful: Objects "on the stack" (the C++ terminology nowadays for this storage class is "auto") must stay valid when you use ...


49

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


47

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

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


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


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


33

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


24

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

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


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


19

You are using "indirect reference in the general sense" in your C++ programming. There's nothing wrong with that. It is almost as powerful as programming with pointers and reference (in the C++ language). Although, there is one technicality pitfall in the way you use vector. For software safety sake, I'll describe that technical pitfall first. It's more ...


18

Pointer arithmetic is in C++ because it was in C. Pointer arithmetic is in C because it's a normal idiom in assembler. There are plenty of systems where "increment register" is faster than "load constant value 1 and add to register". Moreover, quite a few systems let you do "load DWORD into A from address specified in register B, then add sizeof(DWORD) to ...


17

A null pointer is a tool not an enemy. You just ought to use 'em right. Think just minute about how long it takes to find and fix a typical invalid pointer bug, compared to locating and fixing a null pointer bug. It's easy to check a pointer against null. It is a PITA to verify whether or not a non-null pointer points to valid data. If still not ...


17

At face value, since the official Java language specification specifically mentions pointers, then the answer is YES, pointers exist in Java. So how do we explain the apparent contradictory opinions here? Well, like many issues, the disagreement comes down to semantics. The term "pointer" is strongly associated with the C/C++ concept of pointers, which ...


16

You are right that code such as float a = malloc(size); a--; yields undefined behavior, per the ANSI C standard, section 3.3.6: Unless both the pointer operand and the result point to a member of the same array object, or one past the last member of the array object, the behavior is undefined For code like this, the quality of the C code in the book (...


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