简体   繁体   English

将浮点数从字符串放入空数组

[英]Placing floats from string into void array

I get a segmentation fault when my function reads floats from a string and places them in a void array. 当我的函数读取字符串中的浮点数并将它们放置在void数组中时,出现分段错误。 The segfault occurs after about 200 iterations of the for loop in the following code: 段代码在以下代码中的for循环约200次迭代后发生:

// Allocate memory
void** data;
data = (void**)malloc(num_vals * sizeof(float));

// Convert text to floats
(*(float**)data)[0] = atof(strtok(text, " "));  
for(int index=1; index<num_vals; index++) {
    (*(float**)data)[index] = atof(strtok(NULL, " "));   
    std::cout << (*(float**)data)[index] << std::endl;
}

The void array is necessary because the size and type of data in the string are determined at run-time. void数组是必需的,因为字符串中数据的大小和类型是在运行时确定的。 I've tried increasing the malloc size, but it doesn't change anything. 我尝试增加malloc的大小,但它没有任何改变。 Any thoughts? 有什么想法吗?

Seriously?? 认真吗?

std::vector<float> data;
std::istringstream str(text);
float fv;
while (str >> fv)
{
  data.push_back(fv);
}

Now that's c++ 现在是c ++

Why do you convert to void ** ??? 为什么要转换为void ** ??? You code contains couple errors on indexing, so let me show some reasonable changes 您的代码在索引编制中包含一些错误,因此让我展示一些合理的更改

float* data;
data = (float*)malloc(num_vals * sizeof(float));

// Convert text to floats
data[0] = atof(strtok(text, " "));  
for(int index=1; index<num_vals; index++) {
 data[index] = atof(strtok(NULL, " "));   
 std::cout << data[index] << std::endl;
}

As much as it pains me to do so, here is a version of your code that probably does what you want. 这样做让我很痛苦,这是您的代码的一个版本,可能可以满足您的要求。

// Allocate memory
void* data;
data = malloc(num_vals * sizeof(float));

// Convert text to floats
((float*)data)[0] = atof(strtok(text, " "));  
for(int index=1; index<num_vals; index++) {
    ((float*)data)[index] = atof(strtok(NULL, " "));   
    std::cout << ((float*)data)[index] << '\n';
}

Note, however, that if you worked for me and tried to check in that code, we would have a serious discussion about your choice of career. 但是请注意,如果您为我工作并尝试签入该代码,我们将对您的职业选择进行认真的讨论。

I'd rather see something like this: 我宁愿看到这样的事情:

std::vector<float> v;
std::copy(std::istream_iterator<float>(std::istringstream(text)),
          std::istream_iterator<float>(),
          std::back_inserter(v));

Ps Rob's rule #47: Never say std::endl when you mean '\\n' . Ps Rob的规则#47:当您的意思是'\\n'时,请不要说std::endl

我认为,由于您正在定义void指针的指针并将其分配/广播到void指针,因此它为每个元素分配4个字节的内存,因为在C / C ++中,关于指针类型的指针始终为4个字节浮子不够大

You got your types mixed up in your inexplicable attempt to create this monster under the pretence of writing "C++". 在以编写“ C ++”为幌子的莫名其妙的尝试来创建这个怪物的过程中,您混淆了各种类型。 Anyway. 无论如何。 what you're mallocing is nothing but a float* , so you need to cast data back to float* : 您要分配的内容只是float* ,因此您需要将data强制转换回float*

((float*)data)[0] = myfloat;

There're several issues. 有几个问题。 One is - data should be void * , you have redundant * . 一种是- data应该为void * ,而您有冗余* Other might be alignment, i'm not sure you are able to place a float in any location in the memory. 其他可能是对齐方式,我不确定您是否可以将浮点数放在内存中的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM