Reallocate memory

Everything else that doesn't fall into one of the other PB categories.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Reallocate memory

Post by Polo »

Hi !
First, I'm sorry to post all these memory topics :)
Well, I try to found a problem, and it's certainly because the ReallocateMemory function doesn't work, so I would like to know what can make this function doesn't work (when i say doesn't work, i mean it doesn't reallocate, it just stay the memory like it was)
thanks a lot !
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Polo,

Can you send a sample code (just a few lines) showing what case you found that does not work. From this we will make tests to confirm or tell you what's wrong ...

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

Unfortunately, I can't, the code is 1000 lines long, it works with Windows Xp but not with Windows 98... So it's really hard to see where it doesn't work, since it works on one OS, and since i don't get any error (it just doesn't reallocate on Windows 98)
I know it's quite hard to find a problem without sources...
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Polo,

Just I can't do more in this way. But using the debugger will tell you when things work and when do not ...

If you notice a well delimited bad or amazing behaviour, then maybe you can address your code to PureBasic's support for a deeper analysis or send a post here with a limited part of the code where you experience something like that.

Just if you can cut / paste some lines that make the same behaviour, then you will find better help here.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Soulfire
User
User
Posts: 23
Joined: Mon Mar 08, 2004 7:17 am

Post by Soulfire »

Using ReAllocateMemory() preserves the contents of the old memory space. It may seem that it is not working in several situations:

- You reallocate a large amount of memory to a smaller amount. Since PB preserves the memory's contents, the location of your data in the system's address space will likely not change. PB will simply free the memory that is no longer required by you. However, it will not alter the memory that it frees, and if no other program decides it needs to use that memory, your data will possibly stay in that address space indefinitely. So, you could theoretically read past the smaller size you allocated and still get your old data, but that data has actually been freed.

- You reallocate a small amount of memory to a larger amount. PB will not initialize the new bytes of memory, so they may have garbage data that you need to account for. Also, you might think that resizing some memory to be larger than it was before would mean that the address that the memory is stored in would have to be changed. While this is true in some cases, if you are increasing the memory size by only a few dozen(or possibly even hundreds) of bytes, chances are that the memory directly following the old smaller block of bytes was unclaimed by any application, and PB will simply attach that memory to your already allocated memory. This means that the starting address of your new, bigger data block will remain unchanged, giving the appearance that the reallocation failed. However, do not be alarmed, for it actually succeeded!

Hope this helps
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

If you reallocate to a larger block, there is no garbage added. The new
added memory will be initialized to 0.

And you should always expect a pointer change when reallocating memory.
(see the Help file for an example)

Timo
quidquid Latine dictum sit altum videtur
Soulfire
User
User
Posts: 23
Joined: Mon Mar 08, 2004 7:17 am

Post by Soulfire »

freak wrote:If you reallocate to a larger block, there is no garbage added. The new
added memory will be initialized to 0.
Sorry, my mistake
freak wrote:And you should always expect a pointer change when reallocating memory.
(see the Help file for an example)
Agreed, but the idea there was only that if the pointer doesn't change, it doesn't mean the reallocate failed.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Well, it doesn't appear to be a problem with reallocation under Win98 as this code works equally well under 98 and XP:

Code: Select all

*blah.l = AllocateMemory(1024*1024)
If *blah
    Debug "Allocated at 0x"+Hex(*blah)
    
    *blah2 = ReAllocateMemory(*blah, 4092*1024)
    If *blah2 = 0
        Debug "Reallocate failed"
    Else
        Debug "Reallocated at 0x"+Hex(*blah2)
        FreeMemory(*blah2)
    EndIf
    FreeMemory(*blah)
EndIf
End
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Post Reply