Aarray

module test();

int mem[byte];

initial
begin
for (int i=0; i < 258; i++)
begin
  mem[i] = i;
  $display("index -> %d content -> %d",i,mem[i]);
end
  if(mem.exists(0))
    $display(" val of 0th location -> %d",mem[0]);
  if(mem.exists(256))
    $display("val of 256th location -> %d",mem[256]);
  else
    $display("no entry");
    
    $display("total memory address -> %d",mem.size());
end
endmodule

question is if declaration of mem is :-

int mem [byte] ;

which means width of mem is 32 bit and indexing is byte type so maximum location is 256 . so if indexing start from 0 than last index is 255 than why this snippet executing both “IF CONDITION” ?

In reply to mrudang pujari1:

The byte data type is signed, so its value ranges from -128 to +127. Change your for-loop to

for (int i = -128; i < 128; i++)

and it will make more sense. Note that when you do something like

if(mem.exists(256))

the 256 value gets implicitly cast to byte type, which will give it a value of 0.

In reply to mrudang pujari1:

This is a big source of problems with SystemVerilog inherited from Verilog. All integral expressions are silently cast to the target type. The index expression (256) gets truncated to (0), and (257) to (1).