#### Archived

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
// ERROR OCCURS HERE
delete data;

return 0;
}


##### Share on other sites
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

##### 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;};

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982384

• 10
• 9
• 15
• 24
• 11