Complete.Org: Mailing Lists: Archives: discussion: February 2000:
[aclug-L] Re: Simple C question (which shows my C ignorance)
Home

[aclug-L] Re: Simple C question (which shows my C ignorance)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: aclug-L@xxxxxxxxxxxx
Subject: [aclug-L] Re: Simple C question (which shows my C ignorance)
From: Tom Hull <thull@xxxxxxxxxxx>
Date: Thu, 03 Feb 2000 01:33:27 -0600
Reply-to: aclug-L@xxxxxxxxxxxx

Larry Bottorff wrote:
> 
> I need to delete a member of an array of structure pointers and have the
> new situation reflected minus the one removed. Can I just do
> free(array1[i]), then decrement the index counter, or do I need to shift
> everything down? The members were allocated with malloc, and I'm lost.
> Any ideas?

free(3) frees a buffer previously allocated by malloc() or calloc(), but
does nothing to the pointer itself. it's still in your array, except now
if you reference it you'll get a bug that nobody will want to touch. you
either have to invalidate your pointer (e.g., set it to 0, always checking
to make sure it's non-0 before referencing it), or shift the rest of your
array down. you can use memcpy(3) for the shift down -- it's smart enough
not to trip on itself, and presumably is coded as efficiently as you can
do it. the choice depends on your usage, where you're willing to spend
the time (small time every access to check for 0, vs. larger time to
compact the array on every delete, vs. much larger time if the array
is huge). if none of these solutions are satisfactory, consider another
data structure.

> --
> Larry Bottorff  /  mrprenzl@xxxxxxxxxx  /  316.345.3387

-- 
/*
 *  Tom Hull * thull@xxxxxxxxxxx * http://www.ocston.org/~thull/
 */

[Prev in Thread] Current Thread [Next in Thread]