Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Um...

Name: Anonymous 2008-10-18 15:04

Why doesn't malloc just wait until enough memory becomes free instead of returning an error if there isn't enough available?

Name: Anonymous 2008-10-18 15:08

>>1
main() {
    fork();
    void *a = malloc( 1024 );
    void *b = malloc( 1024 );
    free( b );
    free( a );
}

Assuming there's only 2048 bytes of memory available, can you see a fairly obvious deadlock? It's easier to have it remain synchronous. If you're really serious about it,

void *awesum_malloc( size_t s ) {
    void *blob = malloc( s );
    if ( blob )
        return blob;
    sleep( 10 );
    return awesum_malloc( s );
}

Name: Anonymous 2008-10-18 15:09

Though realistically, if malloc is failing, you have more a more serious problem on your hands.

Name: Anonymous 2008-10-18 15:10

Because most applications don't want to wait forever waiting for memory allocation. And if you want it, you can add it yourself.

Name: Anonymous 2008-10-18 16:29

>>2
I hope your C compiler handles tail calls there, bucko.

Name: Anonymous 2008-10-18 17:37

>>5
Every self-respecting C compiler does. Go back to your fail language.

Name: Anonymous 2008-10-18 17:57

>>6
gcc doesn't, without explicitely stating you want to do so via obscure compiler flags.
On the other hand, my Scheme compiler does it by default.

Name: Anonymous 2008-10-18 18:35

>>7
obscure compiler flags
Yeah, like -O2. Really obscure, that.

Name: Anonymous 2008-10-18 18:37

Name: Anonymous 2008-10-18 19:01

>>8,9
The only way to know about the -Ox flags is to wade through a thousand page man page, or by word of mouth. If it doesn't do it by default, then any mechanism which provides the functionality is obscure.

Name: Anonymous 2008-10-18 19:08

>>10
The only way to know about the gcc command is to wade through a thousand page man page, or by word of mouth. If it doesn't do it by default, then any mechanism which provides the functionality is obscure.

Name: Anonymous 2008-10-18 19:18

Requiring a compiler-specific feature is bs, but why does it matter, it's trivial to rewrite awesum_malloc to a plain loop

for(;(blob = malloc(s)) == NULL; sleep(10)) {
  return blob;
}

Name: Anonymous 2008-10-18 20:02

>>11
Way to completely misunderstand my point. I'm specifically talking about command-line options. What if you were used to using Visual Sepples, and you had to use gcc for some cross-platform stuff. How would you know whether or not tail calls are implemented, or how to get the compiler to generate tail calls?

Name: Anonymous 2008-10-18 20:04

>>13 here. Just so we're clear, I wouldn't normally use gcc, nor will I ever use a GPL'd piece of software in my life, simply because all GPL'd code is shit. It's as simple as that.

Name: Anonymous 2008-10-18 21:41

>>14
Cudder?

Name: Anonymous 2008-10-18 21:42

>>13
By reading the docs. http://gcc.gnu.org/onlinedocs/

Name: Anonymous 2008-10-18 21:43

>>16
Spoiler failure

Name: Anonymous 2008-10-18 22:43


>>17
Maybe he meant to do that.

Name: >>2 2008-10-18 23:50

>>4-
YHBTE.

Name: Anonymous 2008-10-19 1:25

>>1
maybe if malloc() was written in Erlang you could do so.  However, if you were using Erlang, you wouldn't need such shit.

Name: Anonymous 2008-10-19 1:34

If it doesn't do it by default,
bitches don't know 'bout my sun studio c compiler

Name: Anonymous 2008-10-19 6:10

>>21
No I wouldn't, I tend not to use products from shitty enterprise companies.

Don't change these.
Name: Email:
Entire Thread Thread List