Is D's GC.calloc and C's memset played the same role?
Daniel Kozak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Dec 23 08:54:48 PST 2014
FrankLike via Digitalmars-d-learn píše v Út 23. 12. 2014 v 15:37 +0000:
> Today,I meet a question:get all processes names.
>
> ----------C++ CODE-----------------
> #include "stdafx.h"
> #include <windows.h>
> #include <stdio.h> //C standard I/O
> #include <tlhelp32.h>
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> HANDLE
> hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
>
> if(hProcessSnap==INVALID_HANDLE_VALUE)
> {
> _tprintf(_T("CreateToolhelp32Snapshot error!\n"));
> return -1;
> }
>
> PROCESSENTRY32 pe32;
> pe32.dwSize = sizeof(PROCESSENTRY32);
>
> BOOL bMore=Process32First(hProcessSnap,&pe32);
> int i=0;
>
> _tprintf(_T("PID\t thread nums \t name \n"));
>
> while(bMore)
> {
> bMore=Process32Next(hProcessSnap,&pe32);
> _tprintf(_T("%u\t"),pe32.th32ProcessID);
> _tprintf(_T("%u\t"),pe32.cntThreads);
> _tprintf(_T("%s\n"),pe32.szExeFile);
>
> i++;
> }
>
> CloseHandle(hProcessSnap);
> _tprintf(_T("Count:%d\n"),i);
>
> return 0;
> }
> ----------------D code--------------------------
> import std.stdio;
> import std.string;
> import core.sys.windows.windows;
> import core.memory;
> import win32.tlhelp32;
>
> void main()
> {
>
> HANDLE
> hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
>
> if(hProcessSnap is null)
> {
> writeln("CreateToolhelp32Snapshot error!\n");
> return ;
> }
>
> PROCESSENTRY32* pe32 =
> cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof);
>
> pe32.dwSize = PROCESSENTRY32.sizeof;
>
> bool bMore=cast(bool)Process32First(hProcessSnap,pe32);
> int i=0;
>
> writeln("PID\t thread nums\t name \n");
>
> while(bMore)
> {
> bMore=cast(bool)Process32Next(hProcessSnap,pe32);
> string s = cast(string)pe32.szExeFile;
> auto a = s.indexOf('\0');
> if(a >=0)
>
> writeln("\t",pe32.th32ProcessID,"\t",pe32.cntThreads,"\t",s[0..a]);
> i++;
> }
>
> CloseHandle(hProcessSnap);
> writeln(format("count:%d",i));
>
> return ;
> }
> -----------------------end----------------------
> you will find the different:
> D: PROCESSENTRY32* pe32 =
> cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof);
>
> C++:PROCESSENTRY32 pe32;
>
> GC.calloc means: memset ?!
calloc means alloc cleared memory same as malloc but clear all bits to
zero
More information about the Digitalmars-d-learn
mailing list