• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Bc9 Info

Started by James C. Fuller, January 14, 2013, 03:53:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James C. Fuller

Fred,
  I cant remember hot to zero a type in PB myself! TYPE SET ??

As for the zeroing in c I had to use the zero int b={0};. So I tried it with the zero for char arrays and struct's and it worked fine.
I needed a generic implementation as the bc9 source can be a bit intimidating.

James

Patrice Terrier

QuoteThat seems like an awful lot of overhead for what gains?.
No it is not, and this kind of STATIC is very useful when you need to share some kind of GLOBAL between an EXE and a DLL.

That works the same than method properties.

...
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Frederick J. Harris

Just for your info James, I was still curious about this sort of construct ...

int b={0};

as I never saw anything like that until fairly recently.  I'm wondering if its some relatively late modification of the C/C++ standards.  To try a little experiment, I tried to compile the following little program using VC6 (circa 1998 or so) as a C program, and it doesn't compile.  It gets stuck on those {} brackets on lines 13, 14.  Here is the program ...


#include <cstdio>
#define BYTES 16

struct Etwas
{
int    a;
int    b;
double c;
};

int main()
{
struct Etwas obj={};
char szBytes[BYTES]={};

printf("obj.a=%d\n",obj.a);
printf("obj.b=%d\n",obj.b);
printf("obj.c=%f\n\n",obj.c);
for(int i=0; i<BYTES; i++)
     printf("%d\t%c\n",i,szBytes[i]);
getchar();

return 0;
}


The above works just fine as a C++ program with the latest Mingw from CodeBlocks. 

Frederick J. Harris

FYI, with old VC6 compiling as C you can't have empty brackets, i.e., {};  You need the zero in the middle (  {0};  ).

Also, something else I was wrong about; C/C++ does do some initializations.  Apparently global/static constructs are initialized to zero.  I'd have to test that, but its what I read if I got it right.

James C. Fuller

Quote from: Frederick J. Harris on January 22, 2013, 09:53:23 PM
FYI, with old VC6 compiling as C you can't have empty brackets, i.e., {};  You need the zero in the middle (  {0};  ).

Also, something else I was wrong about; C/C++ does do some initializations.  Apparently global/static constructs are initialized to zero.  I'd have to test that, but its what I read if I got it right.

Fred,
  This link in an earlier post states that and more: http://gynvael.coldwind.pl/?id=406

James

James C. Fuller

Fred,
  Here is the c++ test code I used to verify for 32bit.
The g++ fromMinGWTDM64 must initialize the stack up to a point as everythinhg is zero until byte 34 of the str1 char array.
With VC++ from sdk 7.1 everything has a value. It did warn with "uninitialized local variable used".

James


With no intialization

// *********************************************************************
// Created with bc9 - BASIC To C/C++ Translator (V) 9.1.0.6 (2013/1/22)
//                 BCX (c) 1999 - 2009 by Kevin Diggins
// *********************************************************************
//              Translated for compiling with a C++ Compiler
//                           On MS Windows
// *********************************************************************
#include <process.h>    // dos
#include <conio.h>      // dos
#include <direct.h>     // dos
#include <io.h>         // dos
#include <ctype.h>      // dos/linux
#include <fcntl.h>      // dos/linux
#include <math.h>       // dos/linux
#include <stdio.h>      // dos/linux
#include <string.h>     // dos/linux
#include <stddef.h>     // dos/linux
#include <stdlib.h>     // dos/linux
#include <setjmp.h>     // dos/linux
#include <time.h>       // dos/linux
#include <stdarg.h>     // dos/linux
#include <sys/types.h> 
#include <sys/stat.h>   

// ***************************************************
// types from winapi that may be needed
// ***************************************************
typedef char *PCHAR,*LPCH,*PCH,*NPSTR,*LPSTR,*PSTR;
typedef unsigned int UINT;
typedef unsigned long DWORD,ULONG;
typedef unsigned char UCHAR,byte;
typedef void *PVOID,*LPVOID;
//----------------------------------------------------
// this is missing From MinGWTDM64 stdlib.h
//----------------------------------------------------
#if defined(__MINGW32__) || defined(__MINGW64__)
  #ifndef MAX_PATH
    #define MAX_PATH 260
  #endif
#endif
//----------------------------------------------------

#ifndef __cplusplus
  #error A C++ compiler is required
#endif
#include <windows.h>

// *************************************************
//        User's GLOBAL ENUM blocks
// *************************************************

// *************************************************
//            System Defined Constants
// *************************************************

