I'm having some trouble with dynamic memory allocation of a struct. The struct looks like this:

struct	J							
{
	int			w;
	int			s;	
};

During the execution of the program, the following line of code results in a crash, which when debugging was because of std::bad_alloc, which does not make any sense to me.

J* x = new J;

Any idea why this would cause a crash? It's just a simple dynamic allocation of a struct but in my program whenever it reaches that line the program crashes and shuts down.

Recommended Answers

All 2 Replies

you probably trashed memory somewhere else before that line was executed. Most likely cause is writing beyone the bounds of a buffer, but there could be lots of other reasons too.

I'm guessing this isn't the first new call in your program, which probably means that some prior allocation has been trashed.

If you have a small, AND COMPLETE program which crashes you can show us, that may be of some use.

Otherwise finding the root cause gets pretty tricky.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.