in reply to Re: C vs perl
in thread C vs perl

++broquaint (after fixes, of course). A mighty phat response. One question since I am still learning C... when you do
ret = (char*) malloc(strlen(str) + strlen(str));
Are ret[0], ret[1], ret[2], ... ret[n] all set to '\0'? My manual for malloc() says "The memory is not cleared", but in my debugger it looks like all these values are zero.

Also, I don't think you need to cast malloc(). In fact, it might hide errors.

---
"A Jedi uses the Force for knowledge and defense, never for attack."

Replies are listed 'Best First'.
Re: Re: Re: C vs perl
by Elian (Parson) on Apr 28, 2002 at 19:21 UTC
    malloc just hands you back some memory that it has handy. If it's newly allocated from the system, odds are that it'll be zeroed. (On most OSes these days freshly minted process memory comes pre-zeroed since it's no more expensive to hand you zeroed memory as any other type) malloc keeps a free list, though, and when you free() some memory, you may get that same chunk back later, with whatever gook might be in it.

    If you want guaranteed zeroed memory, use calloc() instead. It zeroes the memory before it's handed to you, and generally in the way most efficient for the OS you're on.

Re: Re: Re: C vs perl
by h.toothrot (Initiate) on Apr 29, 2002 at 10:33 UTC
    Also, I don't think you need to cast malloc(). In fact, it might hide errors.

    The type of malloc is void*, I think I remember a compiler telling me that char* is not the same as void*. Consequently I had to cast malloc to char*.