• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: 64-bit question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 64-bit question


  • Subject: Re: 64-bit question
  • From: Mike Fischer <email@hidden>
  • Date: Wed, 9 Nov 2005 02:12:26 +0100

Am 08.11.2005 um 21:03 schrieb Sam Hart <email@hidden>:

I think it is a bug in the 32bit handling too, albeit less dramatic.
The process b0rked with a bus error rather than an "Out Of Memory".

Given the source snippet Bob originally posted on this list I think it might simply be one of the following bugs in his code:
- The stack gets overwritten when i > ARRAY_SIZE.
- It tries to copy from NULL after malloc failed.
- The strcpy function will copy an unspecified amount of data because buf is not a \0 terminated C string. Depending what comes after buf on the stack this will overwrite more or less dynamic memory which might also cause a crash. strncpy or memset would have been better.


Oh and some other non crashing bugs or oddities in that code are:
- The display of the allocated memory dosn't include the initial allocation of bigbuf so it is off by about 8MB (for 64-bit, or 4MB for 32-bit).
- The choice of 1000000 is somewhat arbitrary, given that the page size of the VM system is 4096 bytes. I'd use 1MB or 1024*1024.


Here is a more correct version of said code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define	BUFFER_SIZE (1024*1024)

int main(int argc, const char * argv[])
{
    long long int   i = 0,
		    total = 0;

    while (++i)
    {
        char    *bp = malloc(BUFFER_SIZE * sizeof(char));

if (bp == NULL)
{
printf("Internal malloc failed!\n");
break; // When this happens then the situation will not get better. Give up right away.
}
else
{
memset(bp, 'A', BUFFER_SIZE * sizeof(char)); // Access the memory so the VM system will actually allocate
// physical memory, not just logical address space.
// Note: it would be enough to touch one byte of every memory
// page but the speed difference will not justify the more
// complicated code as the VM thrashing will hide any
// small perfomance gains made here.
total = i; // Keep track of the total.
if (i % 1000 == 0) // Don't print too much.
{
printf("MB allocated: %lld\n",i);
}
}
}
printf("Done! Total MB allocated: %lld\n", total);
while (1) { } // So it doesn't exit! Really bad style to use an infinite loop but ok for this test!


    return 0;
}

Does this version crash with a bus error as well?


Mike -- Mike Fischer Softwareentwicklung, EDV-Beratung Schulung, Vertrieb Address: Bundesstrasse 9, D-20146 Hamburg, Germany Phone: +49 (0)40/45038886, Fax: +49 (0)40/45038887

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: write(2)/writev(2) atomicity?
  • Next by Date: Re: Active DHCP interfaces
  • Previous by thread: Re: 64-bit question
  • Next by thread: Re: Thread critical sections
  • Index(es):
    • Date
    • Thread