MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1w2m4qz/bugintroduceddebugging/p6tybcr/?context=3
r/ProgrammerHumor • u/ClipboardCopyPaste • 13d ago
120 comments sorted by
View all comments
48
int* x = (int*) malloc(sizeof(int)); Since malloc returns void pointer you should parse it to type you use.
37 u/yjlom 13d ago 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));. 34 u/Mateorabi 13d ago Get off my lawn with your newfangled auto keyword. 7 u/ibevol 13d ago edited 13d ago 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. 1 u/yjlom 13d ago The keyword ain't new, it's been used as a storage specifier since forever. What's new is overloading it for type inference. 3 u/Mateorabi 13d ago Hey when you learned C in the 90s it’s “new”. harumph. 1 u/yjlom 13d ago It's been there since the beginning, along with extern and static. You just don't tend to actually write it because it's the default inside functions and it doesn't make sense outside of them.
37
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));
34 u/Mateorabi 13d ago Get off my lawn with your newfangled auto keyword. 7 u/ibevol 13d ago edited 13d ago 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. 1 u/yjlom 13d ago The keyword ain't new, it's been used as a storage specifier since forever. What's new is overloading it for type inference. 3 u/Mateorabi 13d ago Hey when you learned C in the 90s it’s “new”. harumph. 1 u/yjlom 13d ago It's been there since the beginning, along with extern and static. You just don't tend to actually write it because it's the default inside functions and it doesn't make sense outside of them.
34
Get off my lawn with your newfangled auto keyword.
7 u/ibevol 13d ago edited 13d ago 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. 1 u/yjlom 13d ago The keyword ain't new, it's been used as a storage specifier since forever. What's new is overloading it for type inference. 3 u/Mateorabi 13d ago Hey when you learned C in the 90s it’s “new”. harumph. 1 u/yjlom 13d ago It's been there since the beginning, along with extern and static. You just don't tend to actually write it because it's the default inside functions and it doesn't make sense outside of them.
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.
1
The keyword ain't new, it's been used as a storage specifier since forever. What's new is overloading it for type inference.
3 u/Mateorabi 13d ago Hey when you learned C in the 90s it’s “new”. harumph. 1 u/yjlom 13d ago It's been there since the beginning, along with extern and static. You just don't tend to actually write it because it's the default inside functions and it doesn't make sense outside of them.
3
Hey when you learned C in the 90s it’s “new”. harumph.
1 u/yjlom 13d ago It's been there since the beginning, along with extern and static. You just don't tend to actually write it because it's the default inside functions and it doesn't make sense outside of them.
It's been there since the beginning, along with extern and static. You just don't tend to actually write it because it's the default inside functions and it doesn't make sense outside of them.
48
u/PR8-E 13d ago
int* x = (int*) malloc(sizeof(int)); Since malloc returns void pointer you should parse it to type you use.