#define CCPTR const char*
#define cSizeOfDefaultString 2048

// *************************************************
//            User Defined Constants
// *************************************************

#define QWERTY_CLASS struct _QWERTY*

// *************************************************
//               Standard Prototypes
// *************************************************

void    Pause (void);
// *************************************************
//          User Defined Types And Unions
// *************************************************


typedef struct _QWERTY
{
  int     a;
  float   b;
  char     c[80];
  RECT     q;
}QWERTY, *LPQWERTY;


// *************************************************
//            User Global Variables
// *************************************************

static PCHAR   *g_argv;
static int     g_argc;


// *************************************************
//               User Prototypes
// *************************************************

void    TestIt (void);

// *************************************************
//            User Global Initialized Arrays
// *************************************************



// *************************************************
//                 Runtime Functions
// *************************************************

void Pause(void)
{
  printf("\n%s\n","Press any key to continue . . .");
  _getch();
}



// ************************************
//       User Subs and Functions
// ************************************

void TestIt ()
{
  int      a;
  int      b;
  int      c;
  int      d;
  int      e;
  int      f;
  int      g;
  int      h;
  int      i;
  int      j;
  int      k;
  int      l;
  int      m;
  int      n;
  int      o;
  int      p;
  float    s1;
  float    s2;
  float    s3;
  float    s4;
  float    s5;
  float    s6;
  float    s7;
  float    s8;
  float    s9;
  float    s10;
  double   d1;
  double   d2;
  double   d3;
  double   d4;
  double   d5;
  double   d6;
  double   d7;
  double   d8;
  double   d9;
  double   d10;
  char     str1[2000];
  struct _QWERTY   qq;
  printf("%s% d\n","a = ",(int)a);
  printf("%s% d\n","b = ",(int)b);
  printf("%s% d\n","c = ",(int)c);
  printf("%s% d\n","d = ",(int)d);
  printf("%s% d\n","e = ",(int)e);
  printf("%s% d\n","f = ",(int)f);
  printf("%s% d\n","g = ",(int)g);
  printf("%s% d\n","h = ",(int)h);
  printf("%s% d\n","i = ",(int)i);
  printf("%s% d\n","j = ",(int)j);
  printf("%s% d\n","k = ",(int)k);
  printf("%s% d\n","l = ",(int)l);
  printf("%s% d\n","m = ",(int)m);
  printf("%s% d\n","n = ",(int)n);
  printf("%s% d\n","o = ",(int)o);
  printf("%s% d\n","p = ",(int)p);
  printf("%s% .7G\n","s1 = ",(float)s1);
  printf("%s% .7G\n","s2 = ",(float)s2);
  printf("%s% .7G\n","s3 = ",(float)s3);
  printf("%s% .7G\n","s4 = ",(float)s4);
  printf("%s% .7G\n","s5 = ",(float)s5);
  printf("%s% .7G\n","s6 = ",(float)s6);
  printf("%s% .7G\n","s7 = ",(float)s7);
  printf("%s% .7G\n","s8 = ",(float)s8);
  printf("%s% .7G\n","s9 = ",(float)s9);
  printf("%s% .7G\n","s10 = ",(float)s10);
  printf("%s% .15G\n","d1 = ",(double)d1);
  printf("%s% .15G\n","d2 = ",(double)d2);
  printf("%s% .15G\n","d3 = ",(double)d3);
  printf("%s% .15G\n","d4 = ",(double)d4);
  printf("%s% .15G\n","d5 = ",(double)d5);
  printf("%s% .15G\n","d6 = ",(double)d6);
  printf("%s% .15G\n","d7 = ",(double)d7);
  printf("%s% .15G\n","d8 = ",(double)d8);
  printf("%s% .15G\n","d9 = ",(double)d9);
  printf("%s% .15G\n","d10 = ",(double)d10);
  for(a=0; a<=1999; a+=1)
    {
      if(str1[a]!=0 )
        {
          printf("%s% d\n","No zero at ->",(int)a);
          goto L1000;
        }
    }
L1000:;
  printf("% d\n",(int)qq.a);
  printf("% d\n",(int)qq.b);
  printf("%s\n",qq.c);
  printf("% d\n",(int)qq.q.left);
}


// *************************************************
//                  Main Program
// *************************************************

int main(int argc, char *argv[])
{
  g_argc = argc;
  g_argv = argv;
TestIt();
Pause();
  return 0;   /* End of main program */
}




With all set to ={0}


