Archived

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

problem with STL within typedef

This topic is 5463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 12
• 10
• Forum Statistics

• Total Topics
632577
• Total Posts
3007163

×