gpt4 book ai didi

c++ - 在 C++ 中返回动态数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:38:38 24 4
gpt4 key购买 nike

我需要从一个函数返回一个 unsigned int*。下面的代码可以编译,但会在 Windows 64 位机器上运行时崩溃。我知道我在某个地方犯了一个愚蠢的错误,有人可以为我指出。 :p。我还在我的 header 中声明了该函数,所以我知道它不是那个错误。

请注意我已经删除了变量名和数字,因为这个函数所在的问题还没有公开发布。

功能:

 unsigned int* convertTime(unsigned int inputInteger, unsigned short inputFrac) {
unsigned int* output = new unsigned int[2];
double messageTimeFraction = double(inputFrac) * 20e-6;

output[1] = unsigned int(inputInteger + 2209032000);
output[2] = unsigned int(messageTimeFraction * 2e32);

return output; // Seconds
}

实现:

unsigned int* timeStamp;
timeStamp = convertTime(inputInteger,inputFrac);

最佳答案

嗯,对于初学者来说,您有 output[1]output[2]。数组在 c/c++ 中是零索引的,所以它们应该是:output[0]output[1]

但是,既然你问的是 c++...我强烈建议你使用 std::vectorstd::pair

(当然,为了可读性,您可能只想使用带有有用字段名称的普通结构)

关于c++ - 在 C++ 中返回动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3103164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com