Thread: Strange errors

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Strange errors

    When I run it, half the time I get "exception: bad allocation", and in the other half I get six 3's but the last is a garbage value. Does anyone know why? I'm using Visual C++ 2008. It works fine when size is less than 7.

    Code:
    void print_array(ostream& os, int* a, int n)
    {
    	for (int i = 0; i < n; i++) {
    		cout << a[i] << "\n";
    	}
    }
    
    int main()
    try {
    	const int size = 7;
    
    	int* p2 = new int(size);
    	for (int i = 0; i < size; i++) {
    		p2[i] = 3;
    	}
    
    	print_array(cout, p2, size);
    
    	cin.get();
    	return 0;
    }
    catch(exception& e) {
    	cerr << "exception: " << e.what() << '\n';
    	keep_window_open("q");
    	return 1;
    }
    catch (...) {
    	cerr << "Some exception\n";
    	keep_window_open("q");
    	return 2;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    int* p2 = new int(size);
    p2 points to a single int with a value of size. You want to use new[] instead, and remember to have a corresponding delete[]... but of course in this case you might as well use std::tr1::array or a raw array since size is const. If not, you should just use std::vector.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int* p2 = new int(size);
    delete p2; // Delete me!
    Create one int and initialize it with the value of size.
    Code:
    int* p2 = new int[size];
    delete [] p2; // Delete me!
    Create size ints
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sneaky little linker errors...
    By Tozar in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2006, 05:40 AM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. strange errors
    By duvernais28 in forum C Programming
    Replies: 9
    Last Post: 02-19-2005, 09:40 AM
  4. strange errors?
    By egomaster69 in forum C Programming
    Replies: 6
    Last Post: 12-21-2004, 06:13 PM
  5. help with strange errors
    By rockdj in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2004, 11:42 AM