Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: C vs perl

by meonkeys (Chaplain)
on Apr 28, 2002 at 18:45 UTC ( [id://162696]=note: print w/replies, xml ) Need Help??


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*.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://162696]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-25 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found