Author Topic: C style background  (Read 1987 times)

0 Members and 1 Guest are viewing this topic.

Emil_halim

  • Guest
C style background
« on: February 01, 2014, 10:18:58 AM »
Hi Charles,

this code compiled okay , but crashed when run it.
Code: [Select]
 includepath "$\inc\"
  $filename "test1.exe"
  #include "RTL32.inc"
  #include     "console.inc"
  
  includepath ""
  #include once "msvcrt.inc"
  
struct point
{
   int x
   int y
}

function GetMEM(long x) as void*
  return malloc(x)
end function

int main()
{

    point* pt;
    
    pt = GetMEM(sizeof point)

    printf( "%d\n" , pt.x )
    
    waitkey
}

main  

1- what wrong with this code ?
2- when compiling zilb there was "->"  ,[c operator], and oxygen compiled it without error.
    when replace pt.x in my example with pt->x  oxygen give error, why?

thanks for help.  

edited:
   is there a c++ style operator  "new" so

   
Code: [Select]
            pt = GetMEM(sizeof point)
           
            will be

             pt =  new  point

   
« Last Edit: February 01, 2014, 10:30:33 AM by Emil_halim »

Charles Pegge

  • Guest
Re: C style background
« Reply #1 on: February 01, 2014, 03:11:41 PM »
Hi Emil,

msvcrt.inc ?

Currently, OxygenBasic does not support arrow notation, and generally does not use explicit pointers in the main body of code. Also, array variables in C ire treated as pointers, but not in O2.

This is how to explicitly allocate memory to a pointer variable, and to free it when finished.
Code: [Select]
includepath "$\inc\"
  $filename "test1.exe"
  #include "RTL32.inc"
  #include     "console.inc"
  
  includepath ""
  
struct point
{
   int x
   int y
}

int main()
{
    point *pt
    &pt=getmemory sizeof point
    print pt.x cr    
    waitkey
    freememory &pt
}

main

Using Let syntax:
Code: [Select]
int main()
{
    let pt=new point
    print pt.x cr   
    waitkey
    freememory @pt
}
« Last Edit: February 01, 2014, 03:24:42 PM by Charles Pegge »

Emil_halim

  • Guest
Re: C style background
« Reply #2 on: February 02, 2014, 07:25:49 AM »
Hi Charles,

msvcrt.inc  is include file for msvcrt.dll library i got it from old oxygenbasic , so i was test it with O2.
Code: [Select]
/* MSVCRT FOR OXYGENBASIC BY PETER WIRBELAUER 12.o8.2o12  */

sys msv
msv= LoadLibrary "msvcrt.dll"

Bind msv       
(
  abort       abort
  abs         abs
  acos        acos
  asctime     asctime
  asin        asin
  atan        atan
  atan2       atan2
  atexit      atexit
   ....            .....
   ....            ...etc....
         


Quote
and generally does not use explicit pointers in the main body of code.
why not , is there a technical reason ?

Quote
  Also, array variables in C ire treated as pointers, but not in O2.
may you can add this feature later , when you have a spare time.

does Let keyword  support array declaration with the new operator?
i mean that
Code: [Select]
   Lest  pt = new point[10]   // allocates sizeof point structure * 10
 

thanks for help.

Charles Pegge

  • Guest
Re: C style background
« Reply #3 on: February 03, 2014, 04:28:32 AM »
Hi Emil,

I am thinking about supporting C-pointers and C array conventions. But these cannot be reconciled directly with OxygenBasic's conventions, where pointers and references are generally abstracted out. Therefore we would have to switch into a different mode of syntax, supporting the full C language core.

There is a new release in the pipeline supporting dynamic arrays in let statements. It can be used with primitive variables and flat types, (but not strings). I need a bit more time to test it.