Pages: [1]   Go Down
Author Topic: A couple of questions  (Read 459 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a couple of questions:

1. operator[](int) is not defined -> is this normal?
2. Say, I have a library and a sketch. What I would like to do (and should be possible, this is C++) is:
   in the sketch:
Code:
#define XYZ
#include <library.h>
   in the library:
Code:
#ifdef XYZ
   -> why doesn't this work?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Although you have those statements in that order in your main code, are you sure that is the order in which the compiler uses them ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

How to use this forum

Read this before posting a programming question


Where is your code?
Logged


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

I have a couple of questions:

1. operator[](int) is not defined -> is this normal?
2. Say, I have a library and a sketch. What I would like to do (and should be possible, this is C++) is:
   in the sketch:
Code:
#define XYZ
#include <library.h>
   in the library:
Code:
#ifdef XYZ
   -> why doesn't this work?

Defining something in the sketch does not make it defined in the library. That is a different compilation unit.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok. I thought the arduino IDE made one big compilation unit from the whole thing. My mistake.
Logged

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

Your sketch (the .ino file) becomes a compilation unit. The library is separate. Passing things to the library is only really possible by sending things via (say) the constructor (if you are using a class).
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I stand corrected.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nick:

Quote
Where is your code?

Code:
void setup() {
}

void loop() {
  int* test = new int[123];
}

result:

Code:
sketch_feb24b.cpp.o: In function `loop':
/usr/local/home/tomas/arduino-1.0.3/sketch_feb24b.ino:5: undefined reference to `operator new[](unsigned int)'
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never mind. I figured it out.
But still, I find it a bit strange (coming from a C++ background) to define:
Code:
void * operator new(size_t)
and not
Code:
void* operator new[](size_t)
I guess I'll add new[] and delete[] myself.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Never mind. I figured it out.
But still, I find it a bit strange (coming from a C++ background) to define:
Code:
void * operator new(size_t)
and not
Code:
void* operator new[](size_t)
I guess I'll add new[] and delete[] myself.
Don't forget that you only have a couple of KB of RAM to play with, so any dynamic memory allocation at all is a Really Bad Idea™

You should shy away from it whenever possible, and stick to using statically defined objects and variables.

Even on the nice powerful PIC microcontrollers I work with that have 128KB of RAM I don't like to use dynamic memory allocation if it can be avoided...
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

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

I guess I'll add new[] and delete[] myself.

For some reason they added new and delete but not new[] and delete[].

I think I have an outstanding bug report about that.

http://code.google.com/p/arduino/issues/detail?id=883
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Don't forget that you only have a couple of KB of RAM to play with, so any dynamic memory allocation at all is a Really Bad Idea™
Yes I understand. I just wanted to allocate 14 bytes, once, and delete them a few seconds later... I just think it's strange to get a compilation error for new[]. (I'm very new to microcontroller development, and finding out things like this every day)
Logged

Pages: [1]   Go Up
Jump to: