Page 1 of 1
AllocateMemory
Posted: Mon Jul 19, 2004 2:01 am
by J. Baker
I see AllocateMemory has changed. How do we use length or should I say Lof now?
Posted: Mon Jul 19, 2004 6:43 am
by GedB
I'm not sure what you mean. The single paramater is Size, which is how many bytes you want.
The returned value is 0 for failure, or a pointer to the memory allocated.
You can pass this pointer to FreeMemory or Reallocate memory.
Posted: Mon Jul 19, 2004 6:53 am
by PB
You need to use the GlobalSize API command to do it, like so:
Code: Select all
a=Random(999)
b=AllocateMemory(a)
Debug a
Debug GlobalSize_(b)
However, I guess PureBasic should have a native command to do this...
(@J.Baker: Lof is used for file lengths only, and not for memory lengths).
Posted: Mon Jul 19, 2004 6:55 am
by J. Baker
Yeah but you can't do this anymore: AllocateMemory(0, len, 0)
Posted: Mon Jul 19, 2004 6:56 am
by PB
> Yeah but you can't do this anymore: AllocateMemory(0, len, 0)
Yes you can, like so:
adr=AllocateMemory(len)
You don't reference memory blocks with an ID number anymore, but by the
return value. So to free the memory block above:
FreeMemory(adr).
Show me what you're trying to do, and I'll show you the new way to do it.

Posted: Mon Jul 19, 2004 7:00 am
by J. Baker
Well, i've been playing with the installer code for some time now, learning from it. Here's a little snippet of what I mean.
If ReadFile(0, windir + "\nstall.bulk")
len = Lof()
AllocateMemory(0, len, 0)
*mem = MemoryID()
ReadData(*mem, len)
CloseFile(0)
EndIf
Posted: Mon Jul 19, 2004 7:02 am
by PB
> Here's a little snippet of what I mean
The new way to use that snippet is as follows:
Code: Select all
If ReadFile(0, windir + "\nstall.bulk")
len = Lof()
mem = AllocateMemory(len)
ReadData(mem, len)
CloseFile(0)
EndIf
Note that MemoryID() isn't used anymore, since the return value ("mem" in
this example) is the equivalent of MemoryID() and it's also the reference to
the memory block (ie. instead of using a memory number). This method is
better than before because it's far less typing and more practical.
Posted: Mon Jul 19, 2004 7:06 am
by J. Baker
Thanks alot, that had me stumped for a couple of hours.

Posted: Mon Aug 09, 2004 11:06 am
by Stefke28
Dear Developers
How can I this translated into one parameter (the new syntax) ?
Buffer=AllocateMemory(0,512,0)
memory=AllocateMemory(1,lang+1)
Explean me?
Thanks
Posted: Mon Aug 09, 2004 11:29 am
by PB
> Buffer=AllocateMemory(0,512,0)
This is now: Buffer=AllocateMemory(512)
"Buffer" now is the start of the memory block, and also the identifier for it,
instead of 0 being the identifier (as it was in the old version of the command).
> memory=AllocateMemory(1,lang+1)
This is now: memory=AllocateMemory(lang+1)
"memory" is now the start of the memory block, and also the identifier for it,
instead of 1 being the identifier (as it was in the old version of the command).
As I said for J.Baker: show me some code that you're having trouble with, and
I'll convert it to the new format for you, to show you how it's now done.

The new function of the old function UseMemory
Posted: Mon Aug 09, 2004 1:28 pm
by Stefke28
Dear Developer
Can somebody help me for translated or witch new function in PureBASIC 3.90 can I used instead of the old function
"*Mem0 = UseMemory(0)"
Thanks
Stefke
Posted: Mon Aug 09, 2004 3:05 pm
by thefool
@stefke28:
u dosent have 2 do that anymore. if you use more than 1 memory bank, you specify the pointer.
*MemoryID = AllocateMemory(Size)
if u have another memory bank, you use the same again, with another
*MemoryID.
then you dont have to use UseMemory anymore.
Re: The new function of the old function UseMemory
Posted: Tue Aug 10, 2004 12:38 am
by PB
> Can somebody help me
UseMemory isn't used anymore because you just access the memory block
directly now. I asked you to post some of your old code so that I can show
you the new way to do it. If you're not willing to do that, then I can't help.
Posted: Tue Aug 10, 2004 6:11 am
by deadmoap
The biggest downfall of PureBasic is the use of crappy ID numbers. I don't mean the actually handle to stuff, I mean the little parameter at the start of most functions where I am forced to use #PB_Any because I don't want to use the little kiddy ID number. It was a very BASIC idea, but I think the majority of PureBasic users aren't really beginners to programming. I use PureBasic for pretty much just a more effecient alternative to C++. It doesn't use such daunting syntax, yet still gives me all the power I need. Newbies to the programming world should go play with Blitz before knocking on PureBasic's door.
PureBasic puts ease into the GUI part of the API but adds stress with those ID numbers. Its 3D and 2D libraries aren't all that fascinating, either. They don't match my programming style. I use external calls to OpenGL and DirectX if I want to do some fast multimedia. Another thing I don't like is the prefix of constants. I'd much rather have something like "Const" to define constants versus "#". It's also a little annoying that PureBasic doesn't have all of the structures.
But PureBasic more than makes up for everything with it's small, fast executables, syntax that doesn't give a headache to newbies but is suitable for very advanced jobs, pointers and other excellent memory manipulation routines, and more. It was definately worth the money.