Subtle Bugs in c++ hunt

J

jimspace

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

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

#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 = 'q';
}
return 0;
}

// --------------------------------------------------
 
D

daniel.w.gelder

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
 
L

Larry I Smith

jimspace said:
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;
}

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

#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 = 'q';
}
return 0;
}

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


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
 
D

daniel.w.gelder

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

puzzlecracker

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.
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.
THIS IS IRRELEVANT AND SELFISH.
 
J

John Carson

puzzlecracker said:
School, college, homework is individual endeavor whether he cheats,
plagiarized, etc., should NOT concern others, lest we should care
about it.


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

John Carson

jimspace said:
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.


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.
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top