Difference between statc and dynamic

hai,
generally static means consumes memory and dynamic didn’t take memory more than that what is difference.

thanks in advance

In reply to nana:

The difference is not really how much memory, but when the memory gets allocated and initialized. There are actually three kinds of storage categories; static, automatic, and dynamic.

Static and automatic storage are part of the declaration of a variable of any type. Static variables get allocated and initialized before time 0 and are never deallocated. Automatic variables get allocated and initialized when entering a procedural scope like a task or function. They get deallocated when exiting the scope.

Dynamic storage is associated with the variable’s type. You can procedurally change the size of an array, queue, or string, as well as construct a class object as any time. The variable that references the array or class can still be declared static or automatic.

In reply to dave_59:
sry for resending.
thank you dave_59,
could you please tell me time 0 and before time 0. how can we get to know difference
if we mentioned a variable as a static then also we can deallocate that variable in dynamic storage?

In reply to nana:

“Before time 0” is informal terminology for the state of the system before any processes created by initial or always blocks have started.

You need to separate the concept of a variable from the contents that it refers to. Dynamic storage is a characteristic of content’s type, not the variable itself. I can declare two types of arrays

typedef int FA_t[5]; // Fixed-sized array
typedef int QA_t[$]; // Dynamically-sized queue

and then use those types to declare static or automatic variables.

module top;
   typedef int FA_t[5]; // Fixed-sized array
   typedef int QA_t[$]; // Dynamically-sized queue
   initial repeat(5) begin :loop
      static    FA_t st_FA = {0,1,2,3,4};
      static    QA_t st_QA = {0,1,2,3,4};
      automatic FA_t au_FA = {0,1,2,3,4};
      automatic QA_t au_QA = {0,1,2,3,4};

      st_FA[0]++;
      au_FA[0]++;
      void' ( st_QA.pop_back() );
      void' ( au_QA.pop_back() );
      $display("st_FA %p au_FA %p", st_FA, au_FA);
      $display("st_QA %p au_QA %p", st_QA, au_QA);
   end : loop
   initial #1 $display($size(loop.st_FA),, $size(loop.st_QA));
endmodule : top

This displays

# st_FA '{1, 1, 2, 3, 4} au_FA '{1, 1, 2, 3, 4}
# st_QA '{0, 1, 2, 3} au_QA '{0, 1, 2, 3}
# st_FA '{2, 1, 2, 3, 4} au_FA '{1, 1, 2, 3, 4}
# st_QA '{0, 1, 2} au_QA '{0, 1, 2, 3}
# st_FA '{3, 1, 2, 3, 4} au_FA '{1, 1, 2, 3, 4}
# st_QA '{0, 1} au_QA '{0, 1, 2, 3}
# st_FA '{4, 1, 2, 3, 4} au_FA '{1, 1, 2, 3, 4}
# st_QA '{0} au_QA '{0, 1, 2, 3}
# st_FA '{5, 1, 2, 3, 4} au_FA '{1, 1, 2, 3, 4}
# st_QA '{} au_QA '{0, 1, 2, 3}
#           5           0

Note that the static, fixed-sized array element st_FA[0] increments 1,2,3…, but the automatic au_FA[0] stays at 1 because the array variable gets re-allocated and re-initialized each time the begin:loop executes.
Each time through the same loop, one element of the static, dynamically sized array st_QA deallocates. After 5 iterations, there or no elements remaining in the queue, leaving it empty. The variable st_QA still exists. Since static variables permanently exist, they can be references from outside their declared scope, as shown in the second initial block. You cannot do that with automatic variables. In this example the automatic variables au_FA and au_QA no longer exist after the first initial block finishes.

SystemVerilog has arrays of arrays which means you can declare and fixed-sized array of queues, or a queue of fixed-sized arrays. There are other dynamic types such as associative arrays and classes that all can be layered in a declaration. Once you reach a dynamic type element, all of the storage underneath that layer becomes dynamically allocated.

In reply to dave_59:

thank you dave.