ONA Bag

Filed in Uncategorized Comments Off

So, I spoiled myself recently and got myself The Union Street ONA bag.

So far I’m liking it, we’ll see how long that lasts. Below you can see some shots of the bag along with it being packed with the camera, lenses, laptop, and tripod.

 

 

A Simple C++ Quiz

Filed in C++ | Quizzes 1 Comment

Recently some people have been pestering me to post back up my C++ quizzes. So…without further ado here is the first one. The answers will be posted later.

  1. Given the following three lines of code, answer these questions
    int* p = new int[10];
    int* j = p + 11;
    int* k = p + 10;
    1. Is the second line well defined behavior?
    2. If the second line is well defined, where does the pointer point to?
    3. What are some of the legal operations that can be performed on the third pointer?
  2. What output should the following lines of code produce?
    int a = 10;
    std::cout<<a<<a++<<--a;
  3. Assuming the function called in the following block of code has no default parameters, and that no operators are overloaded, how many parameters does it take? Which objects are passed to it?
    f((a, b, c), d, e, ((g, h), i));
  4. Assuming the function called in the following block of code takes an A* and a B*, what is potentially wrong with the code?
    f(new A(), new B());

C++ Quiz #2

Filed in C++ | Quizzes 1 Comment

This is a test of your knowledge of C++, not of your compiler’s knowledge of C++. Using a compiler during this test will likely give you the wrong answers, or at least incomplete ones.

1. Using the code below as a reference, explain what behavior should be expected of each of the commented lines, please keep your answer very short.

struct Base {
	virtual void Arr();
};
struct SubBase1 : virtual Base {
};
struct SubBase2 : virtual Base {
	SubBase2(Base*, SubBase1*);
	virtual void Arr();
};
struct Derived : SubBase1, SubBase2 {
	Derived() : SubBase2((SubBase1*)this, this) {
	}
};

SubBase2::SubBase2(Base* a, SubBase1* b) {
	typeid(*a);                 //1
	dynamic_cast(a); //2
	typeid(*b);                 //3
	dynamic_cast(b); //4
	a->Arr();                   //5
	b->Arr();                   //6
}

2. Using the code below as a reference, explain what behavior should be expected of each of the commented lines?

template class X {
	X* p;  //1
	X a;  //2
};

C++ Quiz #3

Filed in C++ | Quizzes 1 Comment

This is a test of your knowledge of C++, not of your compiler’s knowledge of C++. Using a compiler during this test will likely give you the wrong answers, or at least incomplete ones.

Given the following code:

class Base {
public:
	virtual ~Base() {}
	virtual void DoSomething() {}
	void Mutate();
};

class Derived : public Base {
public:
	virtual void DoSomething() {}
};

void Base::Mutate() {
	new (this) Derived; // 1
}
void f() {
	void* v = ::operator new(sizeof(Base) + sizeof(Derived));
	Base* p = new (v) Base();
	p->DoSomething(); // 2
	p->Mutate();      // 3
	void* vp = p;     // 4
	p->DoSomething(); // 5
}

1. Does the first numbered line result in defined behavior? (Yes/No)
2. What should the first numbered line do?
3. Do the second and third numbered lines produce defined behavior? (Yes/No)
4. Does the fourth numbered line produce defined behavior? If so, why? If not, why?
5. Does the fifth numbered line produce defined behavior? If so, why? If not, why?

6. What is the behavior of calling void exit(int);?

Given the following code:

struct T{};
struct B {
	~B();
};

void h() {
	B b;
	new (&b) T; // 1
	return;     // 2
}

7. Does the first numbered line result in defined behavior?
8. Is the behavior of the second line defined? If so, why? If not, why is the behavior not defined?

9. What is the behavior of int& p = *(int*)0;? Why does it have that behavior? Is this a null reference?

10. What is the behavior of p->I::~I(); if I is defined as typedef int I; and p is defined as I* p;?

C++ Quiz #4

Filed in C++ | Quizzes 1 Comment

This is a test of your knowledge of C++, not of your compiler’s knowledge of C++. Using a compiler during this test will likely give you the wrong answers, or at least incomplete ones.

1. What is the value of i after the first numbered line is evaluated?
2. What do you expect the second numbered line to print out?
3. What is the value of p->c after the third numbered line is evaluated?
4. What does the fourth numbered line print?

struct C;

void f(C* p);

struct C {
	int c;
	C() : c(1) {
		f(this);
	}
};

const C obj;
void f(C* p) {
	int i = obj.c << 2;             //1
	std::cout<< p->c <<std::endl;   //2
	p->c = i;                       //3
	std::cout<< obj.c << std::endl; //4
}

5. What should you expect the compiler to do on the first numbered line? Why?

6. What should you expect the value of j to be after the second numbered line is evaluated? Why?

struct X {
	operator int() {
		return 314159;
	}
};

struct Y {
	operator X() {
		return X();
	}
};

Y y;
int i = y;     //1
int j = X(y);  //2

7. What should you expect the compiler to do on the first and second numbered lines? Why?

struct Z {
	Z() {}
	explicit Z(int) {}
};

Z z1 = 1;                 //1
Z z2 = static_cast<Z>(1); //2

8. What should you expect the behavior of each of the numbered lines, irrespective of the other lines, to be?

struct Base {
	virtual ~Base() {}
};

struct Derived : Base {
	~Derived() {}
};

typedef Base Base2;
Derived d;
Base* p = &d;
void f() {
	d.Base::~Base();    //1
	p->~Base();         //2
	p->~Base2();        //3
	p->Base2::~Base();  //4
	p->Base2::~Base2(); //5
}

TOP