// *********************************************************************
// Created with bc9 - BASIC To C/C++ Translator (V) 9.1.0.6 (2013/1/22)
//                 BCX (c) 1999 - 2009 by Kevin Diggins
// *********************************************************************
//              Translated for compiling with a C++ Compiler
//                           On MS Windows
// *********************************************************************
#include <process.h>    // dos
#include <conio.h>      // dos
#include <direct.h>     // dos
#include <io.h>         // dos
#include <ctype.h>      // dos/linux
#include <fcntl.h>      // dos/linux
#include <math.h>       // dos/linux
#include <stdio.h>      // dos/linux
#include <string.h>     // dos/linux
#include <stddef.h>     // dos/linux
#include <stdlib.h>     // dos/linux
#include <setjmp.h>     // dos/linux
#include <time.h>       // dos/linux
#include <stdarg.h>     // dos/linux
#include <sys/types.h> 
#include <sys/stat.h>   

// ***************************************************
// types from winapi that may be needed
// ***************************************************
typedef char *PCHAR,*LPCH,*PCH,*NPSTR,*LPSTR,*PSTR;
typedef unsigned int UINT;
typedef unsigned long DWORD,ULONG;
typedef unsigned char UCHAR,byte;
typedef void *PVOID,*LPVOID;
//----------------------------------------------------
// this is missing From MinGWTDM64 stdlib.h
//----------------------------------------------------
#if defined(__MINGW32__) || defined(__MINGW64__)
  #ifndef MAX_PATH
    #define MAX_PATH 260
  #endif
#endif
//----------------------------------------------------

#ifndef __cplusplus
  #error A C++ compiler is required
#endif
#include <windows.h>

// *************************************************
//        User's GLOBAL ENUM blocks
// *************************************************

// *************************************************
//            System Defined Constants
// *************************************************

#define CCPTR const char*
#define cSizeOfDefaultString 2048

// *************************************************
//            User Defined Constants
// *************************************************

#define QWERTY_CLASS struct _QWERTY*

// *************************************************
//               Standard Prototypes
// *************************************************

void    Pause (void);
// *************************************************
//          User Defined Types And Unions
// *************************************************


typedef struct _QWERTY
{
  int     a;
  float   b;
  char     c[80];
  RECT     q;
}QWERTY, *LPQWERTY;


// *************************************************
//            User Global Variables
// *************************************************

static PCHAR   *g_argv;
static int     g_argc;


// *************************************************
//               User Prototypes
// *************************************************

void    TestIt (void);

// *************************************************
//            User Global Initialized Arrays
// *************************************************



// *************************************************
//                 Runtime Functions
// *************************************************

void Pause(void)
{
  printf("\n%s\n","Press any key to continue . . .");
  _getch();
}



// ************************************
//       User Subs and Functions
// ************************************

void TestIt ()
{
  int      a={0};
  int      b={0};
  int      c={0};
  int      d={0};
  int      e={0};
  int      f={0};
  int      g={0};
  int      h={0};
  int      i={0};
  int      j={0};
  int      k={0};
  int      l={0};
  int      m={0};
  int      n={0};
  int      o={0};
  int      p={0};
  float    s1={0};
  float    s2={0};
  float    s3={0};
  float    s4={0};
  float    s5={0};
  float    s6={0};
  float    s7={0};
  float    s8={0};
  float    s9={0};
  float    s10={0};
  double   d1={0};
  double   d2={0};
  double   d3={0};
  double   d4={0};
  double   d5={0};
  double   d6={0};
  double   d7={0};
  double   d8={0};
  double   d9={0};
  double   d10={0};
  char     str1[2000]={0};
  struct _QWERTY   qq={0};
  printf("%s% d\n","a = ",(int)a);
  printf("%s% d\n","b = ",(int)b);
  printf("%s% d\n","c = ",(int)c);
  printf("%s% d\n","d = ",(int)d);
  printf("%s% d\n","e = ",(int)e);
  printf("%s% d\n","f = ",(int)f);
  printf("%s% d\n","g = ",(int)g);
  printf("%s% d\n","h = ",(int)h);
  printf("%s% d\n","i = ",(int)i);
  printf("%s% d\n","j = ",(int)j);
  printf("%s% d\n","k = ",(int)k);
  printf("%s% d\n","l = ",(int)l);
  printf("%s% d\n","m = ",(int)m);
  printf("%s% d\n","n = ",(int)n);
  printf("%s% d\n","o = ",(int)o);
  printf("%s% d\n","p = ",(int)p);
  printf("%s% .7G\n","s1 = ",(float)s1);
  printf("%s% .7G\n","s2 = ",(float)s2);
  printf("%s% .7G\n","s3 = ",(float)s3);
  printf("%s% .7G\n","s4 = ",(float)s4);
  printf("%s% .7G\n","s5 = ",(float)s5);
  printf("%s% .7G\n","s6 = ",(float)s6);
  printf("%s% .7G\n","s7 = ",(float)s7);
  printf("%s% .7G\n","s8 = ",(float)s8);
  printf("%s% .7G\n","s9 = ",(float)s9);
  printf("%s% .7G\n","s10 = ",(float)s10);
  printf("%s% .15G\n","d1 = ",(double)d1);
  printf("%s% .15G\n","d2 = ",(double)d2);
  printf("%s% .15G\n","d3 = ",(double)d3);
  printf("%s% .15G\n","d4 = ",(double)d4);
  printf("%s% .15G\n","d5 = ",(double)d5);
  printf("%s% .15G\n","d6 = ",(double)d6);
  printf("%s% .15G\n","d7 = ",(double)d7);
  printf("%s% .15G\n","d8 = ",(double)d8);
  printf("%s% .15G\n","d9 = ",(double)d9);
  printf("%s% .15G\n","d10 = ",(double)d10);
  for(a=0; a<=1999; a+=1)
    {
      if(str1[a]!=0 )
        {
          printf("%s% d\n","No zero at ->",(int)a);
          goto L1000;
        }
    }
L1000:;
  printf("% d\n",(int)qq.a);
  printf("% d\n",(int)qq.b);
  printf("%s\n",qq.c);
  printf("% d\n",(int)qq.q.left);
}


