By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Finding errors in this code...

P: n/a
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[i] = 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!!!

May 10 '06 #1
Share this Question
Share on Google+
24 Replies


P: n/a
pat wrote:
Hi everyone, I've got an exam in c++ in two days and...


The FAQ covers this question:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
"How do I get other people to do my homework problem for me?"

Checking the FAQ before posting is always good.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 10 '06 #2

P: n/a
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.

May 10 '06 #3

P: n/a
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.

May 10 '06 #4

P: n/a
* 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[i] = 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 10 '06 #5

P: n/a
pat wrote:
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.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 10 '06 #6

P: n/a

pat wrote:
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[i] = 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

May 10 '06 #7

P: n/a
> 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[i] = 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"...

May 10 '06 #8

P: n/a
On 9 May 2006 04:23:52 -0700,
pat <pa**********@gmail.com> wrote
in Msg. <11**********************@v46g2000cwv.googlegroups .com>
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
May 10 '06 #9

P: n/a
> 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.

May 10 '06 #10

P: n/a

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:4c*************@individual.net...
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[i] = 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
May 10 '06 #11

P: n/a
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
May 10 '06 #12

P: n/a

Gernot Frisch wrote:
class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)

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

May 10 '06 #13

P: n/a
Tomás wrote:
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 doubt that you can share, regularly or not, destructors between C and C++
code.

--
Salu2

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 10 '06 #14

P: n/a
* werasm:
Gernot Frisch wrote:
class demo
{
private:
unsigned char len, *dat;
public:
demo(unsigned char le = 5, unsigned char default) : len(le)


- "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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 10 '06 #15

P: n/a
BigBrian wrote:
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.


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

Best regards,

Tom

May 10 '06 #16

P: n/a

"werasm" <w_*****@telkomsa.net> schrieb im Newsbeitrag
news:11**********************@i40g2000cwc.googlegr oups.com...

Gernot Frisch wrote:
> class demo
> {
> private:
> unsigned char len, *dat;
> public:
> demo(unsigned char le = 5, unsigned char default) :
> len(le)


- "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...
May 10 '06 #17

P: n/a

Gernot Frisch wrote:
"werasm" <w_*****@telkomsa.net> schrieb im Newsbeitrag
news:11**********************@i40g2000cwc.googlegr oups.com...

Gernot Frisch wrote:

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

May 10 '06 #18

P: n/a
werasm wrote:
Gernot Frisch wrote:
Ahrgh! Right. Overseen that one. But hey - the compiler would have
told you anyway...

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.

May 10 '06 #19

P: n/a

cb******@gmail.com wrote:
werasm 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.


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

May 10 '06 #20

P: n/a

werasm wrote:
cb******@gmail.com wrote:
werasm 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.


Why not let him write some code and compile it then?


Ok, maybe he's from ... the deep amazon where they don't have a lab.
They have internet though...

May 10 '06 #21

P: n/a

cb******@gmail.com wrote:
Understanding your tools is a prerequisite for using them
effectively.


When I started out (I studied elec. eng) I tried to understand IC's (at
transistor level). Big mistake! Point is, while this may be true, one
must be wary of to what extent you understand your tool. Understand it
only as much as you need to, nothing more. We remain in disagreement,
but all is entitle to their opinion (Yes, and all should at least
consider alteration of their own every so often, else we don't learn -
I know).

Kind regards,

W

May 10 '06 #22

P: n/a

werasm wrote:
one
must be wary of to what extent you understand your tool. Understand it
only as much as you need to, nothing more.
Right, you don't need to be an electrical engineer to use a calculator.
You do, however, have to understand the math operations it is doing
for you. Your compiler may warn you that you are converting a pointer
to an integer without a cast, but that knowledge does you little good
if you don't understand the principles underlying *why* it might choose
to issue that warning.
From your other post: Why not let him write some code and compile it then?


It's fine for labs and such. When I took C, we had timed labs that
were exactly like that. But if nothing else, letting your compiler do
your thinking for you is time consuming, if nothing else, not to
mention the fact that interview test questions are a lot more like
traditional exams than anything else.

May 10 '06 #23

P: n/a

cb******@gmail.com wrote:
werasm wrote: It's fine for labs and such. When I took C, we had timed labs that
were exactly like that. But if nothing else, letting your compiler do
your thinking for you is time consuming, if nothing else, not to
mention the fact that interview test questions are a lot more like
traditional exams than anything else.


Yes, I know people who fly exams but code like ... No attention to
detail. On the other hand, I know people who struggle to express
themselves under pressure, but are really good. Exams are a flawed
mechanism and interviews relying on them are also (to a large degree).
Of course, one do get people who are good at both (writing exams and
writing code). I've been caught out by judging by exams in the past.
Point taken though.

Kind regards,

W

May 10 '06 #24

P: n/a
On 9 May 2006 11:59:45 -0700,
Thomas Tutone <Th***********@yahoo.com> wrote
in Msg. <11**********************@j73g2000cwa.googlegroups .com>
<SIGH> The lack of senses of humor in this NG continues to amaze me.


Thank you for spotting (and hopefully appreciating) my joke.

robert
May 10 '06 #25

This discussion thread is closed

Replies have been disabled for this discussion.