Pages: 1 [2]   Go Down
Author Topic: How to specify the size of a class array ?  (Read 1722 times)
0 Members and 1 Guest are viewing this topic.
United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's true that using dynamic memory allocation is usually best avoided in embedded systems (see http://critical.eschertech.com/2010/07/30/dynamic-memory-allocation-in-critical-embedded-systems/ for why). However, allocating dynamic memory using malloc or new in the initialization phase (i.e. in stuff called from setup()) is OK if you never release it.

The template solution is also OK. One issue with using templates in embedded systems is that the code gets replicated each time the template is instantiated, leading to code bloat; but in this case you will only even instantiate the class once, so that doesn't apply.

Have you considered using a linked list instead of an array? That way you don't need to know in advance how many boards there will be. The disadvantage is that you need a little more RAM to store the links.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's true that using dynamic memory allocation is usually best avoided in embedded systems (see http://critical.eschertech.com/2010/07/30/dynamic-memory-allocation-in-critical-embedded-systems/ for why). However, allocating dynamic memory using malloc or new in the initialization phase (i.e. in stuff called from setup()) is OK if you never release it.

Thank you for that very interesting and informative article.

The template solution is also OK. One issue with using templates in embedded systems is that the code gets replicated each time the template is instantiated, leading to code bloat; but in this case you will only even instantiate the class once, so that doesn't apply.

You mean if I instantiate two classes with the same template parameters I have two copies of the same "generated" code ? That's interesting. Going to google a bit about that...

But yes, the idea is to have just one instance of that class per sketch.

Have you considered using a linked list instead of an array? That way you don't need to know in advance how many boards there will be. The disadvantage is that you need a little more RAM to store the links.

That's an interesting option. For the use case I'm thinking about I think a statically sized array is a better option though.
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html

Quote
3. Do nothing. Pretend G++ does implement automatic instantiation management. Code written for the Borland model works fine, but each translation unit contains instances of each of the templates it uses. In a large program, this can lead to an unacceptable amount of code duplication.

 smiley-eek
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You mean if I instantiate two classes with the same template parameters I have two copies of the same "generated" code ? That's interesting. Going to google a bit about that...

Nope, standard C++ requires types to be unique, therefore only one definition of code can ever be used to describe a type.

http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html

Quote
3. Do nothing. Pretend G++ does implement automatic instantiation management. Code written for the Borland model works fine, but each translation unit contains instances of each of the templates it uses. In a large program, this can lead to an unacceptable amount of code duplication.

 smiley-eek

Does not apply here, it is for systems that can have page faults, these models help prevent them. ( page faults can be detrimental to games and such ):

Quote
When used with GNU ld version 2.8 or later on an ELF system such as GNU/Linux or Solaris 2, or on Microsoft Windows, G++ supports the Borland model. On other systems, G++ implements neither automatic model.

Only ill formed code will have bloat, there is no reason why common code cannot be placed in a base class and inherited into a non-common template.

The instantiations I posted previously will not increase the size of the program unless you actually use the instantiation, they just allow the compiler to generate object files for the required template types:
Code:
template class Foo< bool >;
template class Foo< char >;
template class Foo< long >;

Using initial memory allocation is only of benefit if you require the memory to be un-initialised. Even then a global array/variable is still just as effective.

Quote
Out of curiosity, why a struct instead of a class, and why make everything public ? Just asking...
POD/Aggregate types cannot have private or protected members, it makes code easier to read, a one line struct is fairly readable, a class may not be.

C++ is your friend smiley.
« Last Edit: December 02, 2012, 07:45:09 pm by pYro_65 » Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I want the user to be able to specify a size. At compile time.

I don't see your objection to using malloc then (or new). If it is specified at compile time, you won't change it, therefore you wan't call free() therefore you won't have any problems.

The number of items can be in the constructor (or classname.begin). Then the class allocates exactly the amount of memory it needs, and hangs onto it. No problems.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope, standard C++ requires types to be unique, therefore only one definition of code can ever be used to describe a type.

A templated class instantiated twice with different template parameters gives rise to two distinct types.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope, standard C++ requires types to be unique, therefore only one definition of code can ever be used to describe a type.

A templated class instantiated twice with different template parameters gives rise to two distinct types.

Yes, that is correct. My response was in regard to multiple instantiations with the same parameters. i.e. the same type.
Logged


Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope, standard C++ requires types to be unique, therefore only one definition of code can ever be used to describe a type.

A templated class instantiated twice with different template parameters gives rise to two distinct types.

Yes, that is correct. My response was in regard to multiple instantiations with the same parameters. i.e. the same type.

My doubt is about code duplication when using a template class with the same parameters in two compilation units. E. g. I have to classes, each written with the usual header/cpp file separation. They both #include MPLArray.h and they both have a data member which is an implementation if the template class with the same parameters, say MPLArray<int, 10>.
The sketch #includes both classes header files, instantiates and uses one (global) object of each class.
Will the final executable contain only one instance of the MPLArrary<int, 10> code, or will there be code duplication ?
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 223
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My doubt is about code duplication when using a template class with the same parameters in two compilation units. E. g. I have to classes, each written with the usual header/cpp file separation. They both #include MPLArray.h and they both have a data member which is an implementation if the template class with the same parameters, say MPLArray<int, 10>.
The sketch #includes both classes header files, instantiates and uses one (global) object of each class.
Will the final executable contain only one instance of the MPLArrary<int, 10> code, or will there be code duplication ?

What should happen is that the compiler will instantiate the template once for each file, then the linker will spot that they are the same and only include one of them in the final code. To test this, if you change the second parameter for one of the instantiations, you should see the code size jump up.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Essex, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 150
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Will the final executable contain only one instance of the MPLArrary<int, 10> code, or will there be code duplication ?

There will only ever be one instance of the executable code if that's what you mean. Each compilation unit gets its own copy of the template methods in the object code but they are marked with the .weak attribute which causes the linker to discard duplicates.
Logged

Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Didn't know about that weak attribute. Goggled a bit about it, and found it in Arduino hardware serial implementation. Interesting... Thanks.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The compiler implicitly marking functions as weak is different to explicitly using a function modifier, may not be portable either.

The serial case usage is not used on a template, and is there to stop multiple definition errors and allow the user to implement the body of serialEvent in their own code.

The hardwareSerial.cpp file is compiled first so without having a weak definition that the linker can throw away, it would have an 'unresolved external. When your sketch is compiled with a new function body, the linker will select it and discard all the others.
Logged


Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks pYro, I wouldn't have been able to write it down as clearly as you did, but your description confirms what I understood about the "weak" mechanics by reading gcc page about it and man nm.
Logged

Pages: 1 [2]   Go Up
Jump to: