Cpp 2 PB translation but understanding

Just starting out? Need help? Post your questions and find answers here.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Cpp 2 PB translation but understanding

Post 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
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post 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
Last edited by Olliv on Wed May 10, 2017 9:45 am, edited 2 times in total.
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Cpp 2 PB translation but understanding

Post by LiK137 »

Olliv, Huge thanks.

But how :))) is it possible to know this???
I'd like to understand if You don't mind...
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post by Olliv »

Wait a lil bit : I add '*' characters comment line to watch better the macro...
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post 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!
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Cpp 2 PB translation but understanding

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post by Olliv »

Did you find my mistake here?

Code: Select all

stat = dbFind_(*tags\n[0] )
('*' character was away)
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post 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)
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Cpp 2 PB translation but understanding

Post 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] )
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post 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?
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Cpp 2 PB translation but understanding

Post 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
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post 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
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Cpp 2 PB translation but understanding

Post 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.
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Cpp 2 PB translation but understanding

Post 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)
LiK137
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Jun 23, 2010 5:13 pm

Re: Cpp 2 PB translation but understanding

Post by LiK137 »

Tested, if You don't mind I'd post code
Post Reply