Jump to content
  • Advertisement


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


problem with STL within typedef

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

If you intended to correct an error in the post then please contact us.

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
Original post by NickB

t *data = new t[4];
delete data;

You should use delete[] when you allocate something with new[].

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!