Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Memory overrun

Avatar of pgnatyuk
pgnatyukFlag for Israel asked on
C++
9 Comments1 Solution1156 ViewsLast Modified:
I attached a program with an obvious mistake:
int *ivar = new int(12);
instead of
int *ivar = new int[12];
Also in the end, there is line delete [] ivar;.

The program complies well by gcc 4.2.2 (Xcode) and works "fine". No warnings. No crash.  
I thought, there is only one variable in this program and so there is nothing to crash. I added few other variables - no crash at all. :( I compiled it manually in a terminal session :) - same result.

Same program in Visual Studio is complied well (no warnings), but an exception raised in the debug session -"Microsoft C++ exception: std::bad_alloc at memory location...". It was not because of the "delete [] ivar;" line. It is in the second loop, in "std::cout << ivar[k];. Actually, almost any touch, a try to read this 'ivar' in VS, causes a crash.

So this code (attached) is correct from the compiler point of view?
How you, guys, detect such memory problems, if you don't work with VS?
Is it again a problem/bug in the gcc from Apple?

#include <iostream>

int main (int argc, char * const argv[]) 
{
	int * ivar = new int(12);
	for (int i = 0; i < 12; ++i)
		ivar[i] = i;
	
	for (int k = 0; k < 12; ++k)
		std::cout << "[" << k << "] = " << ivar[k] << "\n";

	delete [] ivar;
    return 0;
}
Screen-shot-2010-07-02-at-9.53.5.png
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answers