Skip to main content
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 (...
chrisl's user avatar
  • 16.3k
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 ...
Majenko's user avatar
  • 105k
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 ...
tttapa's user avatar
  • 1,320
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 ...
Juraj's user avatar
  • 18.1k
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 ...
crasic's user avatar
  • 226
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 ...
Edgar Bonet's user avatar
  • 43.1k
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 ...
Majenko's user avatar
  • 105k
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 ...
Edgar Bonet's user avatar
  • 43.1k
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(...
Majenko's user avatar
  • 105k
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 ...
Graham's user avatar
  • 179
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!"); } ...
Kwasmich's user avatar
  • 1,513
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 ...
Majenko's user avatar
  • 105k
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 ...
jfpoilpret's user avatar
  • 9,132
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 ...
Majenko's user avatar
  • 105k
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 ...
Alex Hajnal's user avatar
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 - ...
Majenko's user avatar
  • 105k
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 ...
chrisl's user avatar
  • 16.3k
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 ...
Majenko's user avatar
  • 105k
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 ...
Charles Vorbach's user avatar
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 ...
Nick Gammon's user avatar
  • 38.2k
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 ...
Nick Gammon's user avatar
  • 38.2k
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 ...
JRobert's user avatar
  • 15.3k
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....
gfrodo's user avatar
  • 46
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 ...
AnT stands with Russia's user avatar
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 ...
ocrdu's user avatar
  • 1,795
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 = ...
timemage's user avatar
  • 5,231
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>
Majenko's user avatar
  • 105k
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 ...
Majenko's user avatar
  • 105k
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)) { ...
Kingsley's user avatar
  • 773
2 votes
Accepted

Initialize and read from a PROGMEM array of pointers to PROGMEM arrays

const byte* const message[] PROGMEM = to use an item, load it in RAM strcpy_P(buffer, (byte*)pgm_read_word(&(message[i]))); source Arduino reference - PROGMEM
Juraj's user avatar
  • 18.1k

Only top scored, non community-wiki answers of a minimum length are eligible