New answers tagged


your logic seem good except handling of thread safety , what would happen if two thread try to push / pop data from stack , there will be data disparity . please put synchronized keyword in push and pop method


I really don't recommend trying to write anything that is more complicated than necessary during an interview, especially if it's on a whiteboard. Interviews are stressful enough for you; introducing more opportunities for errors is not a good idea. As for the interviewer, their likely thoughts are that your code lacks elegance and is hard to verify. In ...


The usage example is unrealistic, as it fails to show the error handling that's necessary when initialising the stack. Wrapping the initialisation in command-like macros makes it harder to write the correct checks (as we can't just use a return value, we have to inspect the macro to see where the result went): STACK_INIT(8); if (!stack_global) { /* not ...


if((ret = malloc(sizeof(*ret)))) { ret->beg = malloc(size); ret->end = ret->cur = ret->beg + size; } You don't check the second malloc's returned value. When out of memory, you're likely to return an object in invalid state (or maybe not, since ret->end and ret->cur initialization is an UB anyway).


Overall the code is well-written, consistent and easy to follow. Program design / big picture Having one .c file per function is quite extreme - this just creates a lot of fuss when linking and maintaining the code. This isn't a whole lot of code, so it is hard to justify splitting it in so many different .c files when it could have been placed in a single ...

Top 50 recent answers are included