realloc fails segfault

Started by
12 comments, last by SiCrane 15 years, 4 months ago
Quote:Original post by swiftcoder
Check the man page for printf to find the format specifier for short - I don't recall it offhand.

That probably has something to do with the fact that there isn't one. At least not in standard C. Your compiler may support non-standard format specifier extensions.
Quote: A better solution would be to make s an int anyway, or even better a size_t (which is the type expected by malloc/realloc in most implementations) and use the format specifier for size_t (again, check the man page).

Again, no such thing. For scanf() integer format specifiers you've got pointer to int and pointer to unsigned int.
Advertisement
Quote:Original post by SiCrane
Quote:Original post by swiftcoder
Check the man page for printf to find the format specifier for short - I don't recall it offhand.

That probably has something to do with the fact that there isn't one. At least not in standard C. Your compiler may support non-standard format specifier extensions.


Here it says that you need to add an h to the d. Are you saying that's not standard behavior?
Quote:Original post by SiCrane
Quote:Original post by swiftcoder
Check the man page for printf to find the format specifier for short - I don't recall it offhand.

That probably has something to do with the fact that there isn't one. At least not in standard C. Your compiler may support non-standard format specifier extensions.
Quote: A better solution would be to make s an int anyway, or even better a size_t (which is the type expected by malloc/realloc in most implementations) and use the format specifier for size_t (again, check the man page).

Again, no such thing. For scanf() integer format specifiers you've got pointer to int and pointer to unsigned int.
Fair enough, I tend to take the man page as gospel, and don't have a copy of the standard handy. However, I haven't had a problem *yet* (across Windows/linux/Mac) using "%hd" for signed short, and "%zu" for size_t.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Actually, it looks like it is standard in C99. mea culpa.

This topic is closed to new replies.

Advertisement