954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Memory Allocation of a Struct

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.

r30028
Newbie Poster
6 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You