6 Replies - 643 Views - Last Post: 26 June 2013 - 12:31 PM Rate Topic: -----

#1 JoeSaunders   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-June 13

C++ Test Help

Posted 26 June 2013 - 10:00 AM

Hey,

I am pretty new to C++ and recently did a test. Today I got the test results back however some of the test answers didn't match up to the result of the question. For instance one of the questions was:

What, if anything, is wrong with the following program?
#include <iostream.h>

int * EncryptNumber(int w)
{
	int *z_ptr = &w;
	w++;
	return z_ptr;
}

void main()
{
	int a;
	cin >> a;
	cout << *(EncryptNumber(a));
}



A ) The pointer 'z_ptr' is returned and the value of the variable it points to is displayed on the screen.
B ) The pointer 'z_ptr' is returned and its memory address is displayed on the screen.
C ) The pointer 'z_ptr' is not declared in an appropriate manner, causing the program to crash.
D ) The pointer 'z_ptr' is returned but the variable it points to goes out of scope, causing the program to crash.

I answered the question with A and it was marked as a wrong answer, however I tested the code by compiling it and it worked.

Does anyone else think the answer should have been anything other than A? If so why? Or do you agree A should be the correct answer?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Test Help

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: C++ Test Help

Posted 26 June 2013 - 10:29 AM

Quote

Does anyone else think the answer should have been anything other than A?

Yes A is not the correct answer. Remember in your function w is a copy of the data, which goes out of scope when the function exits. While your program may or may not crash, it is incorrect.

Also you should consider finding a new compiler, the one you're using is seriously outdated. The program you supplied will not compile using modern compilers.

Jim
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C++ Test Help

Posted 26 June 2013 - 10:32 AM

.

This post has been edited by tlhIn`toq: 26 June 2013 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: C++ Test Help

Posted 26 June 2013 - 11:15 AM

Well that's one of the problems with undefined behavior, sometimes it'll work but when you really need it to work it'll crash the program. Let's try a little experiment. Try running this program and show the results.

#include <iostream>

using namespace std;

int * EncryptNumber(int w)
{
	int *z_ptr = &w;
	w++;
	return z_ptr;
}

int main()
{
	int a;
	cin >> a;
	cout << a << endl;

	int *temp = EncryptNumber(a);

	cout << *temp << endl;

	cout << *(EncryptNumber(a));

	cout << *temp << endl;
}




Jim
Was This Post Helpful? 1
  • +
  • -

#5 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: C++ Test Help

Posted 26 June 2013 - 11:28 AM

Jim is correct, because the argument to the function is passed-by-value, it is just a local copy to the function. So by returning a pointer to the address of that local variable, you have undefined behavior and it's most definitely wrong to return ANY local variables by pointer or by reference.

As soon as the function returns, all of its variables including the function arguments, are popped off the runtime stack, so the pointer you have from the function may still be a valid pointer to somewhere inside your program and therefore it won't crash when used in certain ways.
Was This Post Helpful? 2
  • +
  • -

#6 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: C++ Test Help

Posted 26 June 2013 - 12:16 PM

Like the others have said, the pointer points to memory allocated on the functions stack frame. When the function returns, the stack frame is popped, which doesn't necesarrly mean it's deleted, it means that the space is fair game for any other function calls. Nonetheless, the correct answer is D, not A - however it may or may not crash, it's behavior is undefined.
Was This Post Helpful? 0
  • +
  • -

#7 JoeSaunders   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-June 13

Re: C++ Test Help

Posted 26 June 2013 - 12:31 PM

Perfect thanks guys, I understand now, I was confused because when I ran the program it worked but that doesn't mean A would be correct in every instance where as D more accurately describes how it should function because w goes out of scope once z_ptr is returned.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1