本文介绍了你能解决这个问题吗? :C ++动态数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2005编写了以下程序。该程序是一个Dynamic

数组,类似于.NET中的System.Collections.ArrayList。该程序工作

好​​,直到我达到65536,我似乎无法弄清楚为什么,因为看起来我的

逻辑工作正常。我是.NET程序员,所以我不习惯使用未托管的C ++代码处理

。如果你觉得写得不好,请批评我的代码

写的。


这是来自main()的循环,如果你改变
65536到更大的值。

for(int i = 0; i< 65536; i ++)

{

folder.entryID =" Testing";

fc->添加(文件夹);

}


谢谢

Russell Mangel

拉斯维加斯,NV

//开始代码

#include" stdafx.h"

#include< iostream>


using namespace std;


struct Folder

{

char * entryID;

};

class FoldersCollection

{

public:

FoldersCollection :: FoldersCollection()

{

Count = 0;

容量= 0;

}

FoldersCollection :: ~FfoldersCollection()

{

delete [] m_Folders ;

}

int计数;

int Capaci ty;

void FoldersCollection :: Add(文件夹文件夹)

{

if(Capacity == 0)

{

m_Folders =新文件夹[INITIAL_CAPACITY];

m_Folders [0] .entryID = folder.entryID;

Count ++;

容量= INITIAL_CAPACITY;

}

其他

{

if(Count< ;容量)

{

m_Folders [Count] .entryID = folder.entryID;

Count ++;

}

其他

{

printf(" Resizing Array。容量:%d。\ n",容量);

调整大小();


m_Folders [Count] .entryID = folder.entryID;

Count ++;

}

}

}

文件夹* FoldersCollection :: GetList()

{

返回m_Folders;

}

私人:

文件夹* m_Folders;

文件夹* m_Temp;

static const int INITIAL_CAPACITY = 4;

void FoldersCollection :: Resize()

{

// Double容量

int newCapacity =容量* = 2;

//创建新数组

m_Temp =新文件夹[newCapacity];

//复制元素

for(int i = 0; i< Capacity; i ++)

{

m_Temp [i ] .entryID = m_Folders [i] .entryID;

}


delete [] m_Folders;

m_Folders = m_Temp;

容量= newCapacity;

}

};

void main(void)

{

FoldersCollection * fc = new FoldersCollection;

文件夹文件夹;


//工作正常到65536,更改为更大的值到崩溃程序...

//我做错了什么...

for(int i = 0;我< 65536; i ++)

{

folder.entryID =" Testing";

fc-> Add(文件夹);

}

printf(完成...计数:%d容量:%d \ n,fc->计数,fc->容量);

删除fc;

}

I have written the following program using VS2005. The program is a Dynamic
Array similar to System.Collections.ArrayList in .NET. The program works
okay until I reach 65536, I can''t seem to figure out why, as it seems my
logic is working okay. I am a .NET programmer so I am not used to dealing
with un-managed C++ code. Please criticize my code if you think it is poorly
written.

This is the loop from main() that will blow up the program if you change
65536 to a larger value.
for(int i = 0; i < 65536; i++)
{
folder.entryID = "Testing";
fc->Add(folder);
}

Thanks
Russell Mangel
Las Vegas, NV

// Begin Code
#include "stdafx.h"
#include <iostream>

using namespace std;

struct Folder
{
char *entryID;
};
class FoldersCollection
{
public:
FoldersCollection::FoldersCollection()
{
Count = 0;
Capacity = 0;
}
FoldersCollection::~FoldersCollection()
{
delete []m_Folders;
}
int Count;
int Capacity;
void FoldersCollection::Add(Folder folder)
{
if(Capacity == 0)
{
m_Folders = new Folder[INITIAL_CAPACITY];
m_Folders[0].entryID = folder.entryID;
Count ++;
Capacity = INITIAL_CAPACITY;
}
else
{
if(Count < Capacity)
{
m_Folders[Count].entryID = folder.entryID;
Count ++;
}
else
{
printf("Resizing Array. Capacity: %d.\n", Capacity);
Resize();

m_Folders[Count].entryID = folder.entryID;
Count ++;
}
}
}
Folder* FoldersCollection::GetList()
{
return m_Folders;
}
private:
Folder *m_Folders;
Folder *m_Temp;
static const int INITIAL_CAPACITY = 4;
void FoldersCollection::Resize()
{
// Double Capacity
int newCapacity = Capacity*=2;
// Create new Array
m_Temp = new Folder[newCapacity];
// Copy elements
for(int i = 0; i < Capacity; i++)
{
m_Temp[i].entryID = m_Folders[i].entryID;
}

delete []m_Folders;
m_Folders = m_Temp;
Capacity = newCapacity;
}
};
void main(void)
{
FoldersCollection *fc = new FoldersCollection;
Folder folder;

// Works okay up to 65536, change to larger value to crash program...
// What I am doing wrong...
for(int i = 0; i < 65536; i++)
{
folder.entryID = "Testing";
fc->Add(folder);
}
printf("Finished... Count: %d Capacity: %d \n", fc->Count, fc->Capacity);
delete fc;
}

推荐答案



就像现在一样,索引在循环中途超出了m_Folders

的分配容量。


As it is now, the index goes beyond the allocated capacity of m_Folders
after midway through the loop.








你将容量变量加倍,然后将原始数组索引到其边界之外的



事实直到65536才有效。


我的个人意见是你不应该在一行上合并多个陈述和

分配。犯错很容易。


如果你这样写的话,你可能会看到这个问题几乎可能是b
立即可能。

容量* = 2

int newCapacity =容量;


-


亲切的问候,

Bruno。


仅删除_nos_pam



Hi,

you double the capacity variable and then index the original array beyond
its boundaries.
the fact that it worked until 65536 is pure luck.

My personal opinion is that you should never combine multiple statements and
assignments on 1 line. it is much too easy to make mistakes.

Had you written it like this, you would have seen the problem almost
immediatly probably.
Capacity*=2
int newCapacity = Capacity;

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"




这篇关于你能解决这个问题吗? :C ++动态数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 10:16