patterncMinor
Critique of realloc() wrapper
Viewed 0 times
reallocwrappercritique
Problem
Seeking code correctness and best practices.
I would like to re-allocate various amounts of memory (0 or more) such that on systems that may return
Given:
Receive:
I came across the below references, which imply a
C11dr 7.22.3.5 The realloc function 4 The
pointer to the new object (which may have the same value as a pointer
to the old object), or a null pointer if the new object could not be
allocated.
C11dr §J.1 Unspecified behavior. — The amount of storage allocated by
a successful call to the calloc, malloc, or realloc function when 0
bytes was requested (7.22.3).
C11dr §J.3.12 Implementation-defined behavior. Whether the calloc,
malloc, and realloc functions return a null pointer or a pointer to an
allocated object when the size requested is zero (7.22.3).
-
Discusses how a memory allocation of 0 is useful.
Answer: when a routine has a
what's the point in malloc(0)?
-
Asks if
Answer: Not necessarily.
What happens if I re-alloc and the new size is 0. Is this equivalent with a free?
-
Allocation wrapper set.
Answer: Does not address
Are these memory-allocation wrapper functions kosher with all C compilers?
[Answer]
-
Use
-
With a 0 size, avo
I would like to re-allocate various amounts of memory (0 or more) such that on systems that may return
NULL for an allocation of 0 does not falsely imply a memory allocation error.Given:
void *(with a valid value or NULL already)
size_t(new desired size)
Receive:
void *(new pointer)
int(failure status)
#include
#include
int ReallocAndTest(char **Buf, size_t NewSize) {
assert(Buf);
char *NewBuf = realloc(*Buf, NewSize);
if ((NewBuf == NULL) && (NewSize > 0)) {
return 1; // return failure
}
*Buf = NewBuf;
return 0;
}I came across the below references, which imply a
realloc() return of NULL is not always a failure. The lone special case occurs with size is 0. The return value may or may not be NULL.C11dr 7.22.3.5 The realloc function 4 The
realloc function returns apointer to the new object (which may have the same value as a pointer
to the old object), or a null pointer if the new object could not be
allocated.
C11dr §J.1 Unspecified behavior. — The amount of storage allocated by
a successful call to the calloc, malloc, or realloc function when 0
bytes was requested (7.22.3).
C11dr §J.3.12 Implementation-defined behavior. Whether the calloc,
malloc, and realloc functions return a null pointer or a pointer to an
allocated object when the size requested is zero (7.22.3).
-
Discusses how a memory allocation of 0 is useful.
Answer: when a routine has a
malloc(size), testing for size != 0 is not needed.what's the point in malloc(0)?
-
Asks if
realloc(ptr), 0) is the same as free(ptr).Answer: Not necessarily.
What happens if I re-alloc and the new size is 0. Is this equivalent with a free?
-
Allocation wrapper set.
Answer: Does not address
realloc(ptr, 0).Are these memory-allocation wrapper functions kosher with all C compilers?
[Answer]
-
Use
void rather than char .-
With a 0 size, avo
Solution
-
Standard naming convention in C is usually
-
You should be using
-
I would consider making the return value deterministic when the new size is 0 by always returning
Standard naming convention in C is usually
lower_case_with_underscore.-
You should be using
void rather than char -
I would consider making the return value deterministic when the new size is 0 by always returning
NULL rather than the implementation defined behaviour (which could technically be a pointer which is not NULL but still not allowed to be dereferenced). free guarantees by spec that free(NULL) is legal.Context
StackExchange Code Review Q#36662, answer score: 6
Revisions (0)
No revisions yet.