Brighter side
Brighter side

Reputation: 402

EXCEPTION_ACCESS_VIOLATION on a static declaration

I am new with the usage of for ***'Address use ***. And I was wondering what are the limitation of this usage. So I created the following procedure:

procedure letshack (A : System.Address) is 
      My_String : String(1..100000);
      for My_String'Address use A;
   begin 
      Put(My_String);
   end;

And this raise a EXCEPTION_ACCESS_VIOLATION while the same code with a String that is 100 length don't raise it. More over if i don't use the integer address, this code works properly.

So what are the limitation of for ***'Address use *** usage. Ps : I am using Ada 95 but any information is welcome.

Edit: I understand a part of the behavior. And this is what I suppose. When you start your program a certain stack is allocated and you can write and read in it. Indeed I Wrote the 5th byte with an integer address

Real Addresses |----------------------------| Virtual Addresses
        0x48000|Stack Origine               |0x00
               |                            |
               |                            |
               |                            |
               |                            |
               |End of Stack                |
  0x48000+range|----------------------------|0x00+range

And you get EXCEPTION_ACCESS_VIOLATION if you are out of stack. It seems strange for a "strong" language if it is right. Because it means you can rewrite your own stack and make bad behave.

Upvotes: 1

Views: 409

Answers (2)

Brighter side
Brighter side

Reputation: 402

Finnaly found the behavior. When you start your program the addresses you use are virtual ones in a page. And the part of the system that handle virtual adress make it for a certain size of memory that is allocated to your process which is constant depending on your system as show the following schema:

Real Addresses |----------------------------| Virtual Addresses
        0x48000|Begin of the virtual address|0x00
               |range                       |
               |                            |
               |                            |
               |End of the virtual address  |
               |range                       |
  0x48000+range|----------------------------|0x00+range

You can do anything without allocating variable in it. For example on my windows this size is 4096 bytes according to the variable si.dwPageSize in <windows.h>. And I tested my String can be 4096 bytes long but not 4097 bytes. I must now test it on my embedded system but seems close to the truth.

Upvotes: 1

Jacob Sparre Andersen
Jacob Sparre Andersen

Reputation: 6601

If you have ensured that you have allocated 100_000 consecutive characters in a readable part of memory starting at A, then it should work.

If A is the address of another Ada entity, it is not supposed to work.

Upvotes: 0

Related Questions