in Programming in C
1,335 views
1 vote
1 vote
What is dynamic variable.? Is there any benefit of using dynamic variable instead of static variable?
in Programming in C
by
1.3k views

1 Answer

7 votes
7 votes
Best answer

A dynamic variable is a variable whose address is determined when the program is run. In contrast, a static variable has memory reserved for it at compilation time.

Dynamic variables provide flexibility. I mean that in dynamic variables, we do not have to mention the data type of variables at compile time, it can be decided by compiler on the basis of input values.

For example: Python, JavaScript etc have facility of dynamic variables. 

selected by
by

13 Comments

Is n,t C language  provide any facility  to create dynamic variable?
0
0
See, We can create variable at runtime by using malloc, calloc but they are not really dynamic variable. Although malloc and calloc declare memory location at runtime, but type of value is already fix.
0
0
type of value is already fix ? what does it mean?
0
0
In Python and JS, compiler decides the type of variable on the basis of value you are putting in that. But In C programmer tells typecast the address, returned by malloc.
0
0
int * a = malloc(... )

Here we have a as a variable. And memory for a ( a is a pointer to int and takes the size of a pointer) is given at compile time. The memory a point to is created at runtime- but that memory is not directly named by any variable.
2
2
what is meaning of size of pointer?

I mean does a pointer contain any size? it only addressed  to a memory location.
0
0

I think its architecture defined i.e. type of Os and complier you are using .Suppose you using 32 bit Os then  the size of pointer is 4 bytes.

0
0
@srestha "it contains address" - and address needs size rt? That's it- size of a pointer is size of the address and that is why it is architecture dependent. On a virtual memory system, this address is virtual address- and you should know why..
2
2
@Arjun sir

Because operating system needs unique address space

and it looks at it as contiguous block of memory.

Actually fragmented memory are accessed by pointer to fill it is the large virtual memory

therefore max size of virtual memory , the maximum size pointer can can access

Means size of pointer sould be large.

is that correct or anything else?
0
0
Simple answer - because OS or programmer knows nothing about physical address. It is handled by MMU. Whatever address is being used by the programmer is the same being used by CPU (when CPU executes corresponding instruction) and as given in text books, this is virtual address.

When a program runs, a chunk of Virtual memory is given to it. And you are right- size of a pointer is the no, of bits needed for addressing virtual memory.

I did not get what is meant by operating system needing unique address?
1
1
Every program while running in OS ,needs unique address space in virtual memory and it should be continuous.

Not always physically continuous

If program needs more memory that is more than RAM holds

by the use of virtual address space , part of hard disk mapped in real memory

OS maintains page table for look after which virtual address is mapped to which physical address. right?

 

Another question

@Arjun Sir @Manoj

What is the relationship between heap and offset
0
0
Heap is a memory area where allocation and de-allocation is done manually.

Whereas the offset specify entire address space .So i think heap is a portion of address space(i.e. offset).
1
1
@Manoj actually all page offset and page number stored in heap but when page is deleted then heap has to delete the pointer of page offset to page. rt?

@Arjun sir

physical address can be calculated by programmer adding logical address with base address . rt?

yes it is just a location where the pointer only access it.

But it cannot be seen by it user for its huge size .Now right?
0
0