Questions transiting from PHP to Arduino.

Hi everyone.
I always programmed on PHP and Python, and i'm having trouble programming in C.
I will show them one at a time, and hopefully i will be understandable. :slight_smile:

PROBLEM 1

I'm trying to move an array from one to another, but i can't find a way to do it.
In PHP i would do:

$array = array(1,2,4);
$oldarray = array(4,5,6);

unset($oldarray); // Optional
$oldarray = $array;

And i would get two identical arrays that are the same as $array. How can i do that in C?

PROBLEM 2

I'm trying to make a queue using multidimentional arrays.
In PHP i would do:

$queue[] = array(1,2,3);
[after some processing]
$queue[] = array(4,5,6);
[time passed here]
$queue[] = array(7,8,9);

And i would get an array like:

queue = {
{1,2,3},
{4,5,6},
{7,8,9},
}

And i could remove an entry just doing:

unset($queue[1]); // 1 is the number of the entry i want to remove

And get a value doing:
$queue[1][2] // Outputs 3

How can i do this in C?

Are those things you hope to do on an Arduino processor?

Why? Under what circumstances would you need a dynamically created array of arbitrary strings?

Sorry, i used strings but i intended to use integers. I'll correct the main post if i can.
I will load the queue from serial and execute it one entry at time to control some lights thru DMX.

edocod:
Sorry, i used strings but i intended to use integers. I'll correct the main post if i can.

Please don't do that again. Next time just add a new message to clarify. By modifying the content of earlier messages you make the thread disconnected, confusing, and possibly incoherent.

edocod:
I will load the queue from serial and execute it one entry at time to control some lights thru DMX.

Languages like PHP and Python have built-in support for complex datatypes like dynamically created arrays. Features like that require a large, fast, easy to manage heap (a whoppin’ big chunk of memory). Typically, processors like the ones found on Arduinos do not have enough memory to support heap intensive features.

Fortunately, you do not have to reinvent the wheel. Others before you have no doubt had to do something similar. Have you done any Google searching…
https://www.google.com/search?q=queue+integer+site%3Aforum.arduino.cc

PROBLEM 1
I think it's fair to say that you try to avoid physically shifting arrays around, it's better to have a few pointers and change them.

But if you really have to move the data look at memcpy() and memmove().


Rob

Basic Arrays are not re-sizeable in C (nor do they contain any information about how big they are; they are just chunks of bare memory exactly big enough to hold the contents), meaning that more complex data structures like queues have to be implemented by the programmer, more or less manually. A queue would typically contain explicit pointers between elements...
(C++ has the "standard template library" (STL) which provides many popular data structures, including queues and resizable arrays, but STL is generally not available to Arduino programs.)

Which is because, as other people have noted, such data structures tend to require more memory than is commonly available on Arduino-class processors. First, languages like PHP throw memory at problems. Apparently a 32bit PHP array element takes about 76 bytes to store (!! How big are PHP arrays (and values) really? (Hint: BIG!) ), and you just can't do that on a cpu that has 2K bytes of memory, total. Second, there is significant overhead in dynamically allocating that data (even if it were smaller) that is also less than desirable on a small CPU. Third, the AVR hardware lacks any of the memory protection mechanisms that make it possible to detect when you've used up all of memory.

So yeah, programming an Arduino in C is going to take different thinking that a PHP program...