Pages: [1]   Go Down
Author Topic: what can be used instead of new[]?  (Read 436 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

why o why does the arduino's IDE not let me do this:
Code:
int* foo = new int[10];
and since it won't what are the alternatives I can use?
thank you
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried this?

int * foo = new int(10);

You know by new you are invoking a constructor function, right?
Logged


Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1883
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't do it because its not C/C++.

int foo[10]; with give you an array of ten int's. Do not use new() on the Arduino until you understand C/C++ and even then think twice!.

Mark
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// new[]
inline void * operator new[] (size_t size) { return malloc (size); }
// delete[]
void operator delete[] (void * ptr) { free (ptr); }

void setup ()
  {
  int* foo = new int[10];

  // use foo here.
 
  delete [] foo; 
  }  // end of setup
 
void loop ()
  {
   
  }  // end of loop

However be warned that free() has a bug in the current library that is supplied with the IDE.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does free/delete have a bug or is there a more fundamental difficulty in implement free ?

Duane B
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a known bug in free:

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

Various threads discuss a minor change to fix it. However since the bug is not in the "source" part of the libraries, but the supplied pre-compiled part, it isn't quite as trivial for the end-user to fix it on their own.

Some of the other Arduino-like boards (I forget which exactly) are supplied with the fixed library.
Logged

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

thank you! i was tearing my hair out!
Logged

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

Will the bug have major consequences? should I check somehow for memory leak? Like using this test code http://andybrown.me.uk/wk/2011/01/01/debugging-avr-dynamic-memory-allocation/ and ending the program if i'm about to run out of memory?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The bug is more insidious than that. If you allocate memory, and then free it, it doesn't get freed correctly. Then you allocate a bit more and you might corrupt memory. It's not really to do with how much you allocate.

I think Andy Brown has a fixed library for the memory allocation. Try browsing his site for it.

I think you are fine if you just need to allocate and not free. For example, if you need to dynamically allocate "x" objects where you don't know what "x" is until runtime. But if you then free them, that's when the trouble starts (or might start).
Logged

Pages: [1]   Go Up
Jump to: