Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: malloc



On Wed, Jul 9, 2008 at 8:55 AM, andrea lucaroni <email@hidden> wrote:
> Hi
>
> I writing a code in C,

No, you're not. You're writing code in C++ (the difference is significant).

> i run my code with powerpc-apple-darwin8-g++-4.0.1,
> my code compiles, to its inside demands an Array of 100000 elements (has
> controlled if I malloc the memory well) i get:
>
> a.out(1232) malloc: *** vm_allocate(size=172032) failed (error code=3)
> a.out(1232) malloc: *** error: can't allocate region
> a.out(1232) malloc: *** set a breakpoint in szone_error to debug
>
> -----
> //code
> int dimdatif=100000;
>  chunk=(int *)malloc(dimdatif*sizeof(int));
>
> ....
>
> delete chunk;
> --------

First, don't use malloc with delete. Either use new/delete or malloc/free.
Second, if you're deleting an array, you have to use "delete[]", not "delete"

Either use:
----
int dimdatif=100000;
chunk=new int[dimdatif];
...
delete [] chunk;
----
Or use:
----
int dimdatif=100000;
chunk=(int *)malloc(dimdatif*sizeof(int));
...
free(chunk);
----

> what can have happened?

Any number of things could cause this. Perhaps you have a memory leak
elsewhere in your code (maybe even caused by your attempting to mix C
and C++ memory management facilities).

I suspect that that error isn't actually caused by this specific
allocation. Allocating 100000 int's would take at least 400000 bytes
of memory, while the error is claiming that it is attempting to
allocate 172032, which is nowhere near enough for your needs.

Have you tried setting a breakpoint on szone_error (as the log message
suggests) or malloc_printf? This will likely help you track down
exactly which allocation is going wrong.


-- 
Clark S. Cox III
email@hidden
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/xcode-users/email@hidden

This email sent to email@hidden

References: 
 >malloc (From: "andrea lucaroni" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.