Visual VC++ Strings, and embedded C strings

Today I have tried the map Template Class for the first time. This functionality is part of the STL/CLR.

It is still possible to use old style

char*
syntax, however this is now considered as “unsafe”. C language was designed on mainframe computers with small memories! Today’s computer memories are different by dimensions, GBytes, not KBytes. As well the operating system will maintain many tasks “threads” at once.

For this reason the C (or C++ if we don’t consider classes today) was extended with new features. Even if for some particular tasks, it would be really handy to use pointer arithmetic, for instance Windows forms use the new Reference type, so anyway a conversion is required.

A Stringbuilder is allocated on the garbage collected heap! After some years exposure to VC++ (I put some efforts into it in 2001 to learn Windows Message loop), the error messages are not really totally cryptic, they contain useful information.

No standard conversion exists from the boxed form of the arithmetic type to the target type

In english this means you just have to know the right types and “forms” for the parameters.

The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters. For comparison, see the String class.

This means the String Class is not mutable, so a “Stringbuilder” must be used. For purposes such as passing a literal constant, “String” is sufficient.


However purpose of this codepiece was to evalute the MAP template class.

You use the container map to manage a sequence of elements as a (nearly) balanced ordered tree of nodes

This is actually quite useful, or would you want to program a library for a balanced, ordered tree of nodes manually? Years ago I sometimes wrote code for primitive chained list, and this was always quite difficult to get it right in terms of allocation/deallocation, and leaks. I would not really want to spent time about it, if the functionality already exists.

Indeed I am interested to figure out how much data I can add to such a map, and if any impact will occur on program execution performance. For instance I play with the idea to use it for various video game data. Typically this relates to some 1000s of associations, but this might easily increase by one or two dimensions. What about a map with 100k String/numeric data associations? If you consider maybe 1K numeric data, this results in 100 MByte raw data already.

I don’t know if there is a margin and if this template class can be used for such purposes.

For the time being also smaller maps are useful with some 100 entries.

I did not use VC++ for a while, actually some years ago I tried out VB, so my actual programming experience with VC++ is about 10 years ago!

private: System::Void button1_Click
(System::Object^ sender, System::EventArgs^ e) {
StringBuilder^ sb = gcnew StringBuilder;
String^ s1;
Mymap::iterator it;
s1=”AND.W”;
c1.insert(Mymap::make_value(s1, 0x8001));
s1=”EORI.L”;
c1.insert(Mymap::make_value(s1, 0x9a12));
s1=”LINK”;
c1.insert(Mymap::make_value(s1, 0x340a));
sb->Insert(0,c1.size());
label1- > Text=sb- > ToString();
while(sb- > Length!=0)sb->Remove(0,1);
s1=”AND.W”;
it = c1.find(s1);
sb- > Insert(0,it->first);
sb- > Insert(0,” — “);
sb- > Insert(0,it->second);
label2- > Text=sb- > ToString();
delete sb;
}

In the meanwhile I have also learned embedded C. String pointers can be quite obscure on small 8-bit chips, even if it is just old plain C including usage of casting from 2 8bit chunks into a 16bit pointer. I am not exactly sure if you can still do this easily nowadays in VC++.

Anyway, here the code piece.

It took me days to figure out how to do it, since it is not documentated properly. On these small chips, string data is stored inside program FLASH memory! If you have a pointer table pointing into individual string tables, the dereferencing does not work for some specific chip models!

Sounds complicated and yes I wrote maybe 10 pages about it including some even more weird code.

curr_chr_size2_ptr=&alpha_chr[chr_curr];
curr_chr_size2=*curr_chr_size2_ptr;
lvl2= ((const char*)
(
(*curr_chr_size2_ptr)
|
(
((unsigned int)(*(curr_chr_size2_ptr+1))) << 8)
)
);
curr_chr_size2=*((const char*)(lvl2));

Once I got this spelling working, I changed it into this:

return;
l_addr_decode:
addr=*(t1+1);addr << =8;
addr|=*(t1);t1=(((const char*)(addr)));
if(goto_dst==0)goto l_dest0;else goto l_dest1;
}

On Windows I strongly hope you would not attempt to save about 25 bytes program memory space by employing such a goto construct.

But hopefully the way the pointer is constructed is clear (addr is a 16bit int). t is a const pointer (pointing into non-mutable program FLASH or better say, it is dereferenced that way).

If we now maintain a table with pointer to FLASH ROM strings, the dispatching table contains two 8-bit entries. I spent many hours actually to examine how the compiler builds the relevant code.

The good thing: On recent chip models the compiler actually can dereference such pointer tables automatically! It is really only required for some very old chip models. Which on the other hand are attractive for their simplicity. After some days I really got it working!

Here the complete source: http://pic.hitechworld.org/data/ledmatrixtag/


By the way I am aware there might be other ways to archive a solution. For microcontrollers, simply use a better chip! And eventually in more recent Visual Studio some additional technology exists which is simpler to use.

Sometimes how something works is not really relevant, because priority is on developing completely different functionality. Here particulary it was the map template class! The strings are just used for debugging (Modifying Windows Forms Text Labels).

The unrelated problem should become solved as quickly as possible.

Using VS2008 the Intellisense somehow stopped working I tried steps mentioned in the MSDN documentation but guess nothing worked. So maybe I am mixing features from different ages of VC++ “evolution”. Maybe there is a more elegant way than to use Windows Forms Text Labels and actually manipulate their values all the times.

From the old-style C point of view, it is just straightforward. You always think in terms of char, and unmanaged memory, and pointer arithmetic. So, manipulate the text label value (which indeed works).

The source code is black on white, because the built-in wordpress editor is very bad for HTML, so I installed a plugin for source code highlighting.

In the end I even had to modify the PHP for the source code highlighting because it does not seem to be designed for C language. I am not really bright with PHP but the < and > are displaying now.

 

Today’s bonus: A japanese hexbug! Actually I don’t have one here, not that I really needed or wanted one, but if I would be a typical freak or hacker, or just a generation younger, it would be important or interesting etc. Just to mention such hex bugs really exist.

 

Leave a Reply

Your email address will not be published. Required fields are marked *