0

I used _expand to expand memory from 256KB to 512KB and it failed (returned NULL), but when I expand 512B to 1024B it works.

Is there replacement for _expand that works with bigger sizes?

DividedByZero
  • 421
  • 1
  • 5
  • 11
  • 3
    `_expand` is not limited. Not in that way, at least. The docs clearly state: "If there is insufficient memory available to expand the block to the given size without moving it, the function returns NULL." – R. Martinho Fernandes Oct 15 '12 at 09:58
  • Maybe it's not able to reserve that large of contiguous space? – M4rc Oct 15 '12 at 09:59
  • Nah I created pages\pools, I didn't allocated any memory after it. – DividedByZero Oct 15 '12 at 09:59
  • Hmmm. Odd. Why not use realloc() in it's place to see if it's a uniform issue? What about debugger, anything of use with that? – M4rc Oct 15 '12 at 10:03
  • @R.MartinhoFernandes I tested their code, here is copy-paste from console: Allocate a 524288 element buffer | Allocated 524288 bytes at 003E0020 | Can't expand – DividedByZero Oct 15 '12 at 10:03
  • @M4rc It's pool, I can't reallocate it anywhere else and on the top of it I need performance on this project, then copy 256KB is worse. – DividedByZero Oct 15 '12 at 10:05
  • @R.MartinhoFernandes: the MSDN page I found (http://msdn.microsoft.com/en-us/library/wfzt8b7y(v=vs.71).aspx) says has a paragraph: "if there is insufficient memory available to expand the block to the given size without moving it. The item pointed to by memblock is expanded as much as possible in its current location." - strange corruption - lack of capitalisation of the initial "if". Anyway - that suggests it resizes as much as possible in place, so may not return NULL simply because it can't expand to the requested size. – Tony Delroy Oct 15 '12 at 10:09
  • @DividedByZero: if you need performance, and have plenty of virtual address space, you might consider directly allocating much more than you're likely to use (see my final comment at http://stackoverflow.com/questions/4362604/expand-versus-new-versus-gnu) – Tony Delroy Oct 15 '12 at 10:11
  • @TonyD This pool is for my programming language. I can't initialize it with fixed-size. I need it expandable with runtime. – DividedByZero Oct 15 '12 at 10:18
  • @Tony That's the VS2003 docs, and it's clear they are... bad (the first sentence you quote does not make sense at all as an English sentence). Try the canonical URL, which gives the most up-to-date version: http://msdn.microsoft.com/en-us/library/wfzt8b7y.aspx – R. Martinho Fernandes Oct 15 '12 at 10:27
  • Someone? There isn't a way to expand to more then 16KB? – DividedByZero Oct 15 '12 at 11:13
  • @DividedByZero: you still don't seem to understand what this is doing. It _may_ expand to more than 16KB - there's nothing magical about that size - it all depends on the run-time dynamic memory usage of your application. This functions can't guarantee in-place resizing, they simply attempt it and report the success or failure. If you can't resize, you have to move it. If you want to move it less often, then size it generously to begin with (lots of virtual address space helps there), but there's no avoiding the tradeoffs or failure-to-resize-inplace case. – Tony Delroy Oct 16 '12 at 00:51
  • @TonyD Sorry but you are the one... I allocated it and line after I expanded it and got exception. – DividedByZero Oct 16 '12 at 00:53
  • The line after you expanded it you should have been checking the `_msize()` value (assuming you checked the `_expand` return value on the line you called it)... are you doing that? Post your actual code if you want help fixing it. – Tony Delroy Oct 17 '12 at 01:59
  • There's (usually) no guarantee that a newly allocated memory block will be at the top of the relevant pool, so there's no reason to expect _expand to always work even if it is called immediately after malloc. If you think what you're doing should work, show the code. – Harry Johnston Oct 17 '12 at 21:51
  • @TonyD I just started the program, allocate and expanded more than 16KB and it failed. This is just limited... – DividedByZero Oct 18 '12 at 10:17
  • @HarryJohnston Yeah right but still I did that in the start of the program. – DividedByZero Oct 18 '12 at 10:18
  • @DividedByZero: show the code or I won't spend any more time on your question.... – Tony Delroy Oct 18 '12 at 10:37

0 Answers0