stl vector 的 size 变成了负值

nut799 2012-09-05 11:09:08
最近在项目中遇到一个问题。在windows 下没问题,gcc编译就会这样子。
简化描述成如下:
一个复杂的类 A 的 vector.(A 里面含有别的好几个类,以及一些vector)

vector<A> vecs;
类似这样子的语句:
A a;
printf("the size of %d before push_back", vecs.size());
vecs.push_back(a);
printf("the size of %d after push_back", vecs.size());
printf("the size of %d after push_back", vecs.size());


//结果是
0
1
-1
谁能解释这个现象呢,谢谢!
另外:
这段程序在windows环境没问题,0,1,1.但是在GCC环境下就 0,1,-1.

为什么 vector的size 变成了 负值呢 ?但是开始是1 ,然后自己变成了-1.求解

...全文
1281 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
nut799 2012-09-07
  • 打赏
  • 举报
回复
我结贴了?
nut799 2012-09-06
  • 打赏
  • 举报
回复
找到原因了。gcc编译器这个版本的bug.当vector里面的类的大小大于1k的时候,push_back以后,size的值会变成负值。网址如下:

It is a bug of the GCC compiler. I told about it somewhere in the forums long time ago. Updating the compiler solves the issue, but you have to do it manually, because the marmalade team is using a very old version yet...

The problem is that the negative vector size is just one the bugs of the compiler... There are other much worse bugs that are fixed with the last version of the gcc compiler.

std::vector::size() returns negative value again.
http://www.madewithmarmalade.com/devnet/forum/stdvectorsize-returns-negative-value-again
nut799 2012-09-05
  • 打赏
  • 举报
回复
code 如下:
printf("________________ParticleDefVector's size is %u\n", (unsigned int )theEmitter.mParticleDefVector.size() );
printf("________________ParticleDefVector's size is %u\n", (unsigned int )theEmitter.mParticleDefVector.size() );
printf("________________ParticleDefVector's size is %u\n", (unsigned int )theEmitter.mParticleDefVector.size() );
输出如下:
09-05 13:07:48.945: INFO/marmalade(3384): ________________ParticleDefVector's size is 1
09-05 13:07:48.945: INFO/marmalade(3384): ________________ParticleDefVector's size is 4294967295
09-05 13:07:48.945: INFO/marmalade(3384): ________________ParticleDefVector's size is 4294967295
iamnobody 2012-09-05
  • 打赏
  • 举报
回复
用cout 是最科学的做法.

printf 要手工核对类型.在C++中理应淘汰.
nut799 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 的回复:]

还是贴出你的A类的代码吧。
[/Quote]

我也怀疑是 类A的问题,因为类A内部没有去实现拷贝构造方法,就靠编译器了。
而A 内部蛮复杂的。包含好几个vector ,而这个vector本身存放的另外几个类。
同样这几个类有没有自己写拷贝构造方法。

具体当push_back的时候,调用拷贝构造的结果,就看编译器的解释了。
所以在windows模拟器环境下,没问题。到了gcc 环境下,就暴露出问题了。

但是我还是疑惑,为什么连续三行打印
会出1, -1 这种事情。
iamnobody 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 的回复:]

%u已经可以打印出无符号数了 。没有必要为了用zu 而去改cout,兄弟
[/Quote]

不是有符号无符号的问题,

size_t 跟 unsigned int 是两个东西 !
而且 std::vector<A>::size_type 跟 size_t 也是两个 东西.

虽然有时候它们恰好是一样的.

仔细看我给你的链接.

不改cout 也可以, 要转换 (unsigned int )vecs.size() ;
nut799 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 的回复:]

那没办法,你的编译器对标准支持不好....

用std::cout 吧...
[/Quote]

gcc编译器
nut799 2012-09-05
  • 打赏
  • 举报
回复
%u已经可以打印出无符号数了 。没有必要为了用zu 而去改cout,兄弟
恨天低 2012-09-05
  • 打赏
  • 举报
回复
还是贴出你的A类的代码吧。
iamnobody 2012-09-05
  • 打赏
  • 举报
回复
那没办法,你的编译器对标准支持不好....

用std::cout 吧...
nut799 2012-09-05
  • 打赏
  • 举报
回复
何解?
nut799 2012-09-05
  • 打赏
  • 举报
回复
%zu
code如下:
printf("________________ParticleDefVector's size is %zu\n", theEmitter.mParticleDefVector.size() );
printf("________________ParticleDefVector's size is %zu\n", theEmitter.mParticleDefVector.size() );
printf("________________ParticleDefVector's size is %zu\n", theEmitter.mParticleDefVector.size() );
输出如下:
09-05 12:40:51.547: INFO/marmalade(3184): ________________ParticleDefVector's size is zu
09-05 12:40:51.547: INFO/marmalade(3184): ________________ParticleDefVector's size is zu
09-05 12:40:51.547: INFO/marmalade(3184): ________________ParticleDefVector's size is zu

iamnobody 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

这三行打印是连续的,中间没做任何事情
[/Quote]

说了,,你还是用错了, 是 %zu
nut799 2012-09-05
  • 打赏
  • 举报
回复
这三行打印是连续的,中间没做任何事情
nut799 2012-09-05
  • 打赏
  • 举报
回复
我使用了%u 打印出来
09-05 12:26:23.856: INFO/marmalade(3037): ________________ParticleDefVector's size is 1
09-05 12:26:23.856: INFO/marmalade(3037): ________________ParticleDefVector's size is 4294967295
09-05 12:26:23.856: INFO/marmalade(3037): ________________ParticleDefVector's size is 4294967295

1 ,4294967295,4294967295

so ?
nut799 2012-09-05
  • 打赏
  • 举报
回复
我使用了%u 打印出来
09-05 12:26:23.856: INFO/marmalade(3037): ________________ParticleDefVector's size is 1
09-05 12:26:23.856: INFO/marmalade(3037): ________________ParticleDefVector's size is 4294967295
09-05 12:26:23.856: INFO/marmalade(3037): ________________ParticleDefVector's size is 4294967295

1 ,4294967295,4294967295

so ?
mujiok2003 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

http://en.cppreference.com/w/cpp/io/c/fprintf

用 %zd , %zu ,等等..

注意这是返回类型一般是size_t. 不是int !
用std::cout<< 可以不用考虑这些细节 .
[/Quote]

good point
iamnobody 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 6 楼 的回复:

http://en.cppreference.com/w/cpp/io/c/fprintf

用 %zd , %zu ,等等..

注意这是返回类型一般是size_t. 不是int !
用std::cout<< 可以不用考虑这些细节 .


如何解释 第一行打印出来是 1 ,什么也没做,再同样的打印变成-1?
[/Quote]
不用正确的格式控制符来输出 ,结果是未定义的! 也就是什么事情都可能发生.不用解释.
nut799 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

http://en.cppreference.com/w/cpp/io/c/fprintf

用 %zd , %zu ,等等..

注意这是返回类型一般是size_t. 不是int !
用std::cout<< 可以不用考虑这些细节 .
[/Quote]

如何解释 第一行打印出来是 1 ,什么也没做,再同样的打印变成-1?
iamnobody 2012-09-05
  • 打赏
  • 举报
回复
http://en.cppreference.com/w/cpp/io/c/fprintf

用 %zd , %zu ,等等..

注意这是返回类型一般是size_t. 不是int !
用std::cout<< 可以不用考虑这些细节 .
加载更多回复(13)

64,682

社区成员

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

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