关于数组的动态分配内存问题

yudingjun0611 2012-07-27 12:39:05
----------------------------
开发环境:vs2008
物理内存:2GB
----------------------------
double *tempDoubleArray = new double[cnFeatrueNumber * cnFeatureDimension];

ZeroMemory(tempDoubleArray, sizeof(double) * cnFeatrueNumber * cnFeatureDimension);

//把vector<vector<double>> featureVector中的内容复制给数组
for(int i = 0; i < cnFeatrueNumber; ++i)
for(int j = 0; j < cnFeatureDimension; ++j)
tempDoubleArray[i*cnFeatrueNumber+j] = featureVector[i][j];


----------------------------
调试了下,cnFeatrueNumber是272,cnFeatureDimension是128,动态分配内存和赋值0都没问题,把vector<vector<double>> featureVector中的内容复制给数组tempDoubleArray却错了,请问这是什么问题啊?难道是堆栈大小不够了吗?
...全文
125 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
allenbein 2012-07-27
  • 打赏
  • 举报
回复
for(int i = 0; i < cnFeatrueNumber;i++)
for(int j = 0; j < cnFeatureDimension; j++)
allenbein 2012-07-27
  • 打赏
  • 举报
回复
应该是这里:

for(int i = 0; i < cnFeatrueNumber; i++)
for(int j = 0; j < cnFeatureDimension; j++)

你可以跟踪下i,j的值
lirunfa 2012-07-27
  • 打赏
  • 举报
回复
tempDoubleArray[i*cnFeatrueNumber+j] = featureVector[i][j];
改成
tempDoubleArray[i*cnFeatureDimension+j] = featureVector[i][j];

++
yudingjun0611 2012-07-27
  • 打赏
  • 举报
回复
4L你是对的,我把变量搞错了。不好意思各位,我贴了个这么二的帖子。。
赵4老师 2012-07-27
  • 打赏
  • 举报
回复
仅供参考
//在堆中开辟一个4×5的二维int数组
#include <stdio.h>
#include <malloc.h>
int **p;
int i,j;
void main() {
p=(int **)malloc(4*sizeof(int *));
if (NULL==p) return;
for (i=0;i<4;i++) {
p[i]=(int *)malloc(5*sizeof(int));
if (NULL==p[i]) return;
}
for (i=0;i<4;i++) {
for (j=0;j<5;j++) {
p[i][j]=i*5+j;
}
}
for (i=0;i<4;i++) {
for (j=0;j<5;j++) {
printf(" %2d",p[i][j]);
}
printf("\n");
}
for (i=0;i<4;i++) {
free(p[i]);
}
free(p);
}
// 0 1 2 3 4
// 5 6 7 8 9
// 10 11 12 13 14
// 15 16 17 18 19
sl51314240 2012-07-27
  • 打赏
  • 举报
回复
tempDoubleArray[i*cnFeatrueNumber+j] = featureVector[i][j];
改成
tempDoubleArray[i*cnFeatureDimension+j] = featureVector[i][j];
新铺村长 2012-07-27
  • 打赏
  • 举报
回复
第一次看到有人这样new 数组,数组的大小必须是常量吧?

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