• entries
146
435
• views
198844

# A Short and Relatively Simple Quiz

1075 views

1. Given the following three lines of code, answer these questions
int* p = new int[10];
int* j = p + 11;
int* k = p + 10;

• Is the second line valid?

• If the second line is valid, where does the pointer point to?

• What are some of the legal operations that can be performed on the third pointer?

2. What output should the following line of code product?
int a = 10; std::cout<

3. Assuming the function called in the following block of code has no default parameters, 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 wrong with the code?
f(new A(), new B());

1)a) The second line is valid, but j will not be defined.
b) It points to the byte that is sizeof(int) bytes after the end of the array that p points to. (Edit: changed answer. Note that I've defined the end as being immediately after the last bit of p[9])
c) Dunno. Subtracting the pointer?

2) The result is undefined, and varies from compiler to compiler. It depends on when the compiler evaluates parameters.

3) 4. And if I remember the comma operator stuff you taught me...the parameters will be a, d, e, and g.

4) (stolen from jpetrie [smile]): You will leak if the second new (whichever one is second isn't defined) throws.

1a. yes.
1b. Past the end of the array declared in the first line

2. undefined?

3. 4 parameters; c, d, e, i

4. Either 'new' could throw an exception, as such you could be left with a memory leak.

Quote:
2nd'd. I choose not to answer for fear of embarassing myself. That and leaving a permanent record of my stupidity.

Jack

Quote:
 I choose not to answer for fear of embarassing myself. That and leaving a permanent record of my stupidity.

Not a problem for me because I have in the past and it existed already [wink]

EDIT: So I had a huge brainfart when answering 1b. Shoot me. Jack was right, after all [lol]

Ok, without looking at other people's answers (honest! you can verify that by looking at my incorrect answers [grin]):

1.
· Yes, the second line is valid.
· The pointer points to one location past the end of the array.
· Comparison against either of p and j, and the usual pointer arithmetic.

2. Undefined by the standard.

3. 4 parameters: c, d, e, i.

4. I don't see anything wrong. Even though we're passing unnamed objects, the function can store pointers to the allocated memory of both A and B objects in static or global variables.

I'm working w/ the assumption that sizeof(int) == 4;

As such, new int[10] creates a block of memory of 40 bytes.

Edit: Doh on #1.

1)
a) Yes this is "legal", you are pointing to the 3rd byte of the 3nd int. &p[2]+3
b) Yes this is "legal", you are pointing to the 2nd byet of the 2nd int. &p[2]+2
c) Any normal pointer and int operations are possible, but you will be modifying an offset of the 2 ints. So you effect the lower byte of the 3rd int &p[2] and the upper 3bytes of the 4th int, &p[3]

2) will display
10109
3) 4 params,
a,d,e,g
4) Probably a Memory Leak, generally functions don't take reponsiblity for memory management of passed pointers.

1) Yes
one sizeof(int) beyond the end of the allocated memory (skipping one sizeof(int))
std::sort(p, k)

2) 101010

3) 4 parameters, c, d, e, i

4) Since you don't keep pointers to the allocated memory, you're going to get memory leaks. (Unless f deletes everything passed to it, I suppose.)

Okay, I'll give it a go then. But I'm not looking forwards to the results [grin]

1a) Yes, pointer arithmetic
1b) Two past the end of the array. +9 would be the last element (zero based), +10 is one-past the end and +11 is two past..
1c) Pointer arithmetic, outputting the address but not dereferencing.

2) "101010". 'a' outputs "10", 'a++' outputs "10" and increments it to 11, "--a" decrements it to 10 and then outputs it.

3) Haven't got a clue to be honest. Wasn't even aware that was valid code [embarrass]

4) Memory leaks. Calling code doesn't have the resultant pointers to use delete on, and the function is unlikely/shouldn't be designed to delete it's parameters. Also, but not sure, some funky stuff with the order and timing of construction for A() and B().

Jack

heh, i have a feeling i'm going to get a few wrong but...

1) The second line is syntatically valid but semantically wrong. The second line would point to one memory address "slot" past p. Legal operations on the third? -- and ++?

2) 10910; a is printed out as is first, then --a does it's magic and makes it 9 and since a++ executes last, it goes back to 10.

3) 4; and I don't know which ones get called.

4) I don't know

I object to 1(a) and 1(b) on the grounds that "valid" is not well-defined in this context. 1(c) is okay, since "legal" operations (those allowed by a standards-conforming compiler) are well-defined.

1.)(a) Yes.
1.)(b) p + 11*sizeof(int)
1.)(c) Dereferencing the pointer would be legal, although it wouldn't give you anything useful.

2.) Not defined. "<<" does not delineate sequence points, so you're still in the same sequence point until the ";".

3.) The comma operator has left-to-right associativity and parentheses take precedence over commas. Thus, only the outermost and rightmost commas count in each block: f(c, d, e, i).

4.) Besides the memory-leak problem other people alluded to, the order of argument evaluation is left up to implementers, so that if A's constructor depends on information constructed by B (say, some static data), disaster is likely to result. That would be bad programming practice to create such an unwieldy dependency, but there's a reason sites like Daily WTF exist. =)

1. a)Yes.
b)Two after the array p is pointing to.
c)None, really. It is out of bounds for the array p.

