Trying to parse some text ( using C++ )

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Cristian
Posts: 55
Joined: Fri Nov 25, 2005 12:02 pm

Trying to parse some text ( using C++ )

Post by Cristian »

I need to get a block of text into memory and for that i'd use
text=(char**)realloc(text, no_lines*sizeof(char*));
text[no_lines]=(char*)malloc(max_line_width*sizeof(char));

however when I try

text=(char**)realloc(text, 8*sizeof(char*));
text[0]="test";

it crashes. does anyone know an efficient method of parsing
a block of text, please?

EDIT. Nevermind. solved. Forgot to properly allocate from the very start!
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

first up you shouldn't use malloc/calloc/realloc, you should use 'new' and 'delete' in c++:

Code: Select all

c8 **arr = new c8*[size];
delete [] arr;
even better, try irrlicht's string and array classes, they manage all the memory for you and the end result is much cleaner nicer looking code (at the cost of a few cycles)

Code: Select all

  array<string<c8> > text;
  text.set_used(no_lines);
  text[0] = "some text which, ";
  text[0] += "unlike a const pointer can be edited later";
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
execute
Posts: 14
Joined: Wed Nov 22, 2006 7:48 pm

Post by execute »

Or you can just allocate memory normally:
char* somechar = ParseText(text);
char achar[sizeof(somechar)+1];
strcpy(achar, somechar);

Well it's just trial and error with pointers if it gets too complicated.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, dynamic arrays are not supported by most compilers, so simply avoid it. You should put in a constant only into your array size: [constant]
Post Reply