C++ string assignment problem

Associate
Joined
30 Oct 2004
Posts
112
Location
Whitstable
Hi everyone,

I'm having a bit of a nightmare with some code I'm trying to write in C++.

Basically, I have a structure like this:

Code:
typedef struct{
	char *name;
	string genObs;
	char *details;
	char *propAct;
	int priority;
}modelInfo;

which is contained within a variable 'void* userdef', which is part of a structure for another object. So basically I have a structure within a structure.

The problem I keep getting, is that if I assign a value to 'genObs' (i.e. assigning a string), my program keeps crashing! So far I have this:

Code:
modelInfo *mi = (modelInfo *) mav_malloc(sizeof(modelInfo));

string s = "test string";

mi->genObs = s;

In case you are wondering, the 'mav_malloc' is a specific function for the toolkit I'm using (its for creating 3D virtual environments).

I don't understand why I keep getting a crash whenever I assign a string! If I try to assign an int to the 'priority' field, I have no problems!

Any help would be appreciated!

TIA.
 
I think its because you havent given the string a length so i guess by mallocing the struct its allocating space for a string of length 1 unit as you havent specified an actual length. The int works cos ints are the same size.
 
Hi everyone,

I'm having a bit of a nightmare with some code I'm trying to write in C++.

Basically, I have a structure like this:

Code:
typedef struct{
	char *name;
	string genObs;
	char *details;
	char *propAct;
	int priority;
}modelInfo;

which is contained within a variable 'void* userdef', which is part of a structure for another object. So basically I have a structure within a structure.

The problem I keep getting, is that if I assign a value to 'genObs' (i.e. assigning a string), my program keeps crashing! So far I have this:

Code:
modelInfo *mi = (modelInfo *) mav_malloc(sizeof(modelInfo));

string s = "test string";

mi->genObs = s;

In case you are wondering, the 'mav_malloc' is a specific function for the toolkit I'm using (its for creating 3D virtual environments).

I don't understand why I keep getting a crash whenever I assign a string! If I try to assign an int to the 'priority' field, I have no problems!

Any help would be appreciated!

TIA.

Which actual line is crashing? Is it the last line?

One issue may well be that static string types are actually const char arrays, wheras the pointer that you are assigning is a plain old char pointer.
 
Code:
typedef struct{
	char *name;
	string genObs;
	char *details;
	char *propAct;
	int priority;
}modelInfo;

1) Try not to mix std::string and char *'s - Just stick with std::String's.

2) No need to typedef struct here (that comes from C) - just use struct StructName.

Code:
modelInfo *mi = (modelInfo *) mav_malloc(sizeof(modelInfo));

string s = "test string";

mi->genObs = s;

3) malloc (comes from C again) only allocates memory - it dosent construct objects, you need to use new / delete (or new[] / delete[]) and you should aways stick with these in C++. If you need to use your own allocator you should overload new on your class (or use placement new).
 
3) malloc (comes from C again) only allocates memory - it dosent construct objects, you need to use new / delete (or new[] / delete[]) and you should aways stick with these in C++. If you need to use your own allocator you should overload new on your class (or use placement new).

Yup. This is the reason why it goes wrong. The call tries to run the assignmenbt operator for the string, but there's nothing there - the internal pointer that holds the strings data isnt set, and points to garbage memory. As soon as you try and write data to it the OS barfs and kills the OP's program.

What sort of cruddy toolkit forces you to use its own malloc anyway?
 
Is the 'string' type you're using there std::string?

What, exactly, does mav_malloc do? Is it equivalent to malloc? If so, then your struct is not getting constructed, and you need to use placement new. As a rough rule of thumb, don't mix types with constructors and malloc - it's not a pretty combo.

new (mi) modelInfo();

will do the placement new.
 
Back
Top Bottom