www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is D's GC.calloc and C's memset played the same role?

reply "FrankLike" <1150015857 qq.com> writes:
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 ?!
Dec 23 2014
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Dec 23 2014
prev sibling next sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 23 Dec 2014 15:37:12 +0000
FrankLike via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 you will find the different:
   D: PROCESSENTRY32* pe32 =3D=20
 cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof);
=20
 C++:PROCESSENTRY32 pe32;
=20
 GC.calloc means: memset ?!
do you see that shining star there? here it is, right in the end: `PROCESSENTRY32*`. and do you see that same star in C sample? jokes aside, it's dead simple: C code using stack-allocated struct (`PROCESSENTRY32` without an inderection) and D code using heap-allocated struct (`PROCESSENTRY32*` with indirection). hence C code using `memset()`, yet D code using `GC.calloc()`.
Dec 23 2014
parent reply "FrankLike" <1150015857 qq.com> writes:
On Tuesday, 23 December 2014 at 20:22:12 UTC, ketmar via 
Digitalmars-d-learn wrote:
 On Tue, 23 Dec 2014 15:37:12 +0000
 FrankLike via Digitalmars-d-learn 
 <digitalmars-d-learn puremagic.com>
 wrote:

 you will find the different:
   D: PROCESSENTRY32* pe32 = 
 cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof);
 
 C++:PROCESSENTRY32 pe32;
 
 GC.calloc means: memset ?!
do you see that shining star there? here it is, right in the end: `PROCESSENTRY32*`. and do you see that same star in C sample?
Yes,if you not do like it,it will not work.
 jokes aside, it's dead simple: C code using stack-allocated
Not joke.it works fine,you can run it. Not C,it's C++.
 struct
 (`PROCESSENTRY32` without an inderection) and D code using
 heap-allocated struct (`PROCESSENTRY32*` with indirection).

 hence C code using `memset()`, yet D code using `GC.calloc()`.
Dec 23 2014
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Wed, 24 Dec 2014 00:24:44 +0000
FrankLike via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 On Tuesday, 23 December 2014 at 20:22:12 UTC, ketmar via=20
 Digitalmars-d-learn wrote:
 On Tue, 23 Dec 2014 15:37:12 +0000
 FrankLike via Digitalmars-d-learn=20
 <digitalmars-d-learn puremagic.com>
 wrote:

 you will find the different:
   D: PROCESSENTRY32* pe32 =3D=20
 cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof);
=20
 C++:PROCESSENTRY32 pe32;
=20
 GC.calloc means: memset ?!
do you see that shining star there? here it is, right in the=20 end: `PROCESSENTRY32*`. and do you see that same star in C sample?
=20 Yes,if you not do like it,it will not work. =20
 jokes aside, it's dead simple: C code using stack-allocated
=20 Not joke.it works fine,you can run it. Not C,it's C++.
 struct
 (`PROCESSENTRY32` without an inderection) and D code using
 heap-allocated struct (`PROCESSENTRY32*` with indirection).

 hence C code using `memset()`, yet D code using `GC.calloc()`.
you did quoted the relevant part. let me repeat it: C code using stack-allocated struct (`PROCESSENTRY32` without an inderection) and D code using heap-allocated struct (`PROCESSENTRY32*` with indirection). hence C code using `memset()`, yet D code using `GC.calloc()`. i.e. D code using *pointer* *to* *struct*, so you must allocate it manually.
Dec 23 2014
prev sibling parent reply "JN" <666total wp.pl> writes:
I know this is probably a theoretical exercise, but easier way 
might be to execute "tasklist" and just grep the output.
Dec 24 2014
parent "FrankLike" <1150015857 qq.com> writes:
On Wednesday, 24 December 2014 at 12:53:06 UTC, JN wrote:
 I know this is probably a theoretical exercise, but easier way 
 might be to execute "tasklist" and just grep the output.
study D,you should do some exercises.
Dec 25 2014