Strange errors

This is a discussion on Strange errors within the C++ Programming forums, part of the General Programming Boards category; When I run it, half the time I get "exception: bad allocation", and in the other half I get six ...

  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
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,823
    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, 08:40 AM
  4. strange errors?
    By egomaster69 in forum C Programming
    Replies: 6
    Last Post: 12-21-2004, 05:13 PM
  5. help with strange errors
    By rockdj in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2004, 11:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21