-1

I'm translating codes from C++ source code to delphi language. There is a partition allocated to memory in C++ source code and when I allocate the same partition in delphi I get an error. Where am I doing wrong? I've used getmem, setlength and all the others.

C++ code (work) :

int main()
{


    char* data = reinterpret_cast<char*>(malloc(938278912));
    data[938278911]='m';
    printf ("%c \n",data[938278911]);
    return 0;
}

Delphi code (not work):

  procedure TMainForm.UnpackButton1Click(Sender: TObject);
  var
  tmpbuf : Pointer;
  begin
  GetMem(tmpbuf,938278912);
  end

Where am i doing wrong ?

SeCi
  • 9
  • 2
  • I'd try it with a smaller number (like, 2 instead of 938278912). –  Feb 07 '22 at 14:03
  • 1
    Hardly surprising that you can't find 1GB of contiguous address space in what I presume is a 32 bit process. If you need 1GB of contiguous address space, do it in a 64 bit process – David Heffernan Feb 07 '22 at 14:04
  • Yes then it works. The size I need to read is not that small. – SeCi Feb 07 '22 at 14:05
  • I'm compiling the c++ source code as 32 bit and it works fine. Why doesn't it work properly in Delphi? Many of my plugins and libraries are prepared as 32 bit. Is there any way I can run 32 bit ? – SeCi Feb 07 '22 at 14:09
  • 2
    First question you should ask yourself: Why on earth do you need 938'278'912 bytes of contiguous memory? – Jabberwocky Feb 07 '22 at 14:11
  • @SeCi -- If what you posted is the only way to allocate memory in Delphi, what else can we tell you to do? Either accept that `GetMem` works the way it does, or use something different to allocate memory. Maybe call the OS allocation function(s) instead of Delphi canned functions? – PaulMcKenzie Feb 07 '22 at 14:12
  • @Jabberwocky It's written that way in C++ source code. The person who wrote it is google, I think we should ask him the question. – SeCi Feb 07 '22 at 14:14
  • @PaulMcKenzie My goal is to understand why it's not working. What is the code against delphi when working in C++. My question is not only for this problem. – SeCi Feb 07 '22 at 14:15
  • @SeCi C++ is not Delphi. You are forgetting that simple fact. If you don't know the inner-workings of `GetMem`, then you cannot assume what you have is equivalent C++. The second thing you could have done -- what is the maximum amount of memory that `GetMem` will work with? What is the point where `GetMem` stops working? – PaulMcKenzie Feb 07 '22 at 14:16
  • @PaulMcKenzie I know, but I don't know what the solution is. I need to translate this resource into Delphi language. – SeCi Feb 07 '22 at 14:20
  • @SeCi -- You say translate to Delphi language, but then you also make the claim it has to be 32-bit. What if there is not enough memory when run in Delphi (that was already mentioned). When you attempt to allocate close to 1 GB of contiguous memory in a 32-bit program, expect things to possibly not work at some point, and the only solution is to allocate less, or go to 64-bit. – PaulMcKenzie Feb 07 '22 at 14:22
  • @PaulMcKenzie Understood. Thank you for your help. – SeCi Feb 07 '22 at 14:24
  • What are you actually trying to do because this program is quite useless? – David Heffernan Feb 07 '22 at 14:59
  • *Where am i doing wrong?* You seem to be comparing grape with oranges. I see now evidence of Windows environment in your C++ code. OTOH the Delphi code you show is a Windows program. If you create a console application in Delphi, the required memory is available. – Tom Brunberg Feb 07 '22 at 15:05
  • @DavidHeffernan I didn't write the C++ source code. I got it directly from google source. My application is written in delphi language. So I need to translate the code. When I compile and run the Google source code, it uses around 1 mb. The incoming data is around 950 mb, so it works without any errors. My source code in delphi already takes up around 150 mb of memory without any processing, and when it comes to 950 mb, I get an error. I guess I'll have to shred the data or write it as a file. – SeCi Feb 07 '22 at 15:08
  • @TomBrunberg Yeah. I realized my mistake. Thanks. – SeCi Feb 07 '22 at 15:12
  • Related : [How can I enable my 32-bit Delphi application to use 4gb of memory on 64-bit windows](https://stackoverflow.com/q/1849344/327083). This might explain why the C++ application "works" and the Delphi one "doesn't work". Making your 32-bit Delphi app large-address-aware would perhaps allow this example to work, but the solution, as with the C++ version, would remain fragile for reasons others have outlined here. – J... Feb 07 '22 at 15:44
  • Don't know what you mean when you say the C++ code takes 1mb. Look at it. It allocates close to 1gb on the heap. Do you understand this code? Do you realise how useless it is? – David Heffernan Feb 07 '22 at 16:20

1 Answers1

3

As you explain in your comments you are compiling as a 32 bit process. And you are asking for a memory block of around 1gb. The system is failing to allocate this because it so happens that your process doesn't have a free contiguous memory block of that size.

This is entirely to be expected in a 32 bit process which has a total address space of either 2gb or 4gb depending on whether or not your process is large address aware. It's entirely possible that in some scenarios the C++ code would fail similarly.

You cannot expect to reliably allocate such memory blocks in 32 bit process. Either use a 64 bit process or allocate multiple smaller blocks of memory.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490