Go Down

Topic: A couple of questions (Read 525 times) previous topic - next topic

sokopok

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: [Select]
#define XYZ
#include <library.h>

   in the library:
Code: [Select]
#ifdef XYZ
   -> why doesn't this work?

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


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: [Select]
#define XYZ
#include <library.h>

   in the library:
Code: [Select]
#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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

sokopok

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

Nick Gammon

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).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

sokopok

Thanks, I stand corrected.

sokopok

Nick:

Quote
Where is your code?


Code: [Select]
void setup() {
}

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


result:

Code: [Select]
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)'

sokopok

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

majenko


Never mind. I figured it out.
But still, I find it a bit strange (coming from a C++ background) to define:
Code: [Select]
void * operator new(size_t)
and not
Code: [Select]
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...

Nick Gammon


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
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

sokopok

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)

Go Up