vector num[100];清0清空操作

wingrez 2018-08-20 10:49:14
vector<int> num[100];

现在要对num中所有的元素清0,或者清空。
遍历num,clear操作我知道是可以的。
用memset(num,0,sizeof(num));可以吗?
...全文
1552 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
craigtao 2020-02-11
  • 打赏
  • 举报
回复
那个叫 赵四的,你是答非所问,
啊大1号 2018-08-22
  • 打赏
  • 举报
回复
《The Intel 64 and IA-32 Architectures Software Developer's Manual》
flybirding10011 2018-08-22
  • 打赏
  • 举报
回复
(≧∇≦)/
weixin_42996664 2018-08-22
  • 打赏
  • 举报
回复
调用memset函数可以把数组全部清零
赵4老师 2018-08-21
  • 打赏
  • 举报
回复
_VEC_memzero:
78AB0184: 66 0F EF C0 pxor xmm0,xmm0
78AB0188: 51 push ecx
78AB0189: 53 push ebx
78AB018A: 8B C1 mov eax,ecx
78AB018C: 83 E0 0F and eax,0Fh
78AB018F: 85 C0 test eax,eax
78AB0191: 74 25 je 78AB01B8
78AB0193: 8B D8 mov ebx,eax
78AB0195: F7 DB neg ebx
78AB0197: 83 C3 10 add ebx,10h
78AB019A: 2B D3 sub edx,ebx
78AB019C: 33 C0 xor eax,eax
78AB019E: 52 push edx
78AB019F: 8B D3 mov edx,ebx
78AB01A1: 83 E2 03 and edx,3
78AB01A4: 0F 85 B4 01 00 00 jne 78AB035E
78AB01AA: C1 EB 02 shr ebx,2
78AB01AD: 74 08 je 78AB01B7
78AB01AF: 89 01 mov dword ptr [ecx],eax
78AB01B1: 8D 49 04 lea ecx,[ecx+4]
78AB01B4: 4B dec ebx
78AB01B5: 75 F8 jne 78AB01AF
78AB01B7: 5A pop edx
78AB01B8: 8B C2 mov eax,edx
78AB01BA: 83 E2 7F and edx,7Fh
78AB01BD: C1 E8 07 shr eax,7
78AB01C0: 74 3E je 78AB0200
78AB01C2: 8D 24 24 lea esp,[esp]
78AB01C5: 90 nop
78AB01C6: 90 nop
78AB01C7: 90 nop
78AB01C8: 90 nop
78AB01C9: 90 nop
78AB01CA: 90 nop
78AB01CB: 90 nop
78AB01CC: 90 nop
78AB01CD: 90 nop
78AB01CE: 90 nop
78AB01CF: 90 nop
78AB01D0: 66 0F 7F 01 movdqa xmmword ptr [ecx],xmm0
78AB01D4: 66 0F 7F 41 10 movdqa xmmword ptr [ecx+10h],xmm0
78AB01D9: 66 0F 7F 41 20 movdqa xmmword ptr [ecx+20h],xmm0
78AB01DE: 66 0F 7F 41 30 movdqa xmmword ptr [ecx+30h],xmm0
78AB01E3: 66 0F 7F 41 40 movdqa xmmword ptr [ecx+40h],xmm0
78AB01E8: 66 0F 7F 41 50 movdqa xmmword ptr [ecx+50h],xmm0
78AB01ED: 66 0F 7F 41 60 movdqa xmmword ptr [ecx+60h],xmm0
78AB01F2: 66 0F 7F 41 70 movdqa xmmword ptr [ecx+70h],xmm0
78AB01F7: 8D 89 80 00 00 00 lea ecx,[ecx+00000080h]
78AB01FD: 48 dec eax
78AB01FE: 75 D0 jne 78AB01D0
78AB0200: 85 D2 test edx,edx
78AB0202: 74 2C je 78AB0230
78AB0204: 8B C2 mov eax,edx
78AB0206: C1 E8 04 shr eax,4
78AB0209: 74 0A je 78AB0215
78AB020B: 66 0F 7F 01 movdqa xmmword ptr [ecx],xmm0
78AB020F: 8D 49 10 lea ecx,[ecx+10h]
78AB0212: 48 dec eax
78AB0213: 75 F6 jne 78AB020B
78AB0215: 83 E2 0F and edx,0Fh
78AB0218: 74 16 je 78AB0230
78AB021A: 8B C2 mov eax,edx
78AB021C: 33 DB xor ebx,ebx
78AB021E: C1 EA 02 shr edx,2
78AB0221: 74 08 je 78AB022B
78AB0223: 89 19 mov dword ptr [ecx],ebx
78AB0225: 8D 49 04 lea ecx,[ecx+4]
78AB0228: 4A dec edx
78AB0229: 75 F8 jne 78AB0223
78AB022B: 83 E0 03 and eax,3
78AB022E: 75 4A jne 78AB027A
78AB0230: 5B pop ebx
78AB0231: 58 pop eax
78AB0232: C3 ret
赵4老师 2018-08-21
  • 打赏
  • 举报
回复
《The Intel 64 and IA-32 Architectures Software Developer's Manual》
赵4老师 2018-08-21
  • 打赏
  • 举报
回复
知道为什么memset快吗?
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\memset.asm
        page    ,132
title memset - set sections of memory all to one byte
;***
;memset.asm - set a section of memory to all one byte
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; contains the memset() routine
;
;*******************************************************************************

.xlist
include cruntime.inc
.list

