Finding errors in this code...

P

pat

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!!!
 
P

pat

Harsh, in fairness, I couldn't figure out what the other four errors
were and I thought someone might WANT to help. If you don't want to,
fine.

By the way, it's not a homework assignment, its a question from a past
exam paper that I couldn't figure out.
 
P

pat

Harsh, in fairness I couldn't work out what the other 4 errors were and
I thought someone who knew might WANT to help. If you don't want to
thats fine.

By the way its a question from a past exam paper not a homework
assignment. I won't gain any marks from doing it.
 
A

Alf P. Steinbach

* pat:
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!!!


The one who wrote this assignment (whatever) confused syntax errors with
semantical errors. He or she probably meant compilation errors.
 
P

Phlip

pat said:
Harsh, in fairness, I couldn't figure out what the other four errors
were and I thought someone might WANT to help. If you don't want to,
fine.

Did you read the FAQ entry?
By the way, it's not a homework assignment, its a question from a past
exam paper that I couldn't figure out.

The admonition is not against assigned homework due tomorrow. The admonition
is against copying a learner's question into a post and asking us to do all
of it.

We _like_ to discuss C++ here. Post a question about one or two items from
the "homework", including your attempt at an answer, and including your
comparison to tutorials you read, sample programs you wrote, and others'
programs which you studied.
 
D

David Resnick

pat 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:


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 am not a C++ guru. So take my hints as what they are worth. If you
want to find the syntax errors in your code and can't figure them out
via inspection, an easy way is to compile with your compiler of choice
with all error reporting enabled. Fix things one at a time and note
them down. Then try to understand them, so you can find them by
inspection, unless you can bring your compiler to your exam.

I think there are more errors than specified. I see 3 possible runtime
errors. They are a buffer overrun, a write to uninitialized memory,
and a possible but unlikely infinite loop (if, say, sizeof(char) ==
sizeof(int)). See if you can find those. I see I think 8 errors the
compiler will hit. I guess there may be more...

Your errors 2,3,4 look real. 1 is not an error, though some prefer the
style of 1 variable declaration per line. One error that is hard to
understand from the generated compiler messages is misuse of a keyword.
The one misused here is VERY common -- it is such a logical variable
name that I expect all programmers do that a few times before they
learn the lesson.

-David
 
R

Robert Latest

On 9 May 2006 04:23:52 -0700,
in Msg. said:
Harsh, in fairness, I couldn't figure out what the other four errors
were and I thought someone might WANT to help. If you don't want to,
fine.

Why don't you first try to figure out the syntax errors by looking at
the code, and then verify your result by running the code through a
program that automatically finds C++ syntax errors? Admittedly such
software can be very expensive, but maybe someone will let you use
theirs.

robert
 
G

Gernot Frisch

class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)
// compile error: default is a protected keyword
{
dat = new char[len];
for ( int i = 0 ; i <= le ; i++ )
// runtime error: assign dat[le] = default exceeds allocated range
dat = default;


// syntax error: missing '}' for c'tor
void ~demo(void)
// syntax error: no return type for d'tor
// syntax error: no argument for d'tor (might be one error?)
{
delete [] *dat;
// syntax error: delete [] dat;
}
};

class newdemo : public demo
{
private:
int *dat1;
public:
newdemo(void) : demo(0, 0)
{
*dat1 = 0;
// runtime error - assing 0 to uninitialized pointer
return 0;
// compile error: c'tor has no return value


Maybe I'm missing one "syntax error"...
 
B

BigBrian

Why don't you first try to figure out the syntax errors by looking at
the code, and then verify your result by running the code through a
program that automatically finds C++ syntax errors? Admittedly such
software can be very expensive, but maybe someone will let you use
theirs.

What? Isn't "a program that automatically finds C++ syntax errors"
called a compiler? Not all C++ compilers are expensive, many are free.
 
M

Mike Wahler

Gernot Frisch said:
class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)
// compile error: default is a protected keyword
{
dat = new char[len];
for ( int i = 0 ; i <= le ; i++ )
// runtime error: assign dat[le] = default exceeds allocated range
dat = default;


// syntax error: missing '}' for c'tor
void ~demo(void)
// syntax error: no return type for d'tor
// syntax error: no argument for d'tor (might be one error?)


A destructor parameter of (void) is not an error. It
has exactly the same meaning as (). However, most
prefer the empty argument list as a matter of style.


-Mike
 
T

Tomás

Mike Wahler posted:
A destructor parameter of (void) is not an error. It
has exactly the same meaning as (). However, most
prefer the empty argument list as a matter of style.


If I switched regularly throughout the day from writing C++ to writing C,
I'd probably use (void) instead of (), as the former has equivalent
results in both languages.


-Tomás
 
W

werasm

- "C++ Primer" by Lippman and Lajoie

The rightmost uninitialized parameter must be supplied with a default
argument before any default argument for a parameter to its left can be
supplied

Can't find this in the standard, anyone?
Maybe I'm missing one "syntax error"...

You did not mention that one :)

W
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Tomás said:
If I switched regularly throughout the day from writing C++ to writing C,

I doubt that you can share, regularly or not, destructors between C and C++
code.
 
A

Alf P. Steinbach

* werasm:
- "C++ Primer" by Lippman and Lajoie

The rightmost uninitialized parameter must be supplied with a default
argument before any default argument for a parameter to its left can be
supplied

Can't find this in the standard, anyone?

§8.3.6/4
 
T

Thomas Tutone

BigBrian said:
What? Isn't "a program that automatically finds C++ syntax errors"
called a compiler? Not all C++ compilers are expensive, many are free.

<SIGH> The lack of senses of humor in this NG continues to amaze me.

Best regards,

Tom
 
G

Gernot Frisch

werasm said:
- "C++ Primer" by Lippman and Lajoie

The rightmost uninitialized parameter must be supplied with a
default
argument before any default argument for a parameter to its left can
be
supplied

Ahrgh! Right. Overseen that one. But hey - the compiler would have
told you anyway...
 
W

werasm

Ahrgh! Right. Overseen that one. But hey - the compiler would have
told you anyway...

Jaaa, stupid exam question if you ask me. :) Also, the code in the
exam question is general stinks of bad practise - not a good example
bar the compiler/runtime errors. They could've asked how the code could
be improved obviously without design changes.

Regards,

W
 
C

cbmanica

werasm said:
Gernot Frisch wrote:
Jaaa, stupid exam question if you ask me.

I disagree. Just because the compiler can detect your errors for you
doesn't mean you shouldn't be able to pick them out yourself, any more
than one shouldn't be able to pick out an error in an arithmetic
expression even though a calculator could obviously do the work for
you. Understanding your tools is a prerequisite for using them
effectively.
 
W

werasm

I disagree. Just because the compiler can detect your errors for you
doesn't mean you shouldn't be able to pick them out yourself, any more
than one shouldn't be able to pick out an error in an arithmetic
expression even though a calculator could obviously do the work for
you. Understanding your tools is a prerequisite for using them
effectively.

Why not let him write some code and compile it then? During our exams
we had to produce a working program. We sat in the labs an compiled our
code, and looked for errors (which was of course easy to find as by
that time we had done it umpteen times). For things like these written
exams loose against practise/practical exams. Let him do it like he
would be when out in the field. BTW, who needs an exam to learn C++
anyway. I've only started learning once I started programming :) - Oh,
I actually learnt my lecturer knew less that he thought (I know that
doesn't apply to all lectures, though).

Regards,

W
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top