dtostrf function not working

Hi,
Using the latest version of IDE (1.6.8 ) with Arduino Due I seem not to be able to use the dtostrf function from stdlib.h. Have seen several topics with dtostrf but was not able to solve this in my code with the answers given. The code is brought back to the bare minimum.

#include <stdlib.h>
void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Starting");
  
  char* str = new char[30];
  double flt = 2.4567F;
  
  sprintf(str, "%.1f", flt );    
  Serial.println(str);
  dtostrf(flt,2,1,str);
  Serial.println(str);
}

Compiling results in error: "'dtostrf' was not declared in this scope".

When the line with dtostrf is commented out, compiling ends successfully, showing I might use sprintf as well, but it just annoys me.

What is wrong here? Any clue?

Roelof

Hello,

Roelof999:
What is wrong here?

Two problems: 1. You failed to free the buffer. 2. You are using a buffer allocated from the heap instead of the stack.

Roelof999:
Compiling results in error: "'dtostrf' was not declared in this scope".

I cannot find the function prototype in the header files used for the Due. I suspect the function is simply not available for the Due.

Roelof999:
Compiling results in error: "'dtostrf' was not declared in this scope".

To fix this add the line:

#include <avr/dtostrf.h>

pert:
To fix this add the line:

#include <avr/dtostrf.h>

Let us know how that works for you.

Say what??? What "buffer"? Where is he allocating anything on the heap, or anything that requires freeing? the only "buffer" he has is a character array, which is a stack-based automatic variable.

Regards,
Ray L.

RayLivingston:
Where is he allocating anything on the heap, or anything that requires freeing?

char* str = new char[30];

RayLivingston:
Say what??? What "buffer"? Where is he allocating anything on the heap, or anything that requires freeing? the only "buffer" he has is a character array, which is a stack-based automatic variable.

He is doing it here:

  char* str = new char[30];

This reads "allocate me 30 bytes of heap, and put a pointer to that 30 bytes in str". If there is 3k of heap, his chip will jam shut no later than 100 loops() after it is started. Probably sooner.

PaulMurrayCbr:
He is doing it here:

  char* str = new char[30];

This reads "allocate me 30 bytes of heap, and put a pointer to that 30 bytes in str". If there is 3k of heap, his chip will jam shut no later than 100 loops() after it is started. Probably sooner.

It's allocated in 'setup()', not 'loop()'.