2. "101010", if, as I believe, they are done left to right.
Otherwise it is "9910"(but I'll probably stick with my first answer)

3. I know it would be 4, but I'm going to go with the first poster and say that they are a,d,e,g

4. Toughy. Don't really know that one.

1.
a) yes!
b) yes it is valid, the pointer points to the integer just past the allocated array
c) This may be used in a test to see whether a pointer is out of bounds(like in an std::vector!)

2. 10910(10, then 9, then 10). I am probably horribly wrong here.

3. well I vaguely recognise this syntax so I'll bite. It will take 4 parameters: a, d, e, g

4. An obvious memory leak if the function does not call delete which would not be good practice I don't think. I can't see anything else which is incorrect here.

1. Yes it's "valid". It will point to the *second* cell block of memory after the end of p. P + 1 is the second element of the array, so since there are 10 elements, P + 9 would be the end.

2. "Legal" would be anything I'd supposed you could do with pointers. You could allocate new memory to that location and over write a previous allocation of memory, but it'd still be legal due to how the block head would still start at the correct position.

3. 101010 from the looks of it.

4. It would have 4 parameters:
(a, b, c),
d,
e,
((g, h), i)

I'm thinking the actual objects passed to it are c, d, e,and i.

ALTHOUGH you never specified that there was no use of macros, so
(a, b, c) could have been #define (a, b, c) a, b, c and etc... [wink]

5. In C++, you cannot have an empty () for a default ctor like you can and should in Java?

# Given the following three lines of code, answer these questions
int* p = new int[10];
int* j = p + 11;
int* k = p + 10;

* Is the second line valid?
Yes.

* If the second line is valid, where does the pointer point to?
p + 11*sizeof(int).

* What are some of the legal operations that can be performed on the third pointer?
Keyword being 'some'. Assuming some means 'more than one', but not 'all', pick any two that don't dereference the pointer.

# What output should the following line of code product?
int a = 10; std::cout<<a<<a++<<--a;
This is expanded to cout.operator<<(int).operator<<(int).operator<<(int), more or less. It will print '101010'.

# Assuming the function called in the following block of code has no default parameters, how many parameters does it take? Which objects are passed to it?
f((a, b, c), d, e, ((g, h), i));
If operator, returns it's first argument, a,d,e,g; if it's last, c,d,e,i. I don't know which.

# Assuming the function called in the following block of code takes an A* and a B*, what is wrong with the code?
f(new A(), new B());
The allocations may be made in any order. The compiler may even do both allocations, then call both constructors. If an exception is thrown by an allocation, one may leak. If an exception is thrown by one of the constructors, one or both allocations may leak.

1a: yes, the second line is valid
1b: whatever p points to, PLUS (sizeof(int) * 11)
1c: --k, ++k, cast it, assign a value to the memory location the pointer points to

2: i am inclined to say 101010 although i'm not entirely sure on operator precedence. might as well be 10910 (right-to-left operator precedence)

3: 4 passing: c, d, e, i[/edit]

4: resources are never freed (assuming no GC is running)

So, I haven't read the replies just in case. I already know 1 and 2, since you've only been going on about those for half the morning.

3. I am gonna go with 4 parameters on that one. I'm guessing what's passed to it is c, d, e, i.

4. Again I already know the answer, cause we went over this in IRC.

The answers as I see them.

1. Given the following three lines of code, answer these questions
int* p = new int[10];
int* j = p + 11;
int* k = p + 10;

• The behavior is undefined. Hence it is not valid, as per section 5.7 of the holy standard

• Since the behvaior is undefined, we cannot say anything about the state of the program, nor universe anymore.

• We cannot dereference the pointer, as it points one past the end, however it is a legal pointer (as per 5.7). We can perform subtraction operations against this pointer, along with comparison operations.

2. This line of code has two problems. The first is that the order in which function parameters are evaluated is unspecified. As such, a, a++, and --a can all be processed in any order. However, there is a bigger problem. We are not allowed to modify the same object more than once between sequence points, yet in this case, we have done so twice. As such the behavior of this code is undefined. It could legally obliterate the universe.

3. The function takes 4 parameters, the parameters are c, d, e, and i. The , operator evaluates from left to right, returning the right most type. This is not to be confused with the , used in specifying function parameters, hence why we had to surround them by ()'s.

4. This function call has several problems. The first is fairly obvious: If either of the two news succeedes, and the second one fails, then we leak memory. Furthermore we may also be relying on the construction order (and hence the order in which function parameters are evaluated).

3. The paramaters of fcould be anything if operator, is overloaded for any of the types involved. All you can say for sure is that there's four of them.

I like those red books. They look pretty when you have a lot of them :)

Quote:
 Is the second line valid?

Yes.

Quote:
 If the second line is valid, where does the pointer point to?

Out of range of the pointer p.

Quote:
 What are some of the legal operations that can be performed on the third pointer?

Pointer arithmetic.

Quote:
 What output should the following line of code product?

10910

Quote:
 # Assuming the function called in the following block of code has no default parameters, how many parameters does it take? Which objects are passed to it?

4, c,d,e,i

Quote:
 Assuming the function called in the following block of code takes an A* and a B*, what is wrong with the code?

1a) Syntactically yes, but runtime errors may result if data is written to the memory location pointed to by j.

1b) (2 * sizeof (int)) bytes past the end of the array pointed to by p.

1c) Syntactically pointer arithmetic, dereference, increment, decrement… but runtime errors may result if data is written to k and k points to a memory location outside of the array.

2) 101010

3) It takes exactly 4 parameters: c, d, e, i

4) Default constructors are not invoked by using empty brackets ().

## Create an account

Register a new account