如何删除除 ArduinoSTL 向量函数之外的所有内容

How to remove all but the vector function of ArduinoSTL

问题

我在 Arduino UNO.
上做了一个 C++ 程序 运行 我只使用 ArduinoSTL 作为他的矢量功能,但它使用了 459 字节的 Arduino RAM (23% 的浪费 RAM 太多了对于我所做的),用这个库编译需要多花 5 秒(这很烦人)。


我想做什么

我想能够改变数组的大小,也许用向量。
不想使用那些白白浪费 RAM 的库。

我想做什么

虽然如此,也许我可以 删除除 ArduinoSTL 的向量函数之外的所有内容,所以它将编译更快并占用更少的 RAM。

我试过了。
ArduinoSTL的目录中,有很多.cpp个文件,其中一个名为Vector.cpp.
我的解决方案是删除除 Vector.cpp.
以外的所有内容 没用

解决方案 1

使用常规数组!

使用大的常规数组会更好有时因为ArduinoSTL占用大量内存 并删除让足够 free space 来使用这样简单的数组并且编译是 faster.
的方式 在我所做的事情中,我并不真的需要矢量。一个大数组就够了。


解决方案 2

使用stdlib.h!

提供malloc()calloc()realloc()free()等功能。
主要优点是它是标准并且不仅适用于Arduino
这是 常规 C++ 编码中的一段代码,展示了如何使用它。

#include <stdlib.h> //Mandatory for memory manipulation
#include <stdio.h>  //Used to for the command prompt

int main(){
    uint8_t* MemBlock=nullptr; //Just a pointer pointing to nothing
    int BlockSize=0;           //The size of the block in bytes

    bool Condition=true; //The condition for the if() statement
    
    if(Condition){
        BlockSize=64;
        MemBlock=(uint8_t)malloc(BlockSize); //Allocates a Memory Block

        uint8_t* P=MemBlock; //Defines a pointer that points to same location as the MemBlock pointer

        for(int i=0;i<BlockSize;i++){
            *P=i*23%101; //Change the pointed value by P
            P++; //Moves to the next value (of MemBlock)
        } 
    }else{
        BlockSize=256;
        MemBlock=(uint8_t)malloc(BlockSize); //Allocates a Memory Block

        for(int i=0;i<BlockSize;i++){
            MemBlock[i]=i*17%23; //Fills the Memory Block with values
        }
    }

    uint8_t* P=MemBlock; //Defines a pointer that points to same location as the MemBlock pointer

    for(int i=0;i<BlockSize;i++){
        std::cout<<*P++<<' '; //Displays all values of the Memory Block, *P++ is same as doing *P then P++
    }
    std::cout<<'\n'; //ends the line to show it

    free(MemBlock); //Deallocates the Memory Block to prevent "memory leaks"

    return 0;
}

Arduino IDE 不需要包含该库,因为它已经默认包含。

这里是 link 有关库的更多详细信息:stdlib.h - C++ reference

如果有人知道快速矢量库,请随时分享!