9

There is no such thing like a "heap variable". You have invented a new term, so expecting others to explain you the threading behaviour of this new thing does not make much sense to me. There is, of course, "heap memory", and malloc returns a block of such memory. The start the adress of that memory can be stored in a pointer variable, ...


8

The only real rule with variable names is that it should always be obvious what they refer to. Reusing a variable name can cause issues in some situations if the reassignment fundamentally changes what the variable is representing, but in your case that isn't happening. Compare what you wrote to what this might look if instead of applying a string to a ...


7

Initially I cannot understand your question, because the phrase "heap variable" is not described with sufficient detail and clarity, such that its meaning is a bit ambiguous. Each call to malloc gets assigned a different address. That address assignment happens during the call to malloc, not before, not after. When a thread function is executed on ...


1

Who cares about memory usage for a single variable of type int? Nobody. But there is one huge difference if your code is a bit more complicated: If there are situations where the code inside the loop misses initialising temp then it will without any warning use the value of the previous iteration. If this happens only rarely then you have a very hard to find ...


1

What about heap variables created by malloc() inside a thread function executed by multiple threads? Do all the threads share one instance of the heap variable, or have different instances of the heap variable? In the sense in which I think you're asking - no. malloc is a function call. Each thread is independent. When two independent threads call malloc, ...


1

A variable has: an identifier, like the x in int x;, some memory* where the value is stored, and a scope, the rules for which parts of the program can "see" the variable. Globals and static locals are stored at a location predetermined by the compiler in the data segment of the program. Automatic variables are stored on the stack. And then there'...


1

What about heap variables created by malloc() inside a thread function executed by multiple threads? Do all the threads share one instance of the heap variable, or have different instances of the heap variable? All thread share all instances. It's like my wife and I each having a car. We each have our own car, but each of those two cars is jointly owned by ...


Only top voted, non community-wiki answers of a minimum length are eligible