Does realloc overwrite old contents?

Don’t worry about the old contents. The correct way to use realloc is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer int *oldpointer = malloc(100); /* … */ int *newpointer = realloc(oldpointer, 1000); if (newpointer == NULL) { /* problems!!!! */ /* … Read more

How do realloc and memcpy work?

Let’s take a little closer look at memcpy and, while we’re at it, at “big O” or Landau notation. First, big-O. As i’ve talked about elsewhere, it’s worth remembering the definition of big O, which is that some function g(n) is said to be O(f(n)) when there exists a constant k for which g(n) ≤ … Read more

Why is there no reallocation functionality in C++ allocators?

From: http://www.sgi.com/tech/stl/alloc.html This is probably the most questionable design decision. It would have probably been a bit more useful to provide a version of reallocate that either changed the size of the existing object without copying or returned NULL. This would have made it directly useful for objects with copy constructors. It would also have … Read more