Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • Thank you so very much! Commented Aug 19, 2020 at 13:19
  • Could you please show me how to realloc in a safer manner? Commented Aug 19, 2020 at 13:19
  • @WillyCboi: To realloc “safely,” you can use MyType *NewPointer = realloc(OldPointer, NewSize); if (!NewPointer) { Do error handling here, such as writing an error message to stderr and terminating the program. } OldPointer = NewPointer;. Although, if your error handling consists only of reporting an error and terminating the program, it does not matter if you use data = realloc(data, dataIndex + 1); in a typical system, as the operating system will reclaim all memory when your program terminates. (It is actually wasteful to “clean up” things that are purely internal to the program.) Commented Aug 19, 2020 at 13:21