Thread: sizeof(): why the problem?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    32

    sizeof(): why the problem?

    I just want to prevent the user from entering too large a value for my integer variable "max". Even a max == 300 triggers the warning. When I removed these warning statements, I could do well with max == 300 or larger. Could you please help? Thanks a lot!

    Code:
    // Exercise 12 - Chapter 4
    // Find all prime numbers between 1 - max,
    // where, max is given by the user 
    // Primes found using the simpliest method
    
    /*
    Given n, keep dividing n by 2, 3, ..., (n-1). If divisible by any of these,
    then it's not a prime. Otherwise, it's a prime. By definition, 1 is not
    a prime, and 2 is the only even prime.
    */
    
    #include "std_lib_facilities.h"
    #include <cmath>
    
    bool is_prime(int);
    
    int main()
    {
    	cout << "Enter a terminal number to which to find all";
    	cout << " prime numbers from 1: ";
    	int max;
    	cin >> max;
    	if(max > sizeof(max)) {
    		cout << "Number exceeding defined ranged!\n";
    	}
    	
    	while (true) {
    		cout << "Do you want to try again (Y/N): ";
    		string choice;
    		cin >> choice;
    		if (choice == "Y" || choice == "Yes" || choice == "yes" || choice == "y") {
    			cout << "Enter a terminal number to which to find all";
    			cout << " prime numbers from 1: ";
    			cin >> max;
    			break;
    		}
    		else if (choice == "N" || choice == "No" || choice == "no" || choice == "n") {
    			return 0;
    		}
    	}
    	
    	
    	vector <int> myprimes;
    	
    	for (int i = 1; i <= max; ++i) {
    		if (is_prime(i)) {
    			myprimes.push_back(i);
    		}
    	}
    	
    	for (unsigned i = 0; i < myprimes.size(); ++i)
    		cout << myprimes[i] << endl;
    }
    
    bool is_prime(int x)
    {
    	bool found;
    	
    	if (x < 2)
    		found = false;
    	if (x == 2)
    		found = true;
    	
    	for (int i = 2; i < x; ++i) {
    		if ((x % i) == 0) {
    			found = false;
    			break;
    		}
    		else
    			found = true;
    	}
    	return found;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    sizeof results in the number of bytes an object of its operand, or operand's type, takes up.

    In this case, it should suffice to check the state of the stream after cin >> max. Strictly speaking, this results in undefined behaviour in the case of an overflow, but it appears to work nicely on the systems I tested.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by laserlight View Post
    sizeof results in the number of bytes an object of its operand, or operand's type, takes up.

    In this case, it should suffice to check the state of the stream after cin >> max. Strictly speaking, this results in undefined behaviour in the case of an overflow, but it appears to work nicely on the systems I tested.
    Sorry, but I don't get it. I'm still at a very elementary level (I'm learning C++ entirely by self-study as a first programming language and using the book "Programming: Principles and Practice using C++" by BS). I'm now at Chapter 4 (Control Statements).

    What I want is somehow use some built-in function to determine the max value that an integer can take, say, an integer in C++ is assumed to take up to a maximum value of N. I don't want to refer to the reference and fill in the value itself, just want to call some function. How can I do this?

    Thanks a lot for your time and help!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Even if sizeof() worked the way you think it does, the code still makes no sense. You seem to be treating sizeof(max) as the maximum value that could ever be achieved by the max variable. But if that's so, then how could the comparison "max > sizeof(max)" ever possibly be true? It's basically saying, "If the value of max is greater than the largest possible value of max" which is asking for the impossible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    32
    Quote Originally Posted by brewbuck View Post
    Even if sizeof() worked the way you think it does, the code still makes no sense. You seem to be treating sizeof(max) as the maximum value that could ever be achieved by the max variable. But if that's so, then how could the comparison "max > sizeof(max)" ever possibly be true? It's basically saying, "If the value of max is greater than the largest possible value of max" which is asking for the impossible.
    Thank you very much. It's very clear to me now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 03:54 PM