find the bugs

A

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
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

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)?

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.
}

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]
 
C

copx

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);

One bug found: it's C++ :p

[snip]
 
R

Richard Heathfield

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
 
M

Martin Ambuhl

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);
^^^^^^^^(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.
 
A

ak

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)?

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.
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
 
E

Eric Sosman

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.
 
B

Beej Jorgensen

Richard Heathfield said:
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
 
J

Joe Estock

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)?

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).
 
C

Christopher Benson-Manica

Joe Estock said:
ak wrote:
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.
 
R

Richard Heathfield

Christopher Benson-Manica said:
Joe Estock said:
ak wrote:

</context>

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.

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.
 
A

Ajinkya

Christopher Benson-Manica said:

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

</context>

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

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
 
R

Richard Heathfield

Ajinkya said:

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.
 
A

ak

Ajinkya said:



Wrong. It's a syntax error.


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.

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
 
R

Richard Heathfield

ak said:
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.
 
K

Kenny McCormack

Ajinkya said:



Wrong. It's a syntax error.


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...)
 
D

Default User

ak said:
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
 
D

Default User

Richard said:
Ajinkya said:



Wrong. It's a syntax error.


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
 
R

Richard Heathfield

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.
 
J

Joe Estock

Richard said:
Ajinkya said:



Wrong. It's a syntax error.


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++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top