Array and Pointer...

Just starting out? Need help? Post your questions and find answers here.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Array and Pointer...

Post by Flype »

when working with DLL and we need to return an array of strings, one the best solution can be something like this :
(inspired by the post here : http://www.purebasic.fr/english/viewtopic.php?t=27675)

DLL CODE :

Code: Select all

#MAX_ENV = 100

ProcedureDLL.l GetEnv(*n.Long) 
  
  Static Dim env.s(#MAX_ENV)
  
  If ExamineEnvironmentVariables()
    
    *n\l = 0
    
    While (*n\l < #MAX_ENV) And NextEnvironmentVariable()
      env(*n\l) = EnvironmentVariableName() + " = " + EnvironmentVariableValue()
      *n\l + 1
    Wend
    
    ProcedureReturn @env()
    
  EndIf
  
EndProcedure
CODE TEST :

Code: Select all

If OpenLibrary(0, "env.dll")
  
  *env = CallFunction(0, "GetEnv", @n)
  
  If *env 
    
    Dim env.s(n) 
    
    env() = *env
    
    For i = 0 To n - 1
      Debug env(i)
    Next
    
  EndIf
  
  CloseLibrary(0) ; corrected
  
EndIf
here, everythings ok.


my question is (from the same DLL code) :

is the code below legal ? it works but is it really futur proof ?

Code: Select all

Structure TABLE
  s.s[0]
EndStructure

If OpenLibrary(0, "env.dll")
  
  *env.TABLE = CallFunction(0, "GetEnv", @n)
  
  If *env
    
    For i = 0 To n - 1
      
      Debug *env\s[i]
      
    Next
    
  EndIf
  
  CloseLibrary(0)
  
EndIf
Last edited by Flype on Thu Jun 21, 2007 8:01 am, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Not sure but it seems related to this:
http://www.purebasic.fr/english/viewtopic.php?t=17120
So it is not safe at least you allocate the needed memory amount.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

but what if the memory is already allocated by using in the dll : 'Static Dim' :?: it seems ok.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Oh! sorry, what a sillyness y wrote.
I said nothing :!: :oops:

Yes, i think it is safe.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@Flype,

Running PB 4.10 B2 under Windows ME, the SECOND test code works perfectly, but the FIRST test, after outputting the results correctly, goes into a infinite loop with the last line (EndIf) repeatedly generating the error "Invalid memory access". This still happens even after adding the missing CloseLibrary(0) statement.
Anthony Jordan
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

@akj
:shock:

freak, fred ?
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

should i put it in the bug section ?

as i can't see where this is the fault of the final programmer...
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Code: Select all

    env() = *env 
This kind of assignment is not supported in PB.

You can run into trouble with the automatic string cleanup inside arrays etc, which is probably the case here.
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

So then the 2nd example is safe, but the 1st one is not.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You should consider the string array to be readonly though.
Because the Dll and main program will use different heaps for their string allocation,
modifying the strings from the main program could be trouble.
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Different heaps?
Isn't supposed that dll and exe point to the same address to get and/or set data on it?
EDIT:
freak wrote:the Dll and main program will use different heaps for their string allocation.
This test shows the heap is the same:

Code: Select all

;DLL:
ProcedureDLL AttachProcess(instance)
 Global Dim Array.s(5)
EndProcedure
ProcedureDLL.l CreateArray()
  For i=0 To 5
    Array(i)="Line "+Str(i)
  Next
                MessageRequester("dll point to:",Hex(@Array()))
  ProcedureReturn @Array()
EndProcedure

Code: Select all

;EXE:
Structure array
  item.s[0]
EndStructure
If OpenLibrary(0,"dll_test.dll")
  *var.array=CallFunction(0,"CreateArray")
                MessageRequester("exe point to:",Hex(*var))
  For i=0 To 5
    MessageRequester("hi",*var.array\item[i])
  Next
  CloseLibrary(0)
EndIf
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

@freak, and all.

thanks a lot for clarifying this behaviour.


i have still a short one :

why 'env() = *env' is compiled without error if this is forbidden ?
what's the use... it's pretty dangerous to let guys like us to allow playing forbidden games :D
a debugger warning, or compiler error might be added.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

hmmm, maybe i missed one thing.

so i understood, and it's ok : env() = *env is not legal between pb-dll<-->pb-prog

but is it ok inside a standalone pb-prog (no dll stuff) ?


no legal in all case ?


(i'm wondering because 'it works' like a charm on the provided sample and on winxp).
Last edited by Flype on Thu Jun 21, 2007 8:25 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Psychophanta wrote:Different heaps?
Isn't supposed that dll and exe point to the same address to get and/or set data on it?
Sure, but heaps in Windows are created with HeapCreate() and different heaps are created in a shared memory space for the exe and dll.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Flype wrote:@freak, and all.

thanks a lot for clarifying this behaviour.


i have still a short one :

why 'env() = *env' is compiled without error if this is forbidden ?
what's the use... it's pretty dangerous to let guys like us to allow playing forbidden games :D
a debugger warning, or compiler error might be added.
It's appears to be legal but it surely will cause you tons of problems.

Edit: Like, in real life it's not illegal to cut off your arm, but often you'll find yourself lacking an arm afterwards.
Post Reply