Pages: [1]   Go Down
Author Topic: which features of C++ are supported  (Read 1035 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm just starting off with my first big project, which is to build a cardiac pacemaker library. I had a question about this statement in the library;

"The Arduino language is based on C/C++ and supports all standard C constructs and some C++ features"

I can see from the tutorial on writing a library that classes are part of the C++ features supported but is there a list of which other C++ features are supported/not-supported?

In particular function overloading? Or constructor overloading?

Also what about memory management?

Apologies if these are stupid questions. I'm more used to Java and am currently beginning learning C++ and Obj C. As such I can imagine how to do my program really nicely using OOP, but don't want to start down that road if I can't run it on the AVR. It also means that because Java is a higher level language this is my first foray into lower level stuff.

Any advice/links gratefully received.

p
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The most conspicuous unsupported features are "new" and "delete".  As far as I know function (and constructor) overloading are fully supported.

Regards,

-Mike
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I had a question about this statement in the library;

In which library? It would be useful to know where you saw that statement.

The major parts of the C++ set that are not supported are, as Mike said, the ability to dynamically create objects. Why this is so should be pretty obvious if you look at the amount of memory available on the Arduino and the consequences of running out of memory.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks Mike,

I had noticed that in the writing a library tutorial:

"Morse morse(13);"

doesn't use the word new. I was guessing that it is because the memory is allocated to the instance of a Morse object in advance when the chip is being bootloaded rather than on the fly when the program runs. Is that about right? That would also explain why no memory management is in any of the example programs.

@paul

sorry, rather than 'library' I should have said "extended language reference"; http://arduino.cc/en/Reference/Extended

Am I right to then think I can create objects in the program as long as I do not exceed the memory of the Arduino but they are not dynamically created, rather the compiler allocates them memory in advance of their being used in the program?

But what then if I use multiple constructors which create objects that require different amounts of memory and the choice of constructor depends on the input to the arduino? Is it not possible to do that? Maybe the memory is allocated to all the different class permutations and so I should avoid doing that because it wastes memory?

cheers p


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The new operator would return a pointer to dynamically allocated space in which the object (an instance of a class) had been created, and the constructor called.

The "Morse morse(13);" example creates an instance of the class, and calls the constructor for the class, but does so in statically allocated memory.

Quote
Am I right to then think I can create objects in the program as long as I do not exceed the memory of the Arduino but they are not dynamically created, rather the compiler allocates them memory in advance of their being used in the program?
Yes, you are right. The space is allocated at compile time, not at run time.

Quote
But what then if I use multiple constructors which create objects that require different amounts of memory and the choice of constructor depends on the input to the arduino? Is it not possible to do that?
All objects of a class are the same size. This is inherent in the definition of a class. The size of the instance does not depend on which constructor was called.

The objects may have pointers to dynamically allocated space that are different sizes, but all instances are the same size.

Again, there is that issue of dynamically allocating space, which needs to be handled with extreme care on the Arduino.

All of this is pretty theoretical without knowing more about the requirements for your library.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your help

Quote
All objects of a class are the same size. This is inherent in the definition of a class.
Ah, I was ignorant of that. Say I have declared 4 int in my header: a,b,c and d:


Code:
#include "Foo.h"

Foo::Foo(int z)
{
  a = z*z;
}

Foo::Foo(int, z, int y, int x, int w)
{
a = z*z;
b = y*y;
c = x*x;
d = w*w;
}

I was thinking that in the second constructor more memory was used as even though all 4 int are declared in the header the memory wouldn't be allocated until they were assigned a value. Clearly I've got something a little wrong there. Probably a lack of understanding of pointers. Or maybe the memory is allocated when the int is declared.

I shall make sure not to dynamically allocate memory.

It is a bit hypothetical at the moment as I don't quite know what I'm aiming for. I want to try to trigger arrhythmia in genetically altered mice but haven't got a lot of experience of mice. I am going to have 3 pairs of electrodes optically isolated to 6 digital output pins allowing bipolar stimulation. On top of a basic background pacing rate I will be adding extra stimuli to try cause arrhythmia. I will also have a blanking output so that my stimulations don't interfere with the electrocardiogram.

You can buy kit that does this all, but it costs thousands of pounds, so I thought I'd have a go myself. I'm just planning how I'm going to organise the program. I really need to flesh out a bit on triggering the arrhythmias in mice.


bw

p
Logged

Huntsville, Alabama, USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 327
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pb,

All parameters for function (or method) calls are pushed onto the stack, so the object itself is the same size no matter how it's  called.

Your project sounds fascinating.  Please keep us informed as to how you're getting on.

Regards,

-Mike
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Say I have declared 4 int in my header: a,b,c and d:
The 4 variables are given initial values when the class is instantiated. That the constructor only explicitly assigns non-default values to 1 or 4 variables is not relevant to the size of the instance.

You can reduce the size of a class by using appropriate sized variables (int instead of long, byte instead of int, etc.) and by making methods static, if that is possible for your class.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@mike,

thanks. That's good to know. I will let you know how it goes. I'll try and take a video of it in action. I wish I'd come across microcontrollers years ago; i'd have automated most of the lab.

@ Paul
Quote
The 4 variables are given initial values when the class is instantiated.
ah, I didn't realise that. Will have to do some reading.
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The most conspicuous unsupported features are "new" and "delete".

Someone else may have a different opinion, but I believe the specific answer is that there is no default implementation of new and delete, however they are operators and may be overloaded on a per-class basis. Hence you can implement your own allocator for your classes. The main issue is that you will need to manage your own heap and the details of ensuring that it doesn't extend down into the stack are likely quite complex.

Also, there is no STL support, and apropos built-in types, short and int are 16 bits, long is 32 bits and float and double are identical (32-bit floating-point).

Quote
The "Morse morse(13);" example creates an instance of the class, and calls the constructor for the class, but does so in statically allocated memory.

Sort of. The calculation of the amount of space each object requires is done at compile time, the space is allocated on the stack at runtime.

For instance, if a function foo() defines an object of type Morse:

Code:
void foo() {
 Morse morse(13);

 <do something>

 return;
}

and another function bar() calls foo():

Code:
void bar() {
 int someInt;

 foo();

 return;
}

and the loop() calls both:

Code:
void loop() {
  int anInt;

  foo();
  bar();
}

During the first call to foo() (directly from loop()), the Morse object is created on the stack at a particular memory location, and destroyed when the function call returns. During the second call to foo() (via bar()), the Morse object will be in a different location in memory as there is a stack frame for the bar() function active (with the someInt variable in it).



 
Logged

Pages: [1]   Go Up
Jump to: