Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 4
There are 1 users currently browsing forums.
C and C++ Programming C and C++ are both robust and hugely popular programming languages that are used in multiple facets of programming, ranging from games to operating systems to simple text editors. To dicuss and seek advice, ask around here.

Reply
  #1  
Old 02-25-2010
New Born
 
Join Date: Feb 2010
Posts: 2
Rep Power: 0
Abbash is on a distinguished road
Smile What is wrong with this code?

Hi All,

This is my very first thread in this forum.
Could anybody help me with this code:

class Foo
{
char* buf;
public:
Foo(const char* b = "default")
{
if(b)
{
buf = new char[strlen(b) + 1];
strcpy(buf, b);
}
else
buf = 0;
}

~Foo() { delete[] buf; }
};

Foo func(Foo f)
{
return f;
}

Referring to the code above, when the func is called, the program may crash or exhibit unexpected behaviour, what is the reason for this problem?

Many thanks
Abbash
Reply With Quote
  #2  
Old 03-04-2010
Mancunian
 
Join Date: Mar 2010
Location: Greater Manchester, UK
Age: 18
Posts: 23
Rep Power: 0
Dumaju is on a distinguished road
Re: What is wrong with this code?

Could you provide the error messages? It may also be helpful if you pasted your code whilst wrapping it in [code][/code] tags.
Reply With Quote
  #3  
Old 03-12-2010
New Born
 
Join Date: Feb 2010
Posts: 2
Rep Power: 0
Abbash is on a distinguished road
Re: What is wrong with this code?

Quote:
Dumaju originally posted: View Post
Could you provide the error messages? It may also be helpful if you pasted your code whilst wrapping it in [code][/code] tags.
This was an interview question that I had to do and did not know the answer. It say "This program may crash..." but then again it may not crash. The question is by looking at the code we should be able to determin what is wrong with this code.

Code:
class Foo
{
    char* buf;
public:
    Foo(const char* b = "default")
    {
        if(b)
        {
            buf = new char[strlen(b) + 1];
            strcpy(buf, b);
        }
        else
            buf = 0;
    }

    ~Foo() { delete[] buf; }
};

Foo func(Foo f)
{
    return f;
}
Reply With Quote
  #4  
Old 03-13-2010
Mancunian
 
Join Date: Mar 2010
Location: Greater Manchester, UK
Age: 18
Posts: 23
Rep Power: 0
Dumaju is on a distinguished road
Re: What is wrong with this code?

My guess would be that you may be expecting the function to return a reference, when it's really returning by value? I'll check it out a bit more when I've downloaded and installed Visual Studio.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there anything wrong with my code? LALING PHP Scripting 5 02-04-2007 10:48 AM
Using PHP - Cleaning Up Code aliasxneo PHP Articles 12 12-19-2006 02:40 PM
Code wrong? Kerexx PHP Scripting 3 12-27-2005 08:06 AM
What's wrong with the code? Gustav C and C++ Programming 8 10-16-2005 04:42 PM
im a noob, what wrong with my code? zfmt C and C++ Programming 16 08-15-2005 08:44 PM