Page 1 of 4

Cpp 2 PB translation but understanding

Posted: Tue May 09, 2017 10:54 pm
by LiK137
hi,
is it possible to explain the way how below given CPP code could be interpreted to PureBasic.
The dark side of code not being able to be digested is feeding array to function part.

Code: Select all

long long pitch[64];
dNoise(hAudio, pitch, sizeof(pitch));
char tags[16];
hGuid((hAudio, pitch[i], tags, sizeof(tags));
if (strlen(tags) > 0) {
  int stat = dbFind(tags[1])
  return (INT_PTR)TRUE;
  }

Big Thanx

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 8:02 am
by Olliv
Don't know procedure original marks.
Don't know functions root (suppose external)
(considering 1st array element is zero)
A try :

Code: Select all

;***************************************************************************************************************************************************************************************
Macro _ (StructureName, FieldType, FieldQuantity)

 Structure StructureName#MacroExpandedCount
  n.FieldType[FieldQuantity]
 EndStructure
 Define *StructureName.StructureName#MacroExpandedCount = AllocateMemory(SizeOf(StructureName#MacroExpandedCount) )
EndMacro



Procedure.Q UnknownProcedure(hAudio, i)

 _ (pitch, q, 64)
 dNoise_(hAudio, *pitch, MemorySize(*pitch) )
 _ (tags, b, 16)
 hGuid_(hAudio, *pitch\n[i], *tags, MemorySize(*tags) )
 If *tags\n[0]
  stat = dbFind_(tags\n[0] )
  ProcedureReturn #True
 EndIf

EndProcedure

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 9:25 am
by LiK137
Olliv, Huge thanks.

But how :))) is it possible to know this???
I'd like to understand if You don't mind...

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 9:44 am
by Olliv
Wait a lil bit : I add '*' characters comment line to watch better the macro...

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 10:06 am
by Olliv
Now, I remove #MacroExpandedCount in order to simplify your reading.

#MacroExpandedCount is a compiler directive which is automatically replaced by a compiler internal counter number (#1, then #2, then #3, etc...).

Its goal : just allow you to invoke this macro more than one time.

Below, _Quid_ macro will crash if you invoke it two times or more in a program. But for telling here, this is not a problem, until it s right just one time to test it.

Code: Select all

Macro _Quid_ (StructureName, FieldType, FieldQuantity)

 Structure StructureName
  n.FieldType[FieldQuantity]
 EndStructure

 Define *StructureName.StructureName = AllocateMemory(SizeOf(StructureName) )
EndMacro




_Quid_ (SomeOne, Q, 2) ; just one test
This will be treated like that :

Code: Select all

Structure SomeOne 
  n.Q[2]
EndStructure

Define *SomeOne.SomeOne = AllocateMemory(SizeOf(SomeOne) )
That allow you here to get a memory buffer named "SomeOne" and divided in 2 quads, and allow you to access into the datas of this buffer, even easily.

Data access example :

Code: Select all

*SomeOne\n[0] = 9
Debug *SomeOne\n[0]
so!

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 12:54 pm
by LiK137
ThanQ very much.

I changed with Your new code but in the lines where I use this there is a vsdebugger assertion failed debugger.
I tried to remove jitdebugger from registry but VS catches and throws dialog on every loop on the line.

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 2:31 pm
by Olliv
Did you find my mistake here?

Code: Select all

stat = dbFind_(*tags\n[0] )
('*' character was away)

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 2:59 pm
by Olliv
I doubt about this :

Code: Select all

int stat = dbFind(tags[1])
It is strange dbFind() function needs only a single value. I would suggest this function needs an address. If it is this way, right syntax should be:

Code: Select all

Stat = dbFind_(*tags)

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 3:07 pm
by LiK137
Yes, I put * not because I found Your mistake. It just throw error giving me hint :)
My question is should I use "_" as You mentioned

Code: Select all

stat = dbFind_(*tags\n[0] )

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 3:20 pm
by Olliv
I do not know the root of these functions : are they stored in a DLL file? a OBJ file? a LIB file? Or... an UFO file?

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 3:21 pm
by LiK137
Moreover, I have uninstalled VS2017
I returned system to snapshot.
I have removed default system debugger but this Assertion failure follows me.

Before Your solution the program's this part was not working at all.
With Your help it works but with this assertion on the line where I use IDs

Code: Select all

_ (pitch, q, 64)  ; Just After this line Assertion raises
 dNoise(hAudio, *pitch, MemorySize(*pitch) ) ; with underscore it throws error

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 3:32 pm
by Olliv
myFunction_() it's for LIB and OBJ files

If it is in a DLL files, there is two solutions :

1) Allows you to search, open and execute a DLL function
http://www.purebasic.com/documentation/ ... index.html

2) Allows you to execute a LIB, OBJ function, but you must know all characteristics of your function
http://www.purebasic.com/documentation/ ... mport.html

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 3:39 pm
by LiK137
ThanQ,
This is dll and with nearly 30 working functions.
I am using all except 3 functions which are using ***pointers and *arrays as arguments.
And now function with ***pointer is returning correct value.
the remainin 2 functions use *array which is also return correct data but with the exception of assertion.

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 4:12 pm
by Olliv
As directly as there is a smooth translating between c and pb (line after line), we must prepare the compiler to access to external functions.

If from pb scope, you can observe such these details and good execution of most of functions, so you know how to prepare them (the 2 help links just above).

What more on pb? Not a lot.
The address of a variable :

Code: Select all

@variable
The address of a cell created by the head macro :

Code: Select all

@*cell\[numero]
The value of such a cell :

Code: Select all

*cell\[numero]
The address of the array of cells :

Code: Select all

*cell
The size (in byte) of this array :

Code: Select all

MemorySize(*cell)
A detail which is not notified here : in my suggesting, I allocate memory manually for every array to use.

Here, we have *pitch and *args.
I do not free this memory. So, it is important to know where you can free this memory, especially where the infos of these arrays are useless, to insert :

Code: Select all

FreeMemory(*pitch)
FreeMemory(*args)

Re: Cpp 2 PB translation but understanding

Posted: Wed May 10, 2017 5:11 pm
by LiK137
Tested, if You don't mind I'd post code