473,804 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find the bugs

ak
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?

const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous" ;
return name.c_str();
}

int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);

printf("My name is %s\n", name);
return 0;
}

MY SOLUTION : #bug 1: name==NULL is illegal

#bug 2: name=getName(c) should be included in
the 'if' statement so that NULL will
not be
passed to getName()

The above was my solution...what do you all experts think?
Am i right or have i missed something?

Cheers,
ak

May 4 '07 #1
22 3625

"ak" <aj**********@g mail.comschrieb im Newsbeitrag
news:11******** **************@ y80g2000hsf.goo glegroups.com.. .
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?

const char *getName(const char *c) {
std::string name = lookupName(c);
One bug found: it's C++ :P

[snip]

May 4 '07 #2
ak wrote:
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?
You're posting C++ code to comp.lang.c, not a great idea. Try
comp.lang.c++ instead.
>
const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous" ;
return name.c_str();
<OT>
One bug: The function returns a pointer to mem which will be freed by
the std::string dtor.
</OT>

}

int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);

printf("My name is %s\n", name);
Bug 2: If argc < 2, then you call printf() with name==NULL.
return 0;
}

MY SOLUTION : #bug 1: name==NULL is illegal
If so, it is a syntax error and not a bug.

HTH
Bjørn

[snip]
May 4 '07 #3
ak said:
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?

const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous" ;
return name.c_str();
}

int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);

printf("My name is %s\n", name);
return 0;
}
foo.c:1: warning: no previous prototype for `getName'
foo.c: In function `getName':
foo.c:2: parse error before `:'
foo.c:3: `name' undeclared (first use in this function)
foo.c:3: (Each undeclared identifier is reported only once
foo.c:3: for each function it appears in.)
foo.c:3: `NULL' undeclared (first use in this function)
foo.c:2: warning: label `std' defined but not used
foo.c:1: warning: unused parameter `c'
foo.c: In function `main':
foo.c:9: `NULL' undeclared (first use in this function)
foo.c:14: warning: implicit declaration of function `printf'
make: *** [foo.o] Error 1

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 4 '07 #4
ak wrote:
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?

const char *getName(const char *c) {
std::string name = lookupName(c);
^^^^^^^^(1) ^^(2)
(1) syntax error
(and the label 'std:' is unused, but that's not an error)
(2) undefined identifier (which also makes c unused, but that's not an
error)

That's two. Do I need to find more? Ok.
if (name == NULL)
^^(3)
(3) undeclared identifier
return "Anonymous" ;
return name.c_str();
}

int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
^^(4)
(4) undeclared identifier
if (argc >= 2)
c = argv[1];
name = getName(c);

printf("My name is %s\n", name);
^^^^(5)
(5) variadic function printf() used with out a declaration in scope
return 0;
}

MY SOLUTION : #bug 1: name==NULL is illegal
If 'name' and 'NULL' are defined, 'name == NULL' perfectly legal
#bug 2: name=getName(c) should be included in
the 'if' statement so that NULL will
not be
passed to getName()
Who cares? The example is hopeless broken without looking to the
'logic' of the code.
>
The above was my solution...what do you all experts think?
Am i right or have i missed something?
You are wrong. And you at least missed knowing the name of the language
you are using. The different language C++ has its own newsgroup.
May 4 '07 #5
ak
On May 4, 9:05 am, Bjørn Augestad <b...@metasyste ms.nowrote:
ak wrote:
Recently at an interview i was asked the following question :
Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?

You're posting C++ code to comp.lang.c, not a great idea. Try
comp.lang.c++ instead.
const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous" ;
return name.c_str();

<OT>
One bug: The function returns a pointer to mem which will be freed by
the std::string dtor.
</OT>
}
int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);
printf("My name is %s\n", name);

Bug 2: If argc < 2, then you call printf() with name==NULL.
return 0;
}
MY SOLUTION : #bug 1: name==NULL is illegal

If so, it is a syntax error and not a bug.

HTH
Bjørn

[snip]
Sorrry for posting this question on c.l.c will remember that.
Thanks
AK

May 4 '07 #6
ak wrote On 05/04/07 11:58,:
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?

const char *getName(const char *c) {
std::string name = lookupName(c);
#001: Syntax error.
if (name == NULL)
#002: No declaration for name.

#003: No declaration for NULL.
return "Anonymous" ;
return name.c_str();
#004: No declaration of name as a struct or union
type with a c_str element.
}

int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
#005: No declaration for NULL.

#006: No declaration for NULL.
if (argc >= 2)
c = argv[1];
name = getName(c);

printf("My name is %s\n", name);
#007: No declaration for printf().
return 0;
}

MY SOLUTION : #bug 1: name==NULL is illegal

#bug 2: name=getName(c) should be included in
the 'if' statement so that NULL will
not be
passed to getName()

The above was my solution...what do you all experts think?
Am i right or have i missed something?
#010: Mistaking That Other Language for C.

