problem with STL within typedef

Started by
2 comments, last by NickB 20 years, 6 months ago
Hi, I seem to be having some trouble getting the code below to work correctly; In this code I''ve got a typedef for a structure including an STL string. I''m creating an array of the structures & using them, but when run in Debug mode & it gets to the point of deleting the array I get a error saying that a debug assertion has failed with "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)". Can anyone help me with this as it is rather irritating & I don''t understand it. btw I''m using VS.NET 7.0 pro to compile with. Regards Nick B

#include <string>
#include <iostream>

typedef struct {
	float			a;
	std::string		b;
} t;

int main(int argc, char* argv[])
{
	// alloc some data
	t *data = new t[4];

    // demo that string works okay 
	// (alloc appears to have worked correctly)
	data[0].b = "hello";
	std::cout << data[0].b << "\n";

    // delete the string
	// ERROR OCCURS HERE
	delete data;

	return 0;
}
 
Advertisement
quote:Original post by NickB
	t *data = new t[4];	...	delete data;  

You should use delete[] when you allocate something with new[].
Thanks for that
Also, FYI, since you''re using std::string, you''re obviously compiling a C++ program and because of that, you don''t need to use typedefs with structs. In C++, a struct is defined the same as a class, so in your case you could write this:

struct t {  float a;  std::string b;};

This topic is closed to new replies.

Advertisement