Paul Herring wrote:
> On 2/15/07, Thomas Hruska <[EMAIL PROTECTED]> wrote:
>> Brett W. McCoy wrote:
>>> On 2/15/07, shahwalishaik <[EMAIL PROTECTED]> wrote:
>>>
>>>>  I've attended an interview few days back. The qeustion paper had
>>>> some programs. There's a bug in it, and we need to find it. Please,
>>>> help me find the bug.
>>>>
>>>>    #include<string.h>
>>>>    #define MAX_STRING       80
>>>>    char  *Funct(char *p1, char *p2)
>>>>   {
>>>>     char str[MAX_STRING+1];
>>>>     int 1;
>>>>
>>>>     str[MAX_STRING] = '\0';
>>>>     1=strlen(str);
>>>>     strncat(str,p2,MAX_STRING-1);
>>>>     return str;
>>>> }
>>>>
>>>>
>>>> [mod - There's more than one bug. Simply putting this code through a 
>>>> compiler would show you some syntax errors - you cannot use the numeral 
>>>> one as a variable name (the
>>>>     int 1;  and
>>>>     1=strlen(str);
>>>> lines. A runtime error is the potential buffer overrun in the call to 
>>>> strncat() - PJH]
>>> The function also returns the address of the local str variable.
>> The function also has the bug of not doing anything useful even if it
>> did work.  Best bet is that it was supposed to concatenate two strings.
>>
>> I'd prefer the Safe C++ method of doing string concatenation:
> 
> But if they were using C++, they could have used std::string instead
> of C's string manipulation functions....

Sure they could, but I prefer BString - way too many limitations in 
std::string.


>> printf("%s\n", *str);
> 
> Is printf() part of Safe C++? ;)

I don't like cout - it is ugly to look at.  Personal preference, 
laziness, and history defers to printf() for lack of anything better 
(IMO).  The dereference operator overload of BString takes care of the 
string so it is properly \0 terminated (I wrote the dereference operator 
specifically to interface with things like external libraries and C 
calls).  Some habits die hard.  Or just refuse to die.  Something like that.

Does it really matter as long as the data gets dumped to the console?
(Or am I taking you too seriously and should pay more attention to the 
smiley face?)

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* VerifyMyPC 2.1
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.

Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/

Reply via email to