News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

You Will Hate This Question

Started by Zen, July 28, 2012, 03:51:06 AM

Previous topic - Next topic

Zen

If you are a C++ programmer,...you are probably familiar with templates. If you are not a C or C++ programmer,...templates are essentially a language feature that gives you the ability to create huge structured memory allocations for any type (from integral types to complex data structures) that can be defined. These allocations can then be accessed by a simple code statement.
Here is some background:
Standard Template Library (Wikipedia)
Standard Template Library Programmer's Guide (Silicon Graphics)
Microsoft Standard Template Library for Visual Studio

The problems with templates are that the syntax can be confusing to implement correctly,...and, that they are almost impossible to debug.
If you've ever used templates to implement standard COM interfaces,...you know what I'm talking about. Windows Template Library
The advantage to templates is that the memory allocation happens internally, and is typically pretty reliable.

But,...I hate them,...they're funky and weird.
What they really need are diagnostics built into the library.

...So,...what's the question ???
...Oh,...yeah,...there must be a better way to accomplish a reliable memory allocation that can be accessed reliably,...
...And,...this could be excellently done in assembly language,...
Thoughts???

jj2007

Quote from: Zen on July 28, 2012, 03:51:06 AM
...So,...what's the question ???
...Oh,...yeah,...there must be a better way to accomplish a reliable memory allocation that can be accessed reliably,...

.data?
wc WNDCLASSEX <>

zooba

It sounds like you've misunderstood the purpose of C++ templates.

Templates allow you to write code with unspecified types. This means that the code cannot be compiled until the types have been fully specified, as well as allowing completely separate pieces of code to be generated for different types.

For example:

template<typename T> T addOne(T original) {
    return original + 1;
}


can be called as:

int x = addOne(5);
float y = addOne(0.5f);
MyClass z = addOne(MyClass());


which will generate three separate implementations for the one function:

; addOne<int>
mov eax, original
inc eax

; addOne<float>
fld REAL4 PTR original
fld1
fadd

; addOne<MyClass>
push 1
push original
call MyClass::operator+


In terms of diagnostics, both Microsoft's C++ compiler and Clang provide full instantiation stacks for template errors - gcc is quite poor in this area. And using COM is painful regardless of whether or not you have templates. VB is about the only good language for it.

I suspect what you are referring to, rather than templates, is the C++ Standard Template Library and in particular the collection classes that it provides. std::vector is quite a simple dynamically resizing array, which simply stores the number of elements in the array and reallocates the memory as necessary. std::list is nothing more than a linked list. Both of these can be implemented in assembly with some effort, but I'd be incredibly surprised if you can't find implementations on this forum (and the archived old forum) or in libraries.

As for being able to do it "excellently" in assembly language, this is only partially true. The advantage that C++ with templates has is that all the type information about the stored data is known, such as how to copy it in memory, deallocate it and how much space it requires. Doing this without such rich type information is complicated, since you have to supply and store it separately. Of course, you can write faster algorithms in assembly language especially if you restrict the data types (ref: every string-related thread in The Laboratory), though the effort is much higher and it is harder to change the algorithm for different types. These are the tradeoffs that have to be made with higher/lower level languages

Cheers,
Zooba  :t

Zen

ZOOBA,
Thanks for the reply.

JOCHEN,
I'm still decoding your post.
...Clearly this is one of those cryptic phrases that spies use to identify each other,...

jj2007

No mysteries here: You asked for "a reliable memory allocation that can be accessed reliably" - and I assure you uninitialised variables fit that description 100% :biggrin:

hutch--

Zen,

From reading your original post, what is wrong with defining your own structure and then using the final length of that structure to either allocate a single block of memory OR allocate an array of memory blocks each being the size of the structure plus any padding to get a decent alignment for each structure start ?

It means you have to write your own structure but thats no big deal to do in MASM.

Zen

...Yeah,...you guys are right,...I should have been clearer with my original question,...
What I was referring to was HUGE data structures. For example, in a program I wrote several years ago,...I has two or three data structures that together were over a MB,...just initializing them to zero was an operation. They worked perfectly, though,...(this was a C++ program,...but, the essentials were the same as what you would do in assembly language).   
The reason that I mentioned the Standard Template Library was that it is used alot in C++ programs to store large amounts of sequential data in memory.   
For instance, if you were writing a MIDI program (a music composing program),...you would have a component that reads MIDI format files,...which are mainly a collection of 32-bit MIDI messages, that are then stored in memory and sent as a stream to your software synthesizer. The typical MIDI format file has thousands of of these messages,...the vast majority are note-on or note-off messages, which have the channel, time, and pitch encoded into a DWORD-like structure. The typical MIDI format file reader will assign these messages to a huge linked-list-type of templated data storage container, which can then be accessed in a fairly straight-forward method to stream the MIDI note data to the software synthesizer, which converts the messages to WAVE format sound and actually plays the song.   
...But, these template structures can be a monster to manipulate (especially to re-order),...and, you have no sense of what is really happening, since it is important that the song be played with the shortest latency as possible.