poor pointer hygiene example

"USING UNITIALIZED MEMORY 'isSunny"

how do I fix this line 23 to make this example run?
this is an example from the book.
thank you



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Listing8.13.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
using namespace std;

int main()
{
	// uninitialzed pointer (bad)
	bool* isSunny;

	cout << "Is it sunny (y/n)? ";
	char userInput = 'y';
	cin >> userInput;

	if (userInput == 'y')
	{
		isSunny = new bool;
		*isSunny = true;
	}

	// isSunny contains invalide value if user entered 'n' 
	cout << "Boolean flag sunny says: " << *isSunny << endl;

	// delete being invoked also when new wasn't
	delete isSunny;

	return 0;
}
}
Move line 18 to line 11.
FYI, you have one too many closing brackets at line 30.

Either do what helios suggests:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
   bool* isSunny = new bool;
   *isSunny      = false;

   std::cout << "Is it sunny (y/n)? ";
   char userInput;
   std::cin >> userInput;
   std::cout << '\n';

   if (userInput == 'y' || userInput == 'Y')
   {
      *isSunny = true;
   }

   std::cout << "Is it sunny? " << std::boolalpha << *isSunny << '\n';

   delete isSunny;
}


You can combine the creation and intializing of any variable into one statement:
bool* isSunny = new bool(false);

I prefer a C++11 feature known as "uniform initialization" when creating a new variable:
bool* isSunny { new bool(false) };
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

You could also move all the pointer work inside the if statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
   std::cout << "Is it sunny (y/n)? ";
   char userInput;
   std::cin >> userInput;
   std::cout << '\n';

   if (userInput == 'y' || userInput == 'Y')
   {
      bool* isSunny { new bool(true) };

      std::cout << "Is it sunny? " << std::boolalpha << *isSunny << '\n';

      delete isSunny;
   }
}
Move line 18 to line 11 works! but it wasnt part of the example.
my example shows that line 18 stays.

they said error in line 23.
Last edited on
#include <iostream>
using namespace std;

int main()
{
cout << "Is it sunny (y/n)?";
char userInput = 'Y';
cin >> userInput;

// declare pointer and initialize
bool* const isSunny = new bool;
*isSunny = true;

if (userInput == 'n')
*isSunny = false;

cout << "Boolean flag sunny says: " << *isSunny << endl;

// release valid memory
delete isSunny;

return 0;
}




pointers are so hard to understand
It looks like you are using a Sams C++ book. By the look of the example code I'd say you are using the eighth edition of "Sams Teach Yourself C++ in One Hour a Day."

I have the seventh edition and 8.13 shows BAD code, creates an invalid pointer.

Some current C++ compilers get all huffy about uninitialized variables and stray/invalid pointers as well as a lot of other things. Are you using Visual Studio 2017/2019? That is one of the compilers that complain. A lot.
Just throwing something else into the mix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
  // always initialise to nullptr if there is no other option
  bool *isSunny = nullptr;

  cout << "Is it sunny (y/n)? ";
  char userInput = 'y';
  cin >> userInput;

  if (userInput == 'y') {
    isSunny = new bool;
    *isSunny = true;
  }

  // Use short-circuit evaluation.
  // If the pointer is still nullptr, then it won't be dereferenced
  cout << "Boolean flag sunny says: " << (isSunny && *isSunny) << endl;

  // delete nullptr is a safe no operation.
  delete isSunny;

  return 0;
}


The p && *p is a common way to first test a pointer is valid before going on to dereference it in some way.

You could also move all the pointer work inside the if statement:
But then there is no output when it is not sunny.
Registered users can post here. Sign in or register to post.