不合逻辑的标准::矢量大小? [英] Illogical std::vector size?

查看:68
本文介绍了不合逻辑的标准::矢量大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




首先是一些背景。


我有一个结构,


struct sFileData

{

char * sSomeString1;

char * sSomeString2;

int iSomeNum1;

int iSomeNum2;

sFileData(){...};

~sFileData(){...};

sFileData(const sFileData&){...};

const sFileData operator =(const sFileData& s){...}

};


std :: vector< sFileData,std :: allocator< sFileData>>地址_;


为了简单起见我删除了''的身体

据我所知,我没有内存泄漏。 />

然后我读了一个文件,(每行是190个字符,大部分是空格)。

在每一行我都读''信息填写结构。


因为行中有很多空格我确保我的

数据被修剪了。


因此实际上sSomeString1和sSomeString2永远不会超过10个字符,

(尽管在文件中它们最多可以有40个字符)。


我选择了矢量,因为在阅读文件后我需要搜索

sSomeString1和sSomeString2,(其他原因没有。)


但我的问题是地址_的大小与文件的大小不一致。


文件大约是13Mb,大约有100000''行''每个190个字符。

因为我删除了空格s和我将2个数字转换为int,(来自char)。我猜b $ b猜我不应该使用超过一半,5Mb。


但是加载后我看到我使用了大约40Mb,(比

原始大小)。


据我所知,你无法真正说出矢量的大小,但是我使用了

窗口和任务管理器,我可以看到我之前的应用程序的大小和

阅读文件后,(我别无其他)。


那么什么可能是这些不一致的原因?

我怎样才能优化我的代码来压缩那些40mb甚至更多?


非常感谢

Simon

Hi,

First some background.

I have a structure,

struct sFileData
{
char*sSomeString1;
char*sSomeString2;
int iSomeNum1;
int iSomeNum2;
sFileData(){...};
~sFileData(){...};
sFileData(const sFileData&){...};
const sFileData operator=( const sFileData &s ){...}
};

std::vector< sFileData, std::allocator<sFileData>> address_;

for the sake of simplicity I remove the body of the ''tors
I have no memory leaks as far as I can tell.

Then I read a file, (each line is 190 chars mostly blank spaces).
In each line I ''read'' info to fill in the structure.

Because there are some many blank spaces in the line I make sure that my
data is ''trimmed''.

So in effect sSomeString1 and sSomeString2 are never more than 10 chars,
(although in the file they could be up to 40 chars).

I chose vectors because after reading the file I need to do searches of
sSomeString1 and sSomeString2, (no other reasons really).

But my problem is the size of address_ is not consistent with the size of
the file.

The file is around 13Mb with around 100000 ''lines'' of 190 chars each.
Because I remove blank spaces and I convert 2 numbers to int, (from char). I
guess I should not use more than half, 5Mb.

But after loading I see that I used around 40Mb, (3 times more than the
original size).

as far as I can tell you cannot really tell the size of a vector, but I use
windows and the task manager and I can see the size of my app before and
after reading the file, (I do nothing else).

So what could be the reason for those inconsistencies?
How could I optimize my code to compress those 40mb even more?

Many thanks

Simon

推荐答案

simon schreef:
simon schreef:


我有一个结构,

struct sFileData
{* char * sSomeString1;
char * sSomeString2;
int iSomeNum1;
int iSomeNum2;
sFileData(){...};
~sFileData(){...};
sFileData(const sFileData&){...};
const sFileData operator =(const sFileData& s){...}
};

std :: vector< ; sFileData,std :: allocator< sFileData>>地址_;

为了简单起见,我删除了躯体的身体
据我所知,我没有内存泄漏。

然后我读取一个文件,(每行是190个字符,大部分是空格)。
在每行中我都会读到''信息来填写结构。

因为有很多空格我确保我的
数据被修剪了。

因此实际上sSomeString1和sSomeString2永远不会超过10个字符,
(尽管在文件中)它们可以达到40个字符。

我选择了向量,因为在阅读文件之后我需要搜索
sSomeString1和sSomeString2,(没有其他原因)。

但我的问题是地址的大小与文件的大小不一致。

文件大约是13Mb,大约有100000''行'的190个字符每个。
因为我删除了空格,我将2个数字转换为int,(来自char)。我估计我不应该使用超过一半,5Mb。

但是在加载后我看到我使用了大约40Mb,(比原始尺寸大3倍)。

据我所知,你无法真正告诉你矢量的大小,但是我使用了
windows和任务管理器,我之前可以看到我的应用程序的大小和
读完文件之后,(我别无其他)。
Hi,

First some background.

I have a structure,

