473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Finding errors in this code...

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
24 5244
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
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
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
* 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
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

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

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

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

"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

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

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

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

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
5
by: ojvm | last post by:
ok. thanks again for the time spend reading this. this code adds 2 controls in html form but it places in top of the form. i want this control1 control2 control1 control2 control1 ...
7
by: jammie_linux | last post by:
Hi, Please look at the following code. In my opinion, the value of "char *str" in the main function should not change even after calling the function "funct" and that's beacuse the "char *str" in...
13
by: dbuchanan | last post by:
This code resets a form with two cbo's (comboBoxes) and one datagrid. The first cbo (cboSelection) selects a main table and filters the second cbo. The second cbo (cboView) selects the secondary...
16
by: Sunil Varma | last post by:
Hello, Please see the following code. int main () { double da = 0.01, db; db = foo (da); } double foo (double a)
7
by: sophie | last post by:
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:
13
by: aomighty | last post by:
I'm creating a program to calculate all primes numbers in a range of 0 to n, where n is whatever the user wants it to be. I've worked out the algorithm and it works perfectly and is pretty fast,...
3
by: Ron | last post by:
I have this code: private void btnConvert_Click(object sender, EventArgs e) { // Get a directory string path = @"c:\WAMP\"; foreach (string fileName in Directory.GetFiles(path, "*.xml")) {
7
by: jharrison | last post by:
I'm finding it hard to make this code work in Python 3.0. Been looking at it for some time now: # import module for random functions import random # List of words for the computer to pick...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.