contract sample {
     uint32 count;

     struct example {
         uint32 value ;
     mapping(uint32=example) mapper;    
     event trackInt(uint32);
     event trackObj(uint32);

     function test() {
         example memory  obj;
         obj.value = count +1;


     function getCount() returns (uint32) {
         return count;        

     function get(uint32 id) returns (uint32) {

         example obj = mapper[id-1];
         return obj.value;

Even after calling the test() function several times, I am able to retrieve the details in the struct even though obj is of type memory . Could someone clarify how it works?

2 Answers 2


Variables declared with memory keyword is only accessible during contract execution. Once execution is finished, it's contents are discarded.

In your program, you are creating a temporary struct variable using memory keyword named obj. This variable named obj exists during the execution of the contract and then get destroyed.

You are able to retrieve values because your variables count and mapping mapper both are stored as persistent i.e storage type variable and you are getting that data.

To know more about different type of variable storage, check this.


Even after calling test() function several times ...

As far as I understand,

every time you call the test() function you create a example struct type data object called obj temporarily in the memory and its then stored in the storage with the mapping mapper.

So every time you call the test() there is a obj of type example.

the details in the struct

A struct is a user defined new data type. What do you try to refer by the details in the struct? The struct you defined is not a storage/memory variable or constant value to refer so. It's just you are defining a data type. So whenever you refer as obj, its referred to the temporary example struct type object that is created in the memory with the respective function call.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.