Returning string from C function

Either allocate the string on the stack on the caller side and pass it to your function: void getStr(char *wordd, int length) { … } int main(void) { char wordd[10 + 1]; getStr(wordd, sizeof(wordd) – 1); … } Or make the string static in getStr: char *getStr(void) { static char wordd[10 + 1]; … return … Read more

What’s the scope of a variable initialized in an if statement?

Python variables are scoped to the innermost function, class, or module in which they’re assigned. Control blocks like if and while blocks don’t count, so a variable assigned inside an if is still scoped to a function, class, or module. (Implicit functions defined by a generator expression or list/set/dict comprehension do count, as do lambda … Read more

In ArrayBlockingQueue, why copy final member field into local final variable?

It’s an extreme optimization Doug Lea, the author of the class, likes to use. Here’s a post on a recent thread on the core-libs-dev mailing list about this exact subject which answers your question pretty well. from the post: …copying to locals produces the smallest bytecode, and for low-level code it’s nice to write code … Read more