17
votes
Accepted
Why is it considered bad practice to use the 'new' keyword in Arduino?
Most Arduinos (like the Uno or Nano) have very few RAM, thus you first need to make sure that you never allocate too much memory. Dynamically allocating memory can also lead to heap fragmentation (...
8
votes
Accepted
How do string pointers work in Arduino?
String is not a simple type like an int or a char. It is a class with many member functions and, more importantly, operators. When you create the object it allocates room for that object either on ...
7
votes
Call by reference
void fcn1(int *variable)
{
fcn2(*variable);
}
This function takes a pointer to an integer as parameter, and then passes the value of the integer to the second function.
To do it by reference, use ...
7
votes
Accepted
ArduinoJSON v6 - Passing buffer as function parameter
The StaticJsonDocument is a template class. The template value in <> is here only the size of the internal buffer, but every size used generates a different class. (memory usage!)
To have the ...
7
votes
Why is it considered bad practice to use the 'new' keyword in Arduino?
Dynamic allocation is generally discouraged in embedded applications because you cannot guarantee that you do not exceed (attempt to allocate more than) the available memory. Static allocation will ...
6
votes
Accepted
User callback functions
I don't know the user's function parameters,
A common way for dealing with this issue is to pass a generic pointer to
arbitrary data. See below.
return type
It makes little sense to allow for ...
6
votes
Accepted
Storing an array of function pointers
This all gets very much trickier than just an array of function pointers.
Your functions are methods in unknown objects. As such you can't have one single type that stores pointers to the functions ...
5
votes
Storing an array of function pointers
This is not a direct answer to your question, as I have no such answer,
and I believe the answer you are seeking would be of no use to you.
The problem with your question is that your method of ...
5
votes
Accepted
Setting an array pointer to null on declaration
Here is a full example that shows you all thing things you have been asking over the past few days:
int colors[][3] = {
{255, 0, 0},
{0, 255, 0},
{0, 0, 255}
};
#define NCOLOR (sizeof(...
5
votes
Why is it considered bad practice to use the 'new' keyword in Arduino?
For starters, fix your library
As noted by @crasic, dynamic memory allocation is generally not recommended for embedded systems. It may be acceptable for embedded devices which have a larger amount ...
4
votes
Storing an array of function pointers
I'm not sure if it will not blow your memory limit as every pointer takes up 16 bits. But in C you usually do it this way:
void func1() {
puts("Hello");
}
void func2() {
puts("World!");
}
...
4
votes
Accepted
Works with gcc, not with Arduino. error: taking address of temporary array
It is quite right, using that kind of syntax is not allowed. It's a bit of a pain, but it's ok since there is an alternative method - kind of a "trick" if you will.
That trick is to use a string, not ...
4
votes
Dereferencing char into a struct
In your situation, your best bet is probably to use the standard C function strcpy:
payload_t payload;
char* source = "abcdef";
strcpy(payload.sensorid, source);
Note that source string does not ...
4
votes
Accepted
Accessing funcition in object via `->` is crashing (resetting) Arduino
Derived derivedObj; is a local variable. It's deleted when the function exits. You have saved a pointer to it, but with that object now gone your pointer points to nothing of any interest - so doing ...
4
votes
Can you create an array for functions?
Here's an example of how to do it:
jump_table.ino
int func1(int arg) { return arg + 1; }
int func2(int arg) { return arg + 2; }
int func3(int arg) { return arg + 3; }
int func4(int arg) { return ...
4
votes
Accepted
Why I cant get sizeof a pointer Array
A pointer is just a pointer. It's not an array. On 8 bit Arduinos the memory address range is a 16 bit value, so a pointer will always be 2 bytes.
You need to return two values from your function - ...
4
votes
Accepted
How to send multiple bytes with Wire without copying
No, this function overwrite of Wire.write() is meant for buffers, meaning connected spaces in memory, which can be covered by simply incrementing the buffer. That is not the case for distinct ...
3
votes
Accepted
Setting a pointer to NULL in Arduino library - error: ‘NULL’ was not declared in this scope
I am writing my own library for using with my Arduino.
You should really include Arduino.h, i.e.
#include <Arduino.h>
Not only will that define NULL for you, but you also get the other ...
3
votes
Accepted
Mysterious behavior when using objects
Dave X is absolutely right.
I never assigned the senRoll pointer to an object. Then when I tried to dereference it, it went to some random address. I guess there are very few memory protections, so ...
3
votes
User callback functions
You can't call arbitrary functions (with random return types, and any number of arguments) but you can specify a callback function that takes a certain number of specific arguments, and returns a ...
3
votes
Using pointers with Arduino
You're printing the address of the pointer, not the contents of the pointer.
Say you have the memory map of:
100 20 test1 low byte
101 0 test1 high byte
102 100 ptr1 low byte
103 0 ...
3
votes
Accepted
Message Interpreter and Handler - How to store a function name in a struct?
typedef void (*MessageHandler)(void); // def messagehandler as pointer to func
struct AvcInMessage {
MessageHandler msgHandler;
byte dataSize;
byte data[8];
char ...
3
votes
Accepted
Having an issue accessing values from a pointer
As Majenko mentioned in the comment, you created new local arrays in the constructor, but never put anything in the member arrays of the classes. They have the same name, but are not the same variable....
3
votes
How do arrays work?
BUZZER_NOTE notes[]; //-- Funny how the same declaration tried below DOES work if it's inside a class
It doesn't really. C++ language prohibits that. It is just a non-standard quirk of GCC compiler ...
3
votes
Accepted
Converting c string to integer to unsigned char
Not sure if or why you want to convert char* to int, but if you need to, there are atoi(), atol(), strtol(), strtoul() to consider, or rolling your own function.
Pros and cons are discussed here. Main ...
3
votes
Failure using references variables in classes
I'm not sure what value this is going to have for Arduino users, but here goes. Your question seem to be an over-complicated form of this the following:
My code:
struct MessageTFT {uint8_t txt_size = ...
2
votes
Accepted
Keep References Between Objects
The member variable target does not need to be passed by reference at all.
void Tween::to(float target, float from, float to, float duration, float (* ease)(float t, float b, float c, float d))
{
...
2
votes
Accepted
sending ctrl-z in a struct?
First off, consider trying '\032' or '\x1A' for the ctrl-z. (See, eg, How to send ctrl+z in C on stackoverflow.)
To send a ctrl-z, use something like:
Keyboard.press(ctrlKey);
Keyboard.press('z');
...
2
votes
Setting a pointer to NULL in Arduino library - error: ‘NULL’ was not declared in this scope
You should make sure your header includes the stddef.h header, which holds all these standard definitions (hence its name):
#include <stddef.h>
2
votes
How to pass a string pointer to a function
What you have there are not String objects, but string literals. Their type is const char *
You should avoid using String objects if at all possible. Instead use character arrays.
To get a character ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pointer × 95array × 27
c++ × 22
arduino-uno × 15
programming × 13
string × 9
class × 9
c × 7
functions × 7
serial × 6
memory-usage × 6
memory × 6
arduino-ide × 5
library × 5
struct × 4
arduino-nano × 3
interrupt × 3
nrf24l01+ × 3
variables × 3
data-type × 3
arduino-mega × 2
timers × 2
isr × 2
json × 2
progmem × 2