struct sFileData
{
char*sSomeString1;
char*sSomeString2;
int iSomeNum1;
int iSomeNum2;
sFileData(){...};
~sFileData(){...};
sFileData(const sFileData&){...};
const sFileData operator=( const sFileData &s ){...}
};

std::vector< sFileData, std::allocator<sFileData>> address_;

for the sake of simplicity I remove the body of the ''tors
I have no memory leaks as far as I can tell.

Then I read a file, (each line is 190 chars mostly blank spaces).
In each line I ''read'' info to fill in the structure.

Because there are some many blank spaces in the line I make sure that my
data is ''trimmed''.

So in effect sSomeString1 and sSomeString2 are never more than 10 chars,
(although in the file they could be up to 40 chars).

I chose vectors because after reading the file I need to do searches of
sSomeString1 and sSomeString2, (no other reasons really).

But my problem is the size of address_ is not consistent with the size of
the file.

The file is around 13Mb with around 100000 ''lines'' of 190 chars each.
Because I remove blank spaces and I convert 2 numbers to int, (from char). I
guess I should not use more than half, 5Mb.

But after loading I see that I used around 40Mb, (3 times more than the
original size).

as far as I can tell you cannot really tell the size of a vector, but I use
windows and the task manager and I can see the size of my app before and
after reading the file, (I do nothing else).




1)Windows任务管理器不适用于此

2)向量存储sFileData对象,而不是字符串本身

3)即使向量具有多余的大小(这是常见的,不想在每个pusch_back之后重新分配
)它赢了不包括字符串

4)new []的许多实现分配至少16个字节,加上

删除所需的开销[]

5)那又怎样? 40MB不是很多。当它超过1.5Gb时担心。记忆

很便宜。编写自定义字符串类不是。 BTDT。


HTH,

Michiel Salters



1) Windows Task Manager is not suited for this
2) vector only stores sFileData objects, not the strings themselves
3) Even when vector has excess size (which is common, don''t want to
reallocate after each pusch_back) it won''t include the strings
4) Many implementations of new[] allocate at least 16 bytes, plus
the overhead needed for delete[]
5) So what? 40MB is not a lot. Worry when it exceeds 1.5Gb. Memory
is cheap. Writing a custom string class is not. BTDT.

HTH,
Michiel Salters


>
1)Windows任务管理器不适合这个


是的,但这首先引起了怀疑。

什么可能更好吗?

2)vector只存储sFileData对象,而不是字符串本身
3)即使vector有多余的大小(这很常见,也不想
在每个pusch_back之后重新分配)它不包含字符串
4)new []的许多实现分配至少16个字节,加上
delete []


你是说std​​ :: string在这种情况下可能真的更好吗?

什么可能是更好的方法?

5)那又怎样? 40MB不是很多。当它超过1.5Gb时担心。记忆
很便宜。编写自定义字符串类不是。 BTDT。


40Mb或4Gb,还有一些不太合适的东西,我会先知道它是什么,而不是在地毯下刷它。 br />
HTH,
Michiel Salters
1) Windows Task Manager is not suited for this
yea, but it was what raised suspicion in the first place.
What might be better?
2) vector only stores sFileData objects, not the strings themselves
3) Even when vector has excess size (which is common, don''t want to
reallocate after each pusch_back) it won''t include the strings
4) Many implementations of new[] allocate at least 16 bytes, plus
the overhead needed for delete[]
Are you saying that std::string might actually be better in that case?
What might be a better way?
5) So what? 40MB is not a lot. Worry when it exceeds 1.5Gb. Memory
is cheap. Writing a custom string class is not. BTDT.
40Mb or 4Gb, there is still something not quite right, and i would preffer
to know what it is rather than brushing it under the rug.

HTH,
Michiel Salters



> > 1)Windows任务管理器不适合这个
> > 1) Windows Task Manager is not suited for this

是的,但这首先引起了怀疑。
什么可能更好?

yea, but it was what raised suspicion in the first place.
What might be better?




怀疑什么?它只告诉整个程序现在使用40 MB

而不是程序的这个特定部分使用40 MB。

或者如果它是总数的变化在那里使用的内存量可能是近乎无数其他原因,程序现在正在使用

40 MB而不是预期的5 MB增加。

你需要一个分析器来检查它是否确实是结构的向量

这就是问题。



What suspicion? It only tells that the total program is now using 40 MB
not that this particular part of your program is using 40 MB.
Or if it is a change in the total amount of memory used there could be
a near infinite number of other reasons that the program is now using
40 MB instead of the expected 5 MB increase.
You''d need a profiler to check if it is indeed the vector of structs
that is the problem.


这篇关于不合逻辑的标准::矢量大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