The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

How could I make this explanation clearer? by Phoon 2007-09-23 18:57:39
This is from an email response I just sent to a student asking about malloc()...
> my biggest fear is how do i get malloc working right, that is with my
> struct.

malloc() can be one of the trickier parts of C, simply because it
requires knowledge of pointers and indirection.

Basically, all malloc() does is tell the operating system to give your
program a certain number of bytes of memory.  Remember that the argument
to malloc() is always interpreted as bytes.  If you need to contain
anything other than character values, you need to use the sizeof() operator:

struct mystruct *ptr;

ptr = (struct mystruct *) malloc(sizeof(struct mystruct));

ptr will have one of two things: a pointer to a region of memory big
enough to hold a struct mystruct, or NULL.  You *always* have to check
for NULL when you call malloc().

if (ptr == NULL) {
        fprintf(stderr, "ZOMGWTF?");
        exit(EXIT_FAILURE);
}

(You could use abort() instead of exit() if you want.)

When you access the resulting structure, you need to "indirect" through
the pointer.  This is done with the asterisk operator:

int val;

val = (*ptr).val_field;

Of course, this is a common enough procedure to have a shortcut:

val = ptr->val_field;

One last thing to remember: everything you malloc(), you must free().

if (ptr != NULL) {
        free(ptr);
        ptr = NULL;
}

You don't have to explicitly set ptr to NULL, but it's a very good idea,
because freeing the same memory twice will generally make your program
crash.  Likewise, freeing NULL will also make your program crash, hence
the if statement.  (Memory allocation is the biggest source of bugs in C
programs, so it's worth a little bit of extra effort to make sure
everything is safe.)

If you have any further questions, I'll be in my 'office' tomorrow.
[ Reply ]
  Replace "ZOMGWTF?" with a real error message (n/t) by vdp2007-09-23 19:03:15
  Sounds very clear to me by oot2007-09-23 19:08:52
    Steely Dan: Ricky, don't lose that pointer. by kahuana2007-09-23 19:13:53
  Put in what a pointer really is, by jiteo2007-09-23 19:56:42
  "Where's your malloc statment?" by voxwoman2007-09-24 04:02:41

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)