find the bugs

Discussion in 'C Programming' started by ak, May 4, 2007.

  1. ak

    ak Guest

    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
     
    ak, May 4, 2007
    #1
    1. Advertising

  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]
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, May 4, 2007
    #2
    1. Advertising

  3. ak

    copx Guest

    "ak" <> schrieb im Newsbeitrag
    news:...
    > 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]
     
    copx, May 4, 2007
    #3
  4. 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.
     
    Richard Heathfield, May 4, 2007
    #4
  5. 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.
     
    Martin Ambuhl, May 4, 2007
    #5
  6. ak

    ak Guest

    On May 4, 9:05 am, Bjørn Augestad <> wrote:
    > 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
     
    ak, May 4, 2007
    #6
  7. ak

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, May 4, 2007
    #7
  8. Richard Heathfield <> wrote:
    >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
     
    Beej Jorgensen, May 4, 2007
    #8
  9. ak

    Joe Estock Guest

    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).
     
    Joe Estock, May 5, 2007
    #9
  10. Joe Estock <> wrote:

    > 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.
     
    Christopher Benson-Manica, May 5, 2007
    #10
  11. Christopher Benson-Manica said:

    > Joe Estock <> wrote:
    >
    >> 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.
     
    Richard Heathfield, May 5, 2007
    #11
  12. ak

    Ajinkya Guest

    On May 4, 10:35 pm, Richard Heathfield <> wrote:
    > Christopher Benson-Manica said:
    >
    > > Joe Estock <> wrote:

    >
    > >> 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
     
    Ajinkya, May 5, 2007
    #12
  13. 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.
     
    Richard Heathfield, May 5, 2007
    #13
  14. ak

    ak Guest

    On May 5, 12:49 am, 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.
    >
    > --
    > 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
     
    ak, May 5, 2007
    #14
  15. ak said:

    > On May 5, 12:49 am, Richard Heathfield <> wrote:
    >
    > 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.
     
    Richard Heathfield, May 5, 2007
    #15
  16. In article <>,
    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.


    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...)
     
    Kenny McCormack, May 5, 2007
    #16
  17. ak

    Default User Guest

    ak wrote:

    > On May 5, 12:49 am, Richard Heathfield <> wrote:


    > > --
    > > 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
     
    Default User, May 5, 2007
    #17
  18. ak

    Default User Guest

    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
     
    Default User, May 5, 2007
    #18
  19. 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.
     
    Richard Heathfield, May 5, 2007
    #19
  20. ak

    Joe Estock Guest

    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++.
     
    Joe Estock, May 6, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl J. Van Arsdall

    trackin down hard to find bugs

    Carl J. Van Arsdall, Sep 6, 2006, in forum: Python
    Replies:
    0
    Views:
    296
    Carl J. Van Arsdall
    Sep 6, 2006
  2. ak

    find the bugs

    ak, May 4, 2007, in forum: C++
    Replies:
    26
    Views:
    806
    Jim Langston
    May 7, 2007
  3. Josef 'Jupp' Schugt

    Still use 'ruby-bugs' for Ruby bugs?

    Josef 'Jupp' Schugt, Nov 4, 2004, in forum: Ruby
    Replies:
    2
    Views:
    169
    Tom Copeland
    Nov 4, 2004
  4. Wybo Dekker
    Replies:
    1
    Views:
    379
    Yukihiro Matsumoto
    Nov 15, 2005
  5. vdvorkin
    Replies:
    0
    Views:
    424
    vdvorkin
    Feb 10, 2011
Loading...

Share This Page