TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Neil Heller
date: 2004-06-28 17:18:02
subject: Testing - part 2

9.    Consider the following snippet of C code for looking up the size 
of a user's address book.   Assume that fetchEntries( ) and freeEntries( 
) are already written and do the right thing (allocate an array of 
entries and fill in the correctly allocated array, respectively):

int fetchEntries( int userid, int * countp, entry_t ** entrypp );
int freeEntries( entry_t * entryp );

int getEntryCount( int userid, int * countp) {
    int       count;
    entry_t * entryp;
    int       errcode;
    errcode = fetchEntries( userid, &count, &entryp );

    if (errcode) return(errcode);

    freeEntries(entryp);
    countp = &count;
    return 0;
}


int entryCount;
getEntryCount( 123, &entryCount );
printf( "User has %d entries\n", entryCount);


What's wrong with the way getEntryCount( ) is written?

    a)  freeEntries( ) is called before countp is assigned ?

    b)  freeEntries should be called on &entryp just like fetchEntries.

    c)  entryp should be passed into fetchEntries instead of &entryp 
because it is already a pointer 

    d)  The caller’s count variable (e.g. entryCount) isn’t properly 
assigned.


10.  How could you fix this code to work correctly?

    a)  Call freeEntries( ) after setting countp = &count (instead of 
before it). 

    b)  Dereference countp before assigning it to count (instead of 
assigning countp directly).

    c)  Change getEntryCount to take an int for the entry count (instead 
of a pointer to an int).

    d)  Pass in &entryCount to fetchEntries( ) instead of &count.

þ CMPQwk 1.42 999

--- Maximus/2 3.01
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.