Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用alloca分配内存时转换无效_C++_Arduino - Fatal编程技术网

C++ 使用alloca分配内存时转换无效

C++ 使用alloca分配内存时转换无效,c++,arduino,C++,Arduino,此代码 #include "alloca.h" String str = "abc"; unsigned int *i; void setup() { Serial.begin(9600); i = alloca(StringLength() * sizeof(i)); unsigned int j[StringLength() * sizeof(i)]; } int StringLength() { return str.length(); } void lo

此代码

#include "alloca.h"

String str = "abc";

unsigned int *i;

void setup() {  
  Serial.begin(9600);

  i = alloca(StringLength() * sizeof(i));

  unsigned int j[StringLength() * sizeof(i)];
}

int StringLength() {
  return str.length();
}

void loop() {
}
提供以下错误消息:

函数“void setup()”中的草图\u dec11f.cpp: 草图\u dec11f.cpp:14:7:错误:从“void*”到“unsigned int*”的转换无效

我做错了什么


(也用malloc()尝试过,但也不起作用!)

malloc和alloca返回void*指针。换衣服

i = alloca(StringLength() * sizeof(i));


Malloc和alloca返回void*指针。换衣服

i = alloca(StringLength() * sizeof(i));


您肯定不想要
alloca()
。这是一个位于函数堆栈上的分配,仅在调用期间有效。它让你在函数返回时有动态数组消失(C++中你可以用RAII来完成,但是在C <代码> > ALOLACA是唯一的方法)。
你只需要在分配中加入一个角色。尝试
i=(unsigned int*)malloc(StringLength()*sizeof(*i))
。注意
sizeof(*i)
。这是一个成员的大小:
sizeof(i)
是指针的大小,可能与内部内容无关。

您肯定不想要
alloca()
。这是一个位于函数堆栈上的分配,仅在调用期间有效。它让你在函数返回时有动态数组消失(C++中你可以用RAII来完成,但是在C <代码> > ALOLACA是唯一的方法)。
你只需要在分配中加入一个角色。尝试
i=(unsigned int*)malloc(StringLength()*sizeof(*i))
。注意
sizeof(*i)
。这是一个成员的大小:<代码> siZoof(i)< /C>是指针的大小,不可能与里面的内容有关。

< P>我不确定ALOLCA,但是Malc返回的空隙*和C++不同于C,不支持从空隙*到任何其他指针类型的隐式转换。这是C++与C.</P> < P>不同的许多领域之一。我不确定ALOLCA,但是Malc返回的空隙*和C++不同于C,不支持从空隙*到任何其他指针类型的隐式转换。这是C++与C.</P> < P>不同的许多领域之一,它从AlLoga中投射返回指针:

i = (unsigned int *) alloca(StringLength() * sizeof(i));

从alloca强制转换返回指针:

i = (unsigned int *) alloca(StringLength() * sizeof(i));

C++中,空洞指针不隐式地转换为其他指针,不像C.,因此需要一个抛出:

i = static_cast<unsigned int *>(alloca(StringLength() * sizeof(i)));
i=static_cast(alloca(StringLength()*sizeof(i));
在C++中,空洞指针不隐式地转换为其他指针,不像C.,因此需要一个抛出:

i = static_cast<unsigned int *>(alloca(StringLength() * sizeof(i)));
i=static_cast(alloca(StringLength()*sizeof(i));

>你实际上是使用C++而不是C?事实上它是ARDUINO…但是,是的,你是正确的,你实际上是使用C++,而不是C?事实上它是阿杜伊诺…但是,你是更好的,不要使用<代码> MalOC<<代码>如果C++是C++,最好是,如果你写C++,不要使用<代码> MalOC 。