Finding errors in this code...

S

sophie

Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

Identify 6 syntax and 2 possible runtime errors in this code:


class demo
{

private:
unsigned char len, *dat;


public:

demo(unsigned char le = 5, unsigned char default) : len(le)
{
dat = new char[len];
for ( int i = 0 ; i <= le ; i++ )
dat = default;

void ~demo(void)
{
delete [] *dat;
}
};

class newdemo : public demo
{

private:
int *dat1;

public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;
return 0;
}
};


I'm pretty sure I have 4 but am not sure if they are syntax or runtime
errors...

1 - line 5, "unsigned char len, *dat;" should be two seperate
declarations.
2 - line 16, "void ~demo(void)", can't have return type for destructor.
3 - line 18, " delete [] * dat", shouldn't be a * here.
4 - line 32, "return 0;", constructor can't have return type.


Would anyone be able to tell me what the rest of the errors are cause i
haven't a clue, and does anyone know if the above are runtime or syntax
errors and if there is an easy way to differentiate between the two.
Any help would be great, thanks!!!
 
V

Vladimir Oka

sophie said:
Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

If you can't tell C and C++ apart, you will likely fail.
Our next door neighbours in comp.lang.c++ may help you, though.

Followups set...
Identify 6 syntax and 2 possible runtime errors in this code:


class demo
{

private:
unsigned char len, *dat;


public:

demo(unsigned char le = 5, unsigned char default) : len(le)
{
dat = new char[len];
for ( int i = 0 ; i <= le ; i++ )
dat = default;

void ~demo(void)
{
delete [] *dat;
}
};

class newdemo : public demo
{

private:
int *dat1;

public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;
return 0;
}
};


I'm pretty sure I have 4 but am not sure if they are syntax or runtime
errors...

1 - line 5, "unsigned char len, *dat;" should be two seperate
declarations.
2 - line 16, "void ~demo(void)", can't have return type for destructor.
3 - line 18, " delete [] * dat", shouldn't be a * here.
4 - line 32, "return 0;", constructor can't have return type.


Would anyone be able to tell me what the rest of the errors are cause i
haven't a clue, and does anyone know if the above are runtime or syntax
errors and if there is an easy way to differentiate between the two.
Any help would be great, thanks!!!
 
I

Ian Collins

sophie said:
Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.
I assume you get a fail for posting a C++ question to a C group....
 
R

Richard Heathfield

sophie said:
Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

Identify 6 syntax and 2 possible runtime errors in this code:

foo.c:1: parse error before `demo'
foo.c:2: syntax error before `{'
foo.c:20: warning: ANSI C does not allow extra `;' outside of a function
foo.c:22: parse error before `newdemo'
foo.c:34: warning: ANSI C does not allow extra `;' outside of a function
 
R

Robert Latest

On 9 May 2006 03:44:52 -0700,
in Msg. said:
Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

Topicality issues apart; the fact that you have a C++ exam suggests that
you have been attending a C++ class. Why don't you just apply the
knowledge gained there?

robert
 
A

Andrew Poelstra

Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

Identify 6 syntax and 2 possible runtime errors in this code:

You've already been chewed out for posting invalid code of
a completely different language.
[code snipped]

I'm pretty sure I have 4 but am not sure if they are syntax or runtime
errors...

1 - line 5, "unsigned char len, *dat;" should be two seperate
declarations.
2 - line 16, "void ~demo(void)", can't have return type for destructor.
3 - line 18, " delete [] * dat", shouldn't be a * here.
4 - line 32, "return 0;", constructor can't have return type.
None of those errors make sense in C, except for the first one,
which is a style issue, not a code issue. Or maybe things are
different in C++.
Would anyone be able to tell me what the rest of the errors are cause i
haven't a clue,
No. If we assume it is C, there are too many to post; if we assume
it is c++, we will send you to comp.lang.c++ to ask for help there.
and does anyone know if the above are runtime or syntax
errors and if there is an easy way to differentiate between the two.

I'll give you a hint: runtime errors show up when you are _running_
the program, and syntax errors show up when you are _compiling_ the
program. If you didn't understand that, then I'm afraid that you'll
need to pay more attention when you redo the course.
 
M

Martin Ambuhl

sophie said:
Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

You really want to post questions to the appropriate newsgroup. The
programming language C++ is not the programming language C, and it has
its own newsgroup said:
Identify 6 syntax and 2 possible runtime errors in this code:

Your code has a lot more than 8 errors if it is C.
 
C

cbmanica

Crossposted to comp.lang.c++, followups set.
Hi everyone, I've got an exam in c++ in two days and one of the past
questions is as follows.

Identify 6 syntax and 2 possible runtime errors in this code:

I would call them "compile-time" errors myself.
class demo
{

private:
unsigned char len, *dat;

Nope, nothing wrong with this line.
public:

demo(unsigned char le = 5, unsigned char default) : len(le)

You've got a compile time error here...
{
dat = new char[len];

....and here...
for ( int i = 0 ; i <= le ; i++ )
dat = default;


....a runtime error here...

....and another compile-time error here...
void ~demo(void)

....another compile error here (you got this one)...
{
delete [] *dat;

....and here (again you got it)...
}
};

class newdemo : public demo
{

private:
int *dat1;

public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;

....and another runtime error here...
return 0;

....and a compile-time error here (which you got).

Assuming I didn't make a mistake, that should be all of them.
I'm pretty sure I have 4 but am not sure if they are syntax or runtime
errors...

It's a pretty elementary distinction.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top