This topic is now archived and is closed to further replies.


problem with STL within typedef

Recommended Posts

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
	delete data;

	return 0;

Share this post

Link to post
Share on other sites
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;

Share this post

Link to post
Share on other sites