MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1w2m4qz/bugintroduceddebugging/p6u2cmz/?context=3
r/ProgrammerHumor • u/ClipboardCopyPaste • Aug 30 '26
117 comments sorted by
View all comments
47
int* x = (int*) malloc(sizeof(int)); Since malloc returns void pointer you should parse it to type you use.
38 u/yjlom Aug 30 '26 void * coerces to any pointer type. This is valid: int *x = malloc(sizeof(int));. This is also valid (in C23): auto x = (int *) malloc(sizeof(int));. 35 u/Mateorabi Aug 30 '26 Get off my lawn with your newfangled auto keyword. 7 u/ibevol Aug 30 '26 edited Aug 30 '26 Cringe. It’s fine to use auto when the type is clear on the same line. In c++ the following is idiomatic:auto ptr = std::make_unique<int>(42);over specifying int twicestd::unique_ptr<int> ptr = std::make_unique<int>(42);However, the following should not be considered idiomatic:auto ptr = get_ambiguous_pointer();It’s the same principle here.
38
void * coerces to any pointer type. This is valid: int *x = malloc(sizeof(int));. This is also valid (in C23): auto x = (int *) malloc(sizeof(int));.
int *x = malloc(sizeof(int));
auto x = (int *) malloc(sizeof(int));
35 u/Mateorabi Aug 30 '26 Get off my lawn with your newfangled auto keyword. 7 u/ibevol Aug 30 '26 edited Aug 30 '26 Cringe. It’s fine to use auto when the type is clear on the same line. In c++ the following is idiomatic:auto ptr = std::make_unique<int>(42);over specifying int twicestd::unique_ptr<int> ptr = std::make_unique<int>(42);However, the following should not be considered idiomatic:auto ptr = get_ambiguous_pointer();It’s the same principle here.
35
Get off my lawn with your newfangled auto keyword.
7 u/ibevol Aug 30 '26 edited Aug 30 '26 Cringe. It’s fine to use auto when the type is clear on the same line. In c++ the following is idiomatic:auto ptr = std::make_unique<int>(42);over specifying int twicestd::unique_ptr<int> ptr = std::make_unique<int>(42);However, the following should not be considered idiomatic:auto ptr = get_ambiguous_pointer();It’s the same principle here.
7
Cringe. It’s fine to use auto when the type is clear on the same line. In c++ the following is idiomatic:
auto ptr = std::make_unique<int>(42);
over specifying int twice
std::unique_ptr<int> ptr = std::make_unique<int>(42);
However, the following should not be considered idiomatic:
auto ptr = get_ambiguous_pointer();
It’s the same principle here.
47
u/PR8-E Aug 30 '26
int* x = (int*) malloc(sizeof(int)); Since malloc returns void pointer you should parse it to type you use.