C
Memory, pointers, compilation and portable C programs.
45 troubleshooting guides · Page 1 / 2
Why does sizeof an array parameter return a pointer size?
An array-looking function parameter is adjusted to a pointer type. The callee cannot recover the caller’s element count by dividing sizeof that parameter by sizeof one element.
Why does allocating sizeof(pointer) corrupt a larger C structure?
The pointer and the object it points at have different sizes. `malloc(sizeof p)` allocates room for p itself, not necessarily a complete structure.
How do I resize a C buffer without losing it on allocation failure?
Assigning realloc directly to the sole owning pointer can lose the original block when a nonzero resize fails. The failure path then leaks storage and destroys recoverability.
Why does an interior pointer break after growing its owner buffer?
A pointer into an allocation is tied to that allocation’s lifetime and address. A successful realloc can move storage, leaving cached element pointers invalid.
Why does fgets read an apparently empty line after scanf?
Numeric conversion can leave the line separator in the input stream. The following line read then correctly consumes that separator instead of the next intended response.
How can a fixed-buffer fgets reader detect a line split across reads?
A successful fgets call need not contain a whole line when the buffer fills. Treating each returned chunk as a separate record can silently split a long configuration entry.
Why does atoi accept a malformed port such as 8080x?
A conversion without an end-position check cannot enforce that the entire input is a valid number. A usable prefix can hide an invalid suffix.
Why does a scanf retry loop keep rejecting the same character?
When a conversion fails, the offending input may remain unread. Repeating the identical conversion then encounters the same byte indefinitely.
Why is a strncpy destination not always null-terminated?
A bounded copy count is not automatically a C-string contract. If the source occupies at least that count, strncpy can leave no terminating null in the destination.
How do I distinguish snprintf success from a truncated result?
The return value describes required formatted length, not simply bytes stored. Ignoring it can turn a truncated identifier into an apparently valid output.
Why does memcpy corrupt a buffer when removing a prefix?
Source and destination ranges overlap when shifting content within the same buffer. That operation violates memcpy’s non-overlap contract even if one platform happens to copy in a convenient direction.
Why can returning a-b from a qsort comparator misorder integers?
Subtracting two arbitrary signed integers can overflow, and the comparator then fails to represent a consistent ordering. Extreme values expose errors that ordinary positive samples conceal.
Why can isalpha fail for bytes above ASCII on a signed-char system?
Character classification expects EOF or a value representable as unsigned char. Passing a negative promoted char from an arbitrary byte buffer violates that input contract.
Why must getchar be stored in int rather than char?
The result must represent both byte values and the separate EOF sentinel. Narrowing it to char before comparison can lose that distinction and cause premature stopping or an endless read loop.
Why does fread return one when my buffer contains many bytes?
fread reports completed elements of the requested element size, not necessarily bytes. Calling it with one large element gives a count of zero or one, which is easy to misinterpret.
Why does while(!feof(file)) process the last record twice?
The EOF indicator is set after a read encounters the end, not as a prediction before the next read. Checking it first can execute the body with stale data after a failed read.
How do I prevent count-times-size overflow before malloc?
An allocation can be too small even when malloc succeeds if the byte-count calculation wrapped before the call. Subsequent element writes then exceed the allocation.
Why does newly malloced memory sometimes contain old-looking values?
Freshly allocated storage is not an initialized application object. Reading fields before assigning meaningful values can expose indeterminate data rather than a default zero.
Why does returning a pointer to a local array appear to work once?
The local array’s lifetime ends when its function returns. Its former bytes may remain temporarily, but a pointer to them no longer provides a valid result.
Why does freeing two aliases crash even after setting one pointer to null?
Two pointer variables can refer to one allocation. Nulling one variable does not clear the other alias, and a second free still violates the single-release ownership contract.
Why does a signed overflow check disappear in an optimized build?
A check performed after signed overflow has already relied on undefined behavior. The optimizer may reason that valid signed arithmetic did not overflow and transform the surrounding condition.
Why is -1 greater than a positive size_t in a comparison?
Usual arithmetic conversions can turn a signed negative operand into an unsigned value. The comparison then no longer expresses the intended numeric ordering.
Why does shifting by the integer width not produce zero?
A shift count outside the permitted range is not a portable way to discard all bits. Negative counts and counts at least the promoted operand width are invalid.
Why is f(i++, i++) unsuitable for advancing two C indexes?
The two modifications are not a portable sequencing contract in C. A particular compiler’s observed argument order cannot be relied on to determine the resulting values.
Ask your own question · Sign in to post questions and answers.