Bug found in GCC-AVR/ AVRStudio

On Mon, 17 Jan 2005 23:48:38 -0500, R Adsett wrote in comp.arch.embedded:

There's a very simple reason for this. String literals were part of the C language a good 15 years before the 1989 standard added the 'const' keyword, so that was 15 years worth of existing code that would have been broken.

In fact, even in C++ where string literals do have the type array of const char, they break the type system and allow the address of a string literal to be assigned to a pointer to mutable char, just because there is so much existing code.

Someday somebody will invent the perfect computer programming language. It might be you. For sure, I've given up on the idea that it will be me!

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Reply to
Jack Klein
Loading thread data ...

On Tue, 18 Jan 2005 11:12:06 +0100, David wrote in comp.arch.embedded:

Not unless there was a standard for C other than the original de facto one, K&R 1 published in 1978. Even that text stated that the result of attempting to modify a string literal was undefined.

Yes, this code is perfectly legal, and it causes no problem with implementations that put code into EPROM/flash/write-protected memory. You can always assign a pointer to a more qualified (with const and/or volatile) object with the address of a less qualified object of the same type. And if the compiler put 'i' in read-only memory it would be seriously non-conforming. Because:

*((int *) p) = 100;

...is legal code and does what you expect. You can cast away "const-ness" as long as the object was not actually defined const.

Adding the const qualifier to the type pointed to by a pointer does not mean it can actually only point to const objects. It merely says that you will not modify the object through the pointer.

If you cast away the const and attempt to modify the object, it is well defined if the object is not const, undefined if it is.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
Reply to
Jack Klein

I am pretty sure ImageCraft's AVR compiler does what Jack is saying. Basically, we do "reasonable" without going thru extreme heroics.

Make that EXTREMELY inefficient :-)

--
// richard
http://www.imagecraft.com
Reply to
Richard M.

Nah, I'd probably do something silly like re-invent APL.

Robert

Reply to
R Adsett

Have you tried it? I'm curious because I've used 8051 compilers that have generic pointers, and the code produced is not unreasonable. And there have been cases where the capability would have been welcome.

I may try it someday, though I'm sure an implementor could do a better job. And an implementor could use them for functions like memcpy and puts.

enum memory_space {RAM, FLASH, EEPROM}; typedef enum memory_space Memory_Space; typedef uint16_t Address struct generic_ptr { Memory_Space space; Address adr; }; typedef struct generic_ptr Generic_Ptr;

extern Generic_Ptr GP_make(Memory_Space s, Address adr); extern void GP_write(Generic_Ptr p, void *src, size_t len); extern void GP_read(void *dest, Generic_Ptr p, size_t len); extern void GP_copy(Generic_Ptr d, Generic_Ptr s, size_t len);

Of course, this all assumes that pointers that did not need to be generic would not be made generic. Like the 8051 compilers.

Regards, -=Dave

--
Change is inevitable, progress is not.
Reply to
Dave Hansen

Yep - the number 1 write-only language.

--

Tauno Voipio
tauno voipio (at) iki fi
Reply to
Tauno Voipio

I finally store these data into flash using functions in progmem.h, rather than store them in sram.

Reply to
Leon

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.