我很幸运在这里通过阅读别人的问题找到了很多有用的答案,但是这次我完全无能为力,所以我不得不自己提出一个问题:
我尝试创建一个将卷积应用于数据系列的程序。对于具有不同长度的卷积核(=特定数字的数组)是必需的。
我通过使用 float** 来实现它并在两次取消引用的变量中插入值。数组的个数是固定的,每个数组的长度不是固定的,所以“子数组”是用new分配的—在函数中 CreateKernels在 if 之后.
此函数然后返回 float**连同另一个指针捆绑为 main 的结构。
问题来了:
我用调试 watch 查看了内核指针的取消引用值。一切正常,所有数字都在 CreateKernels 之后符合预期返回(即从 main 范围查看内存)。但是在 main 中的下一个命令之后我的数字完全搞砸了。
如果我尝试在后续代码中使用数据,则会出现段错误。
那么我目前的推理是:
当我使用 new创建它们应该在堆中的变量,并且应该留在那里直到我 free[]变量——无论如何它们不应该被限制在CreateKernels的范围内.将指针分配给内核结构并返回它对你们中的某些人来说可能很奇怪,但它确实有效。
所以真正弄乱我的数据的是 CreatKernels 之后的下一个命令.初始化 int而不是创建 fstream不会弄乱我的号码。但是为什么?
这是我的操作系统内存管理出错了吗?或者这是一个愚蠢的编程错误?
我正在运行 Ubuntu 12.04-64 位并同时使用 Code::Blocks和 g++用于编译(所有默认设置)并且两个可执行文件都给我一个段错误。
如果有任何关于此问题的提示或经验,我将不胜感激!
这是相关代码:
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#define HW width/2
#define width1 4 // kernel width-1 (without peak)
using namespace std;
struct kernel_S
{
const float ** ppkernel;
const float * pnorm;
};
void BaseKernel(const int & width, float * base) // function that fills up an 1d-array of floats at the location of base
{
for(int i=0; i<=HW-1; i++) // fill left half as triangle
{
base[i] = i+1;
}
base[HW] = HW+1; // add peak value
for(int i=HW+1; i<=width; i++) // fill right half as decreasing triangle
{
base[i] = width-i+1;
}
}
kernel_S CreateKernels(const int &width) // function that creates an array of arrays (of variable length)
{
float base_kernel[width+1]; // create a full width kernel as basis
BaseKernel(width, base_kernel);
float * kernel[width+1]; // array of pointers, at each destination of a pointer a kernels is stored
float norm[width+1]; // norm of kernel
for(int j=0; j<=width; j++) // fill up those individual kernels
{
norm[j] = 0;
if(j<=HW) // left side up to peak
{
kernel[j] = new float[HW+j+1]; // allocate mem to a new array to store a sub-kernel in
for(int i=0; i<=HW+j; i++)
{
*(kernel[j]+i) = base_kernel[HW-j+i]; //use values from base kernel
norm[j] += base_kernel[HW-j+i]; // update norm
}
}
else if(j>=HW+1)
{
kernel[j] = new float[HW+width-j+2];
for(int i=0; i<=HW+width-j; i++)
{
*(kernel[j]+i) = base_kernel[i];
norm[j] += base_kernel[i]; // update norm
}
}
}
kernel_S result; // create the kernel structure to be returned
result.ppkernel = (const float **) kernel; // set the address in the structure to the address of the generated arrays
result.pnorm = (const float *) norm; // do the same for the norm
return result;
}
int main()
{
kernel_S kernels = CreateKernels(width1); // Create struct of pointers to kernel data
ifstream name_list(FILEPATH"name_list.txt", ios::in);// THIS MESSES UP THE KERNEL DATA
// some code that would like to use kernels
return 0;
}
最佳答案
请看
您返回指向本地堆栈数据(内核和规范)的指针。你应该动态分配:
float ** kernel = new float*[width+1]; // array of pointers, at each destination of a pointer a kernels is stored
float *norm = new float[width+1]; // norm of kernel
记得用 delete[] 删除。
但是,我建议改用 std::vector 或 std::array
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#define HW width/2
#define width1 4 // kernel width-1 (without peak)
using namespace std;
typedef std::vector<float> floatV;
typedef std::vector<floatV> floatVV;
struct kernel_S
{
floatVV kernel;
floatV norm;
};
floatV BaseKernel(int width) // function that fills up an 1d-array of floats at the location of base
{
floatV base;
base.resize(width + 1);
for(int i=0; i<=HW-1; i++) // fill left half as triangle
{
base[i] = i+1;
}
base[HW] = HW+1; // add peak value
for(int i=HW+1; i<=width; i++) // fill right half as decreasing triangle
{
base[i] = width-i+1;
}
return base;
}
kernel_S CreateKernels(const int &width) // function that creates an array of arrays (of variable length)
{
const floatV base_kernel = BaseKernel(width); // create a full width kernel as basis
kernel_S result; // create the kernel structure to be returned
result.kernel.resize(base_kernel.size());
result.norm.resize(base_kernel.size());
for(int j=0; j<=width; j++) // fill up those individual kernels
{
result.norm[j] = 0;
if(j<=HW) // left side up to peak
{
result.kernel[j].resize(HW+j+1); // allocate mem to a new array to store a sub-kernel in
for(int i=0; i<=HW+j; i++)
{
result.kernel[j][i] = base_kernel[HW-j+i]; // use values from base kernel
result.norm[j] += base_kernel[HW-j+i]; // update norm
}
}
else if(j>=HW+1)
{
result.kernel[j].resize(HW+width-j+2);
for(int i=0; i<=HW+width-j; i++)
{
result.kernel[j][i] = base_kernel[i];
result.norm[j] += base_kernel[i]; // update norm
}
}
}
return result;
}
int main()
{
kernel_S kernels = CreateKernels(width1); // Create struct of pointers to kernel data
ifstream name_list("name_list.txt", ios::in);
// some code that would like to use kernels
return 0;
}
注意 如果您希望kernel 和norm 在结果结构中是const,只需将整个结构常量:
const kernel_S kernels = CreateKernels(width1);
关于c++ - 堆数据困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13431418/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01 客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02 数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试在Rails上安装ruby,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf
文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,