page
;***
;char *memset(dst, value, count) - sets "count" bytes at "dst" to "value"
;
;Purpose:
; Sets the first "count" bytes of the memory starting
; at "dst" to the character value "value".
;
; Algorithm:
; char *
; memset (dst, value, count)
; char *dst;
; char value;
; unsigned int count;
; {
; char *start = dst;
;
; while (count--)
; *dst++ = value;
; return(start);
; }
;
;Entry:
; char *dst - pointer to memory to fill with value
; char value - value to put in dst bytes
; int count - number of bytes of dst to fill
;
;Exit:
; returns dst, with filled bytes
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************

CODESEG

extrn _VEC_memzero:near
extrn __sse2_available:dword

public memset
memset proc \
dst:ptr byte, \
value:byte, \
count:dword

OPTION PROLOGUE:NONE, EPILOGUE:NONE

.FPO ( 0, 3, 0, 0, 0, 0 )

mov edx,[esp + 0ch] ; edx = "count"
mov ecx,[esp + 4] ; ecx points to "dst"

test edx,edx ; 0?
jz short toend ; if so, nothing to do

xor eax,eax
mov al,[esp + 8] ; the byte "value" to be stored

; Special case large block zeroing using SSE2 support
test al,al ; memset using zero initializer?
jne dword_align
cmp edx,080h ; block size exceeds size threshold?
jb dword_align
cmp DWORD PTR __sse2_available,0 ; SSE2 supported?
je dword_align

jmp _VEC_memzero ; use fast zero SSE2 implementation
; no return

; Align address on dword boundary
dword_align:

push edi ; preserve edi
mov edi,ecx ; edi = dest pointer

cmp edx,4 ; if it's less then 4 bytes
jb tail ; tail needs edi and edx to be initialized

neg ecx
and ecx,3 ; ecx = # bytes before dword boundary
jz short dwords ; jump if address already aligned

sub edx,ecx ; edx = adjusted count (for later)
adjust_loop:
mov [edi],al
add edi,1
sub ecx,1
jnz adjust_loop

dwords:
; set all 4 bytes of eax to [value]
mov ecx,eax ; ecx=0/0/0/value
shl eax,8 ; eax=0/0/value/0

add eax,ecx ; eax=0/0val/val

mov ecx,eax ; ecx=0/0/val/val

shl eax,10h ; eax=val/val/0/0

add eax,ecx ; eax = all 4 bytes = [value]

; Set dword-sized blocks
mov ecx,edx ; move original count to ecx
and edx,3 ; prepare in edx byte count (for tail loop)
shr ecx,2 ; adjust ecx to be dword count
jz tail ; jump if it was less then 4 bytes

rep stosd
main_loop_tail:
test edx,edx ; if there is no tail bytes,
jz finish ; we finish, and it's time to leave
; Set remaining bytes

tail:
mov [edi],al ; set remaining bytes
add edi,1

sub edx,1 ; if there is some more bytes
jnz tail ; continue to fill them

; Done
finish:
mov eax,[esp + 8] ; return dest pointer
pop edi ; restore edi

ret

toend:
mov eax,[esp + 4] ; return dest pointer

ret

memset endp

end
wingrez 2018-08-21
  • 打赏
  • 举报
回复
感谢各位,我会注意到 内存泄露 这个问题的。
真相重于对错 2018-08-21
  • 打赏
  • 举报
回复
A* tmp = new A;
你如果用memset(tmp ,0,sizeof(A)) ,你将会失去释放a的过程。
所以必须 delete tmp
同理vector ,vector里面很大可能放一些 对象,当vector clear的时候将会调用容器内部对象析构函数,memset没有这一个过程。很可能就会内存泄露
真相重于对错 2018-08-21
  • 打赏
  • 举报
回复
引用 10 楼 wingrez 的回复:
[quote=引用 9 楼 hdt 的回复:]
不对.既然用stl,不要用c的方法管理内存,而要用c++的


我是觉得memset()比clear()更快。[/quote]
比如下面的代码
class A{
int* a;
public:
A(){
a = new int[10];
}
~A(){
delete[] a;
}
}
待续_1006 2018-08-21
  • 打赏
  • 举报
回复
明明给你实现了clear而且也经历 了时间了考验,为什么非要重复造轮子?
真相重于对错 2018-08-21
  • 打赏
  • 举报
回复
引用 10 楼 wingrez 的回复:
[quote=引用 9 楼 hdt 的回复:]
不对.既然用stl,不要用c的方法管理内存,而要用c++的


我是觉得memset()比clear()更快。[/quote]
难道为了快,就不怕出错么?大跃进怎么来的?
zlxi 2018-08-21
  • 打赏
  • 举报
回复
c++ 用c++ 初始化方法,如果menset 会将vector对象属性清零,导致内存泄漏
qq_41500712 2018-08-21
  • 打赏
  • 举报
回复
《The Intel 64 and IA-32 Architectures Software Developer's Manual》
  • 打赏
  • 举报
回复
可以的。
vector内存 是连续的。
用memset(num.data(),0,sizeof(int)*num.size());
清0,这时,还是100个元素。

你的方法,会破坏vector的管理区,可能会造成内存泄漏。
半雨微凉丶 2018-08-20
  • 打赏
  • 举报
回复
可以啊,还不用遍历
wingrez 2018-08-20
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!


哇,赵老师真是“活跃”啊,这句话您说过好多遍了,不过颇有几分道理!
我还有很长的路要走,谨记~
但是当前这种情况,您是否能给出直接一点的答案,让我参考一下呢?
赵4老师 2018-08-20
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。
任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
wingrez 2018-08-20
  • 打赏
  • 举报
回复
我测试了一下,往num中添加了几个元素,之后用memset清0,元素被清空0,empty true
wingrez 2018-08-20
  • 打赏
  • 举报
回复
引用 9 楼 hdt 的回复:
不对.既然用stl,不要用c的方法管理内存,而要用c++的


我是觉得memset()比clear()更快。
加载更多回复(4)

64,677

社区成员

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

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