Connecting Tech Pros Worldwide Forums | Help | Site Map

Subtle Bugs in c++ hunt

jimspace
Guest
 
Posts: n/a
#1: Jul 23 '05
Can someone look at each of the classes below and tell me where the
subtle bugs are? Thanks.


Handle/Body idiom

class A {
private:
int i;
public:
void fct() const;
}

// --------------------------------------------------

class A {

public:
A() { };
~A() { };

virtual void fct() { };
};

// --------------------------------------------------

class A {
// ...
};

class B : public A {
// ...
};

void fct( A a ) { /* ... */ };

int main() {
B b;
fct( b );
};

// --------------------------------------------------

#include <math.h>
#include <stdio.h>

int main() {
for( int i=0; i<10; i++ ){
printf( "%i %f \n", i, sin( ((double)i)*M_PI ) );
}
return 0;
}

// --------------------------------------------------

class A {
public:
void fct() { /* ... */ };
};

int main() {
A *a = new A();
a->fct();
delete a;
}

// --------------------------------------------------

class A {
// ...
};

A& fct() {
A a;
// ...
return a;
}

// --------------------------------------------------

#include <stdio.h>

int main() {
char c;
while( (c=getchar()) != EOF ){
putchar( c );
}
}

// --------------------------------------------------

class A {
private:
int upper;
int lower;

public:
A( int i ) : lower( i ), upper( lower+1 ) { };
};

// --------------------------------------------------

int a[10000];

void f() {
int i;

for( i=0; i<10000; i++ );
a[i] = i;
}

// --------------------------------------------------

#include <stdlib.h>

void extend( char *p ) {
char *tmp = (char *)malloc( 20*sizeof(char) );

if( tmp ){
free(p);
p = tmp;
}
}

int main() {
int i;

char *p = (char *)malloc( 10*sizeof(char) );
if( p ){
extend( p );
}

for( i=0; i<20; i++ ){
p[i] = 'q';
}
return 0;
}

// --------------------------------------------------


daniel.w.gelder@gmail.com
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Subtle Bugs in c++ hunt


I shouldn't do your homework for you but I remember getting this test
on a job interview and not passing, so I will.

1. Semicolon
2. Can't copyconstruct A from B
3. I don't know, usage?
4. Semicolon!
5. C.R.I.F.T.
6. usage?
7. backwardsosity
8. ;
9. referenceize

You're welcome

Larry I Smith
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Subtle Bugs in c++ hunt


jimspace wrote:[color=blue]
> Can someone look at each of the classes below and tell me where the
> subtle bugs are? Thanks.
>
>
> Handle/Body idiom
>
> class A {
> private:
> int i;
> public:
> void fct() const;
> }
>
> // --------------------------------------------------
>
> class A {
>
> public:
> A() { };
> ~A() { };
>
> virtual void fct() { };
> };
>
> // --------------------------------------------------
>
> class A {
> // ...
> };
>
> class B : public A {
> // ...
> };
>
> void fct( A a ) { /* ... */ };
>
> int main() {
> B b;
> fct( b );
> };
>
> // --------------------------------------------------
>
> #include <math.h>
> #include <stdio.h>
>
> int main() {
> for( int i=0; i<10; i++ ){
> printf( "%i %f \n", i, sin( ((double)i)*M_PI ) );
> }
> return 0;
> }
>
> // --------------------------------------------------
>
> class A {
> public:
> void fct() { /* ... */ };
> };
>
> int main() {
> A *a = new A();
> a->fct();
> delete a;
> }
>
> // --------------------------------------------------
>
> class A {
> // ...
> };
>
> A& fct() {
> A a;
> // ...
> return a;
> }
>
> // --------------------------------------------------
>
> #include <stdio.h>
>
> int main() {
> char c;
> while( (c=getchar()) != EOF ){
> putchar( c );
> }
> }
>
> // --------------------------------------------------
>
> class A {
> private:
> int upper;
> int lower;
>
> public:
> A( int i ) : lower( i ), upper( lower+1 ) { };
> };
>
> // --------------------------------------------------
>
> int a[10000];
>
> void f() {
> int i;
>
> for( i=0; i<10000; i++ );
> a[i] = i;
> }
>
> // --------------------------------------------------
>
> #include <stdlib.h>
>
> void extend( char *p ) {
> char *tmp = (char *)malloc( 20*sizeof(char) );
>
> if( tmp ){
> free(p);
> p = tmp;
> }
> }
>
> int main() {
> int i;
>
> char *p = (char *)malloc( 10*sizeof(char) );
> if( p ){
> extend( p );
> }
>
> for( i=0; i<20; i++ ){
> p[i] = 'q';
> }
> return 0;
> }
>
> // --------------------------------------------------
>[/color]

You mean other than the fact the code has (at least)
multiple defs for 'main()' and 'class A', and memory
bugs in 'extend()'

Your question makes no sense.
Is this your homework?

Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
daniel.w.gelder@gmail.com
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Subtle Bugs in c++ hunt


Pretty good how Larry saw the code bugs before he even considered the
context of the question. I think it's safe to assume it's homework.

puzzlecracker
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Subtle Bugs in c++ hunt



School, college, homework is individual endeavor whether he cheats,
plagiarized, etc., should NOT concern others, lest we should care about
it. I DO NOT. LET HIM DEAL WITH HIS STUFF ON HIS OWN --DON'T BE A
JUDGE, lest you want to judged.
[color=blue]
>Pretty good how Larry saw the code bugs before he even considered the
>Context of the question. I think it's safe to assume its homework.[/color]
THIS IS IRRELEVANT AND SELFISH.

John Carson
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Subtle Bugs in c++ hunt


"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:1115237676.569895.152790@f14g2000cwb.googlegr oups.com[color=blue]
> School, college, homework is individual endeavor whether he cheats,
> plagiarized, etc., should NOT concern others, lest we should care
> about it.[/color]


Tripe. If you help him cheat, you are as guilty as him, in the same way that
assisting a bank robber makes you guilty of a crime. Even if you don't help
him cheat, then his cheating should concern you because it compromises the
validity of the certification process (degrees etc.) on which society
relies.

--
John Carson

jimspace
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Subtle Bugs in c++ hunt


Oops, I should have explained the context of my initial posting a
little more. I'm preparing for a technical interview, and just wanted
to compare my answers to the questions I found on
http://www.beyondcode.org/articles/interview.html

Cheers,
Jim.

John Carson
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Subtle Bugs in c++ hunt


"jimspace" <jimspace@gmail.com> wrote in message
news:1115274801.846325.108310@z14g2000cwz.googlegr oups.com[color=blue]
> Oops, I should have explained the context of my initial posting a
> little more. I'm preparing for a technical interview, and just wanted
> to compare my answers to the questions I found on
> http://www.beyondcode.org/articles/interview.html
>
> Cheers,
> Jim.[/color]


Fair enough. The intent of my remarks was to address the general principles
involved rather than to make any judgement about your particular case.
My form of words probably suggested otherwise, for which I apologise.

--
John Carson

Closed Thread


Similar C / C++ bytes