// *************************************************
//                  Main Program
// *************************************************

int main(int argc, char *argv[])
{
  g_argc = argc;
  g_argv = argv;
TestIt();
Pause();
  return 0;   /* End of main program */
}



James C. Fuller

Here is the output from running the above demo.

result no initialization MinGW g++

a =  0
b =  0
c =  0
d =  0
e =  0
f =  0
g =  0
h =  0
i =  0
j =  0
k =  0
l =  0
m =  0
n =  0
o =  0
p =  0
s1 =  0
s2 =  0
s3 =  0
s4 =  0
s5 =  0
s6 =  0
s7 =  0
s8 =  0
s9 =  0
s10 =  0
d1 =  0
d2 =  0
d3 =  0
d4 =  0
d5 =  0
d6 =  0
d7 =  0
d8 =  0
d9 =  0
d10 =  0
No zero at -> 34
2684580
0
∟·(
0

Press any key to continue . . .



result no initialization VC++

a =  983054
b =  983054
c =  983054
d =  983054
e =  983054
f =  983054
g =  983054
h =  983054
i =  983054
j =  983054
k =  983054
l =  983054
m =  983054
n =  983054
o =  983054
p =  983054
s1 =  1.377552E-039
s2 =  1.377552E-039
s3 =  1.377552E-039
s4 =  1.377552E-039
s5 =  1.377552E-039
s6 =  1.377552E-039
s7 =  1.377552E-039
s8 =  1.377552E-039
s9 =  1.377552E-039
s10 =  1.377552E-039
d1 =  2.08603645031748E-308
d2 =  2.08603645031748E-308
d3 =  2.08603645031748E-308
d4 =  2.08603645031748E-308
d5 =  2.08603645031748E-308
d6 =  2.08603645031748E-308
d7 =  2.08603645031748E-308
d8 =  2.08603645031748E-308
d9 =  2.08603645031748E-308
d10 =  2.08603645031748E-308
No zero at -> 0
65568
0

2949164

Press any key to continue . . .


result with Initializtion from both VC++ and g++


a =  0
b =  0
c =  0
d =  0
e =  0
f =  0
g =  0
h =  0
i =  0
j =  0
k =  0
l =  0
m =  0
n =  0
o =  0
p =  0
s1 =  0
s2 =  0
s3 =  0
s4 =  0
s5 =  0
s6 =  0
s7 =  0
s8 =  0
s9 =  0
s10 =  0
d1 =  0
d2 =  0
d3 =  0
d4 =  0
d5 =  0
d6 =  0
d7 =  0
d8 =  0
d9 =  0
d10 =  0
0
0

0

Press any key to continue . . .



Frederick J. Harris

You know what?  Maybe we ought to just stick to PowerBASIC?

I mean, with PowerBASIC, everything's initialized to zero.  That would include globals, locals, and statics.  With strings, they are initialized to empty strings.

Its consistent.  Consistency is GOOD. :)