--
Er*********@sun .com
May 4 '07 #7
Richard Heathfield <rj*@see.sig.in validwrote:
>foo.c:1: warning: no previous prototype for `getName'
foo.c: In function `getName':
foo.c:2: parse error before `:'
I'm not sure this is the answer the interviewers were looking for. :)

-Beej

May 4 '07 #8
ak wrote:
Recently at an interview i was asked the following question :

Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?
I'm presuming the required headers were omitted for brevity (which,
generally in interviews they are since the focus is primarily on the
code syntax and/or logic).
>
const char *getName(const char *c) {
std::string name = lookupName(c);
(1) std::string is not a valid statement in C.
if (name == NULL)
return "Anonymous" ;
return name.c_str();
(2) Technically speaking you would generally receive the diagnostic
"Request for member not in a struct or union" or something similar since
"name" was not declared. Regardless of the diagnostic message this is,
in fact, the second error that the interviewer was looking for.
}

int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);

printf("My name is %s\n", name);
return 0;
}

MY SOLUTION : #bug 1: name==NULL is illegal
How so? You're comparing a pointer to NULL which is perfectly legal.
>
#bug 2: name=getName(c) should be included in
the 'if' statement so that NULL will
not be
passed to getName()
Why? Both of the following statements would yield the same results,
however the first one is most certainly easier to maintain and read:

if (name == NULL)
{
/* code here */
}

if ((name = getName(c)) == NULL)
{
/* code here */
}

Note the additional set of parentheses around the function call. Passing
a NULL value to a function /shouldn't/ be an issue provided that the
function were written to handle NULL values (as it should).
>
The above was my solution...what do you all experts think?
I'm no expert, but I know my way around the language and I'll offer my
two cents. Whether you choose to listen or not is another story, however.
Am i right or have i missed something?

Cheers,
ak
No, you are not right and yes you have missed something. In fact you
have missed many "something" 's. I strongly suggest that you study the
language in more detail before attempting to apply for another position
in your chosen field. I realize that you did not write the above
fragment of code, however you also didn't analyze it correctly either.

Some interesting topics to focus on would be operator precedence,
general logic, as well as familiarizing yourself with the standard C
library and learning the keywords used in the language. Once you are
comfortable with these topics you could then focus on reading and
writing code. Pointers are also another area that you should focus on,
however I think that you should first get a grasp on the fundamentals of
the language (and programming in general, really).
May 5 '07 #9
Joe Estock <je*****@nospam nutextonline.co mwrote:
ak wrote:
std::string name = lookupName(c);
MY SOLUTION : #bug 1: name==NULL is illegal
How so? You're comparing a pointer to NULL which is perfectly legal.
name is not a pointer. Such are the perils of posting C++ questions
to comp.lang.c. OP is correct.
Some interesting topics to focus on would be operator precedence,
general logic, as well as familiarizing yourself with the standard C
library and learning the keywords used in the language.
Given that the code (and the errors) are specific to C++, this advice,
while obviously sound, is unlikely to help OP pass a C++ exam.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
May 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
3483
by: kartik | last post by:
there seems to be a serious problem with allowing numbers to grow in a nearly unbounded manner, as int/long unification does: it hides bugs. most of the time, i expect my numbers to be small. 2**31 is good enough for most uses of variables, and when more is needed, 2**63 should do most of the time. granted, unification allows code to work for larger numbers than foreseen (as PEP 237 states) but i feel the potential for more undetected...
108
6476
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the /start/ and /stop/ indices and the /step/ or stride length of the slice. Missing or out-of-bounds indices are handled in a manner...
20
4140
by: Prashanth Badabagni | last post by:
hi, i'm prashanth Badabagni .. Can anyone tell me the BUGS present in C language whether programming or syntactical BUGS .... Thanks in advance ... Prashanth Badabagni
9
1474
by: David Teran | last post by:
Hi, we are currently using another database product but besides some licensing issues we are finding more and more problems with the database. We are evaluating PostgreSQL and it looks quite handy. Interesting features paired with good performance and a lot of users seems to be a prove for the quality. I wonder if people encountered bugs like 'a select returns a wrong number of rows' or any other very serious bugs. We do not have...
5
1483
by: NewToCPP | last post by:
There are several occations where we write onto someone else' memory region. Is there any debugging mechanism to find out which part of the code is causing this problem?
2
2223
by: TheSteph | last post by:
Using : Windows 2000 Pro SP4 / VS.NET 2005 / .NET 2.0 / C# - All updates done. I have several bugs when I use the DataGridView : When scrolling (or after scrolling) the grid have these problems :
15
2169
by: Gary Peek | last post by:
Can anyone tell us the browsers/versions that exhibit errors when tables are nested too deeply? And how many levels of nesting produces errors? (not a tables vs CSS question)
26
350
by: ak | last post by:
Recently at an interview i was asked the following question : Assuming the function lookupName is defined, what's wrong with this code (hint: 2 bugs)? const char *getName(const char *c) { std::string name = lookupName(c); if (name == NULL) return "Anonymous";
2
4166
by: dev_15 | last post by:
Hi, Does anyone know of any good links to sites etc which have exercises where you need to find out cause of runtime errors in a given piece of code. I am a bit weak in this area. Are there any books with these type of problems in? Thanks
87
5581
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first place? I can see two definite disadvantages with this: 1) deeply nested recursive calls to a function (especially if it defines
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.