Is there any predefined macro to check if variable is writable in run-time?

pborut
Posts: 3
Joined: Thu Mar 07, 2019 11:32 am

Is there any predefined macro to check if variable is writable in run-time?

Postby pborut » Thu Mar 07, 2019 11:55 am

Hello,
I'm quite new to esp32. Esp-idf proved so far to be very good library, also documentation is very good.
I'm porting some existing code to esp32 DK and got to exceptions where I try to write to variables that are declared as 'const'. While that for some reason worked on other platform, it of course raises exception in esp32.
I'd like to have portable code, so I'd like to check this in run-time.

I can of course write my own checks, but my question is, is there any predefined macro or other method available already available to find out in run-time, if particular item is writable?

Thank you and best regards,
Borut.

ESP_Sprite
Posts: 9766
Joined: Thu Nov 26, 2015 4:08 am

Re: Is there any predefined macro to check if variable is writable in run-time?

Postby ESP_Sprite » Fri Mar 08, 2019 8:54 am

Normally, as long as you don't do stupid things like force-casting const variables to non-const ones, your compiler should shout warnings at you if you try to do this.

pborut
Posts: 3
Joined: Thu Mar 07, 2019 11:32 am

Re: Is there any predefined macro to check if variable is writable in run-time?

Postby pborut » Sat Mar 09, 2019 4:51 pm

Thanks ESP_Sprite.
Yes, checking such things in runtime is usually not necessary, except in such cases like some monitor program integrated in code and used in runtime.
In my case I've solved problem by finishing my python code generator that generates relational database table images in constants for my C runtime. I used to compute foreign key record pointers in runtime, that led to those errors. Now I've moved that computation offline to my code generator python and compiler and runtime are again happy.

Nevertheless, question about existing macros still stands opened. Do you perhaps know about such?

Best regards,
Borut.

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Is there any predefined macro to check if variable is writable in run-time?

Postby ESP_igrr » Sun Mar 10, 2019 1:36 am

You can use esp_ptr_in_dram to check whether particular pointer points to a location in RAM. Const variables will be placed into memory mapped flash region (DROM) and this function will return false for pointers to them.

https://github.com/espressif/esp-idf/bl ... y_layout.h

pborut
Posts: 3
Joined: Thu Mar 07, 2019 11:32 am

Re: Is there any predefined macro to check if variable is writable in run-time?

Postby pborut » Sun Mar 10, 2019 10:00 am

Thank you ESP_igrr! :)
This is answer I was looking for.
Best regards,
Borut.

Who is online

Users browsing this forum: No registered users and 138 guests