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: Brian Chapman <tchapman@xxxxxxxxxxxxx>
Date: Sat, 05 Feb 2000 12:23:45 -0600
Reply-to: aclug-L@xxxxxxxxxxxx

At 2/3/00 01:12 AM , 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?

Anytime your having problems with a C program it is always best to post
the code segment you're having problems with. It's often a good chance
that the real problem may not even be what you think it is.

From what i can gather from your post it sounds to me as if you are trying
to implement a fixed array for dynamicaly allocated memory pointers,
perhaps for alien invaders in a space game? =)

I'll assume the latter since it's more interesting, but the fundamental
algorithm should suffice for any similar situation. (The following code is
for illustration purposes only):


/* AllocateAlien 
 * returns: NULL on error else a fresh new alien.
 */

alien_t *
AllocateAlien (void)  
{
    register i;

    for (i=0; i < MAX_LIVE_ALIENS; i++)
        if (live_aliens [i] == NULL)
        {
            live_aliens [i] = (alien_t *) malloc (sizeof(alien_t));
            return live_aliens [i];
        }

    return NULL;
}

/* FreeAlien 
 * removes a dead alien from memory.
 */
        
void 
FreeAlien (alien_t *alien)
{
    if (alien != NULL) 
        free (alien);
}




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