473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 3580

"ak" <aj**********@gmail.comschrieb im Newsbeitrag
news:11**********************@y80g2000hsf.googlegr oups.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...@metasystems.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.invalidwrote:
>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*****@nospamnutextonline.comwrote:
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)gmail.com | don't, I need to know. Flames welcome.
May 5 '07 #10
Christopher Benson-Manica said:
Joe Estock <je*****@nospamnutextonline.comwrote:
>ak wrote:
<context quoted by Christopher>
std::string name = lookupName(c);
</context>

<context restored after being snipped by Christopher>
>>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;
}
</context>
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.
Yes, it is.
Such are the perils of posting C++ questions to comp.lang.c.
Such are the perils of carelessly snipping relevant context. And note
that the original code was (flawed) C code, not C++ code. If it were
C++ code, he would have posted it to comp.lang.c++, not comp.lang.c.
("Trust the programmer", remember?) And he didn't, so it isn't.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 5 '07 #11
On May 4, 10:35 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Christopher Benson-Manica said:
Joe Estock <jest...@nospamnutextonline.comwrote:
ak wrote:

<context quoted by Christopher> std::string name = lookupName(c);

</context>

<context restored after being snipped by Christopher>>>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;
}

</context>
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.

Yes, it is.
Such are the perils of posting C++ questions to comp.lang.c.

Such are the perils of carelessly snipping relevant context. And note
that the original code was (flawed) C code, not C++ code. If it were
C++ code, he would have posted it to comp.lang.c++, not comp.lang.c.
("Trust the programmer", remember?) And he didn't, so it isn't.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
In the function where if(name==NULL) is executed, in that function
'name' is not a pointer its a 'string'.
Its a c++ code which should not have been here in the first place.

Ciao,
Ajinkya

May 5 '07 #12
Ajinkya said:

<snip>
In the function where if(name==NULL) is executed, in that function
'name' is not a pointer its a 'string'.
Wrong. It's a syntax error.
Its a c++ code which should not have been here in the first place.
Only the OP knows whether that is the case. Since he posted it in
comp.lang.c, I will continue to presume that he intends it to be C code
unless he states otherwise.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 5 '07 #13
ak
On May 5, 12:49 am, Richard Heathfield <r...@see.sig.invalidwrote:
Ajinkya said:

<snip>
In the function where if(name==NULL) is executed, in that function
'name' is not a pointer its a 'string'.

Wrong. It's a syntax error.
Its a c++ code which should not have been here in the first place.

Only the OP knows whether that is the case. Since he posted it in
comp.lang.c, I will continue to presume that he intends it to be C code
unless he states otherwise.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Sorry mate I posted a c++ code in c.l.c Will avoid this hence forth. I
see it leads to a lot of confusion. By the way name is not a pointer
in getName function-its a string.

cheers,
ak

May 5 '07 #14
ak said:
On May 5, 12:49 am, Richard Heathfield <r...@see.sig.invalidwrote:

Sorry mate I posted a c++ code in c.l.c Will avoid this hence forth. I
see it leads to a lot of confusion.
It eventually leads to enlightenment.
By the way name is not a pointer
in getName function-its a string.
In C, it remains a syntax error. In C++, it becomes an off-topic
construct. In neither case is it a string, as far as comp.lang.c is
concerned.

Note in passing that at no point did I claim that the 'name' object in
getName is a pointer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 5 '07 #15
In article <JM******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Ajinkya said:

<snip>
>In the function where if(name==NULL) is executed, in that function
'name' is not a pointer its a 'string'.

Wrong. It's a syntax error.
>Its a c++ code which should not have been here in the first place.

Only the OP knows whether that is the case. Since he posted it in
comp.lang.c, I will continue to presume that he intends it to be C code
unless he states otherwise.
Um, dumb dumb, he has already stated that it *is* C++.
And that it was an error to post it here. And even apologized for doing
so.

How much more stupid can you get?

(Wait. Don't answer that...)

May 5 '07 #16
ak wrote:
On May 5, 12:49 am, Richard Heathfield <r...@see.sig.invalidwrote:
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.

Sorry mate
Please trim your posts, especially signatures (I have retained an
example above). I know Google doesn't do that automatically, but
nothing stops you from doing it.


Brian
May 5 '07 #17
Richard Heathfield wrote:
Ajinkya said:

<snip>
In the function where if(name==NULL) is executed, in that function
'name' is not a pointer its a 'string'.

Wrong. It's a syntax error.
Its a c++ code which should not have been here in the first place.

Only the OP knows whether that is the case. Since he posted it in
comp.lang.c, I will continue to presume that he intends it to be C
code unless he states otherwise.

As the OP posted the same question to clc++ after the initial flurry
here, I think it's safe to infer that without an explicit statement.


Brian
May 5 '07 #18
Default User said:
Richard Heathfield wrote:
>Ajinkya said:
<snip>
>>
Its a c++ code which should not have been here in the first place.

Only the OP knows whether that is the case. Since he posted it in
comp.lang.c, I will continue to presume that he intends it to be C
code unless he states otherwise.


As the OP posted the same question to clc++ after the initial flurry
here, I think it's safe to infer that without an explicit statement.
Only for those who subscribe to clc++ (or, of course, for those who
trust the claims of those who do). And, naturally, I trust your claim.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 5 '07 #19
Richard Heathfield wrote:
Ajinkya said:

<snip>
>In the function where if(name==NULL) is executed, in that function
'name' is not a pointer its a 'string'.

Wrong. It's a syntax error.
>Its a c++ code which should not have been here in the first place.

Only the OP knows whether that is the case. Since he posted it in
comp.lang.c, I will continue to presume that he intends it to be C code
unless he states otherwise.
Which is also what I was doing. I do not subscribe to c.l.c++ and the
original article was posted to c.l.c therefore I presumed it to be
[faulty] c code and orchestrated my responses accordingly. After
following the later threads I see that it was, in fact, meant for c.l.c++.
May 6 '07 #20
Richard Heathfield wrote:
Default User said:
As the OP posted the same question to clc++ after the initial flurry
here, I think it's safe to infer that without an explicit statement.

Only for those who subscribe to clc++ (or, of course, for those who
trust the claims of those who do). And, naturally, I trust your claim.

I was certain that you would find that information to be of use.


Brian
May 6 '07 #21
On May 5, 5:35 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>
<context restored after being snipped by Christopher
>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;
}
</context>
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.

Yes, it is.
The "name==NULL" code occurred inside the getName() function,
where the pointer 'name' is not visible.

May 6 '07 #22
Old Wolf said:
The "name==NULL" code occurred inside the getName() function,
where the pointer 'name' is not visible.
BION, IR = (in the OP) as ==, and from that misreading I proceeded
directly to the scene of the misunderstanding. Mea culpa.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 6 '07 #23

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

Similar topics

83
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...
108
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...
20
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
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...
5
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
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...
15
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
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) {...
2
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...
87
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.