AllocateMemory

Just starting out? Need help? Post your questions and find answers here.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

AllocateMemory

Post by J. Baker »

I see AllocateMemory has changed. How do we use length or should I say Lof now?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
GedB
Addict
Addict
Posts: 1313
Joined: Fri May 16, 2003 3:47 pm
Location: England
Contact:

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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).
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Yeah but you can't do this anymore: AllocateMemory(0, len, 0)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
Last edited by PB on Mon Jul 19, 2004 7:00 am, edited 1 time in total.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Thanks alot, that had me stumped for a couple of hours. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Stefke28
User
User
Posts: 11
Joined: Thu Jul 15, 2004 9:06 am
Location: BELGIUM

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. :)
Stefke28
User
User
Posts: 11
Joined: Thu Jul 15, 2004 9:06 am
Location: BELGIUM

The new function of the old function UseMemory

Post 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
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: The new function of the old function UseMemory

Post 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.
deadmoap
User
User
Posts: 79
Joined: Sun Feb 22, 2004 11:45 pm
Location: Riverdale, Utah
Contact:

Post 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.
Post Reply