How is memory reserved using a declaration statement?

Skill/Topic: Variables and Pointers
A) Memory is reserved by using a data type in a declaration statement.
Explanation: The form of a declaration statement varies depending on the programming language you use. For example, here is a declaration statement for C, C++, and Java:int myVariable;

Showing Answers 1 - 18 of 18 Answers

samiksc

  • Jan 19th, 2006
 

Memory is reserved using data type in the variable declaration. A programming language implementation has predefined sizes for its data types.

For example, in C# the declaration int i; will reserve 32 bits for variable i.

A pointer declaration reserves memory for the address or the pointer variable, but not for the data that it will point to. The memory for the data pointed by a pointer has to be allocated at runtime.

The memory reserved by the compiler for simple variables and for storing pointer address is allocated on the stack, while the memory allocated for pointer referenced data at runtime is allocated on the heap.

  Was this answer useful?  Yes

Anand Mishra

  • Apr 5th, 2006
 

Memory is reserved at the time of declararion of variable size is dependent on thetype of variable and type of Compiler

  Was this answer useful?  Yes

deepak jain

  • Apr 24th, 2006
 

at declaration, memory is allocated in case of declaration of variables and not in case ofpointers to some structures.fo that ,we have to allocate using 'new' keyword.

  Was this answer useful?  Yes

Pramod

  • Nov 8th, 2006
 

Memory is not allocated if declare a varialbe  merely.

ie,

int i; // will not allocate the memory

If we define the variable then only memory will be allocated based on the type of the Data

i = 10; // will allocate the memory

  Was this answer useful?  Yes

Pradeep Kumar

  • Nov 26th, 2006
 

Dear Pramod, memory is allocated at the time of declaration of the variable.in ur programint i;if you will print this by usingprintf("%d",i); //it will give some garbage value bcz value of i is not initialisedprintf("%u",&i); //this is the address of i in memory

  Was this answer useful?  Yes

ajitha

  • Jun 29th, 2007
 

First of all u have to understand the difference b/w declaration and definition.
When u define something, memory is allocated and when u declare something
memory is not allocated.
int i ;// i is a declaration until the value of i is specified.

  Was this answer useful?  Yes

In C if we write

int a;

this statement does both viz. declaration and definition. It first created a variable named a and then defines it be of type int and allocated memory. Although we have not specified any data to be stored in the memory allocated the default value is assigned to the varible or junk is there. 

But its no that always that a declaration statement reserves memory. If we extend the scope of a variable using extern we are just declaring that the scope is extended and there is no memory allocation or definintion involved. 

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions