0

I'm facing a really weird phenomenon of memcpy. I've allocated a data pointer with 2GB size, but it seems I can't do memcpy when my offset to the pointer is more than 1666800 bytes. Here is the code

dataMem = (struct dataRecord*) malloc(memsize * 1000000); // where memsize is 2000

... loop condition ...
{
    memcpy(dataMem + (dataCount * sizeof(struct dataRecord)), mesg, sizeof(struct dataRecord)); 
    dataCount++;
}

where sizeof(struct dataRecord) is 1200 bytes, and the dataCount is 1389. It supposed to be dataMem + 1666800 and the offset still far from 2000000000, as allocated before.

I really confused about this and have no clue.

  • What processer/compiler/OS are you using? – Gort the Robot Dec 12 '12 at 03:30
  • 3
    Please don't cast the return value of malloc in C - it can hide subtle errors. – paxdiablo Dec 12 '12 at 03:32
  • I use g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, Ubuntu 12.04 x64 3.2.0-23-generic, with 8GB RAM – Luck Haryadi Dec 12 '12 at 03:32
  • should I declare the `dataMem` as `void *` and then `dataMem = malloc(size)` ? – Luck Haryadi Dec 12 '12 at 03:34
  • So your OS is 32bit? I ask because you're asking to allocate approximately 1.907 gB of memory. How much is on the box is irrelevant past 4gB, only 3 gB of which (at best) is addressable in a 32bit process. Are we to assume you actually *checked* the return value of that `malloc()` unlike the code above? – WhozCraig Dec 12 '12 at 03:41
  • no, it's 64bit for sure. But the other case was that I cannot allocate more than 3 GB on this 64 bit OS. Yesterday I also search for the solution, but I think its OK for me to allocate only 2 GB for the process. – Luck Haryadi Dec 12 '12 at 03:43
  • Is there a reason you're NOT using the size of your data record in the allocation computation? Just curious. – WhozCraig Dec 12 '12 at 03:48
  • It's because I'm intended to use memory size parameters when executing my application. ex `./fva [memsize in MB]`. So I can prepare a mostly 2 GB memory to be inserted with bunch of data when triggered so. The data will be inserted using network. – Luck Haryadi Dec 12 '12 at 03:53

1 Answers1

0

So I modified the code and get rid of the malloc casting.

void *dataMem;
dataMem = malloc(memsize * 1000000);

Seems the memcpy is working properly now. But why we must not cast malloc?

  • 1
    When using C (*not* C++, where it is required) casting `malloc()` can hide the difference between `sizeof(int)` and `sizeof(void*)`, and if they're not the same on your platform, bad things ensue. [Read this](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc) for a better description. The better question is why your change 'fixed' anything. – WhozCraig Dec 12 '12 at 03:46
  • [More info here](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc). – Lundin Dec 12 '12 at 07:33