Live data from Hacker News

Your code should be taken out back, lined up against a wall, and machine-gunned

cygwin.com

1–10 of 76 posts

Re: Your code should be taken out back, lined up against a wall, and machine-gunned

#4
post #3

Why was he complaining about the stack pointer first? Isn't this line: st1 = (void *)malloc(5000) + 5000; completely invalid too? You can't (I think it's undefined) increment a void pointer.

It may be undefined, but gcc accepts it without warning under -Wall and does exactly what you'd expect. Note that (a) casting malloc's return is evil, (b) casting malloc to void* is silly since it's declared returning void* already, and (c) casting malloc to void* so it can be received as a char* is also pretty goofy.

Re: Your code should be taken out back, lined up against a wall, and machine-gunned

#5
post #3

Why was he complaining about the stack pointer first? Isn't this line: st1 = (void *)malloc(5000) + 5000; completely invalid too? You can't (I think it's undefined) increment a void pointer.

Yep, it's basically undefined because a void type has no known size to do pointer arithmetic with.

Re: Your code should be taken out back, lined up against a wall, and machine-gunned

#7

Then the bleeding corpse should be hung, drawn and quartered. Then burnt. Then the smouldering rubble should be jumped up and down on. By a hippo. By a hippo....

It is a natural choice. 3,600 lb is more than the weight of any human in the world, and elephants can't jump.

Re: Your code should be taken out back, lined up against a wall, and machine-gunned

#9
post #4
post #3

Why was he complaining about the stack pointer first? Isn't this line: st1 = (void *)malloc(5000) + 5000; completely invalid too? You can't (I think it's undefined) increment a void pointer.

It may be undefined, but gcc accepts it without warning under -Wall and does exactly what you'd expect. Note that (a) casting malloc's return is evil, (b) casting malloc to void* is silly since it's declared returning void* already, and (c) casting malloc to void* so it can be received as a char* is also pretty goofy.

This might be a stupid question, but what are you meant to do if not cast malloc's return?

My C is a bit rusty, but how else are you meant to allocate data of different types on the heap?

Re: Your code should be taken out back, lined up against a wall, and machine-gunned

#10
post #5
post #3

Why was he complaining about the stack pointer first? Isn't this line: st1 = (void *)malloc(5000) + 5000; completely invalid too? You can't (I think it's undefined) increment a void pointer.

Yep, it's basically undefined because a void type has no known size to do pointer arithmetic with.

GCC whines and moans, but treats it like a pointer to char.
Post reply on HN