i want to know the differnce between int a=10 &
int a;
a=10;
though both look same and assign the value 10 to the variable a.
but in memory theres a big difference between te two.
Can anyone help me with this??

AFAIK int a = 10; will allow the compiler to directly construct a with the value of 10 where as when you split it up the code still has to create a and then you have an additional assignment to assign 10 to it.

There is no difference in behavior between the two. Both declare a variable "a" and give it the value 10.

In a compiler without signifigant optimizations, int a; a = 10; might take extra intructions. Even in optimized compilers, if you have int a; <code block>; a = 10; it might not pick up on the fact that a was not modified until you assign it a value depending on the code block and the compiler. It is usually safer (and considered to look better) to use int a = 10; instead.

Edit: Please ignore me.

There is no "memory difference" between those two things. The only difference is semantically, as in, the first creates a variable with an initial value, the other creates a variable and then assigns it a value. The first is preferred, but this is purely a matter of semantics (i.e., how you read and understand the code).

A standard compliant compiler must produce the same code in both cases. This is not even a matter of optimization, really, it's a matter of standard compliance. There are two operations involved here: allocating the space on the stack and setting the memory to the value of 10. When you have a statement like int a = 10;, then the compiler is required to make room for the variable on the stack and initialize its memory to the value 10. When you have a statement like int a;, then the compiler is required to make room for the variable on the stack and do absolutely nothing else. And then, with a=10;, the compiler is required to set the memory of a to the value 10. So, if you have int a; a = 10; (nothing in between), what is required of the compiler is exactly the same as what is required if you had int a = 10;.

If there are other things that appear between the declaration and the initialization, then in most cases, the compiler is allowed to (and frankly, expected to) make the code identical in both cases too (unless you actually read the uninitialized value of a, in which case, it's undefined behavior, so, the compiler is allowed to put in code that will draw a ham sandwich on the screen, or whatever else it wants to do). This is because the compiler is allowed to re-order operations as much as it likes, as long as the observable effect is the same (the "as-if" rule).

Also, the memory that is allocated on the stack is usually pre-allocated in block when entering the function, and therefore, we are really only talking about one operation here (the initialization of the variable) because in both cases the stack allocation will be done as the function body (or narrowest scope) is entered, i.e., before the int a; or int a = 10; statements even appear in the function.

N.B.: The only thing that can really control when a variable is initialized or written to is the use of memory fences, which is a bit of an advanced topic.

I agree to Mike. I don't think there would not be any difference in the memory allocation unless you go for dynamic memory allocation.

A standard compliant compiler must produce the same code in both cases. This is not even a matter of optimization, really, it's a matter of standard compliance.

That's not true. The standard makes absolutely no guarantees about what the generated instructions look like - it only makes guarantees about the behavior of the code. So as long as two pieces of code act the same, it does not matter (as far as standard compliance is concerned) whether they're doing so using the exact same instructions.

There are two operations involved here: allocating the space on the stack and setting the memory to the value of 10.

Yes, but (depending on the architecture of course) there may be one instruction that can perform both operations at once: push. It's theoretically conceivable that a simple compiler could translate int a = 10; to pushl $10 and int a; a = 10; to subl $4, %esp, followed by movl $10, (%esp) (completely ignoring calling conventions, %ebp etc. and that any sane compiler is going to increment the stack pointer all at once when the function is called - not once per variable).

Since those two pieces of code are completely equivalent as far as their observable behavior is concerned, there is nothing in the standard that forbids a compiler from doing this.

Or for a (somewhat) more realistic example: Let's say the compiler initializes all uninitialized variables to something like 0xDEADBEEF for debugging purposes. Now let's say that same compiler is not smart enough to see that the same variable is assigned to in the very next statement. Then that compiler would generate code that first stores 0xDEADBEEF at that memory and then overwrites it with 10, whereas the instructions for int a = 10; would store 10 directly. Again, as there's no difference in observable behavior, this would be perfectly allowed by the standard.

There is actually no difference between two of them. The only diffrence that is found in the way of assigning the value of 10 to the variable 'a'.

yes, eventhough that both is same internaly it has difference. In the fisrt case that means int a=10 there creating a space for a with the value 10 but in the second case is not that first it create a variable and then it assign the value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.