• Advertisement

Archived

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

Dynamic Allocation

This topic is 6252 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

If you have a string you want to read in but don''t know it''s size, only that it ends in a 0, and waant to store it to [char String1;] is there any way to just add one byte to the current char list? I mean like: String1 += (char*) malloc(sizeof(char)); I know this doesn''t work, I''ve tried it. Is there a way? ----------------------------- Blue*Omega (Insert Witty Quote Here)

Share this post


Link to post
Share on other sites
Advertisement
You''ll have to (not have to, but this is the easier way) find out how big it is first, then you can store it.

Or, you can read it is chunk (say, of 10 bytes), and use 2 dynamicly allocated arrays, and swap as needed to make the other bigger.

Or create a linked list style string class.



http://www.gdarchive.net/druidgames/

Share this post


Link to post
Share on other sites
The way I do this is to allocate a buffer that you think will NEVER get filled i.e. 512 chars or something.

Read the string into that buffer...
find the length of the string.
Allocate the new buffer.
And do a strcpy.

easy as pie



Regards,
Nekosion

Share this post


Link to post
Share on other sites
Thanks for the help! Nekosion, could you please explain a bit more? I think I know what your saying but would like an example.

-----------------------------

Blue*Omega

(Insert Witty Quote Here)

Share this post


Link to post
Share on other sites
Use a temp string function as previously mentioned, (as long as you know your strings won''t go over a certain length). You have a temporary string buffer and call a function to get a pointer to a string of a certain size. The buffer is circular in nature so as you grab strings from it, it will eventually start over-writing itself. An obvious disadvantage if you need a whole pile of strings all at once.

#define MAX_TEMP_STRING_SPACE (512l)


char temp_string[MAX_TEMP_STRING_SPACE];
int temp_string_free = 0;


char *TempString( int size )
{
char *p;

if ( size > MAX_TEMP_STRING_SPACE ) ... error!!!!!!!
if ( MAX_TEMP_STRING_SPACE - temp_string_free < size ) temp_string_free = 0;

p = &temp_string[temp_string_free];
temp_string_free += size;

return p;
}



There''s really nothing wrong with:

char *new_string;

new_string = (char*)malloc( strlen(old_string) + 1 );
strcpy( new_string, old_string );

Apart from memory fragmentation... but that''s a different story ;-)


---Strange

Share this post


Link to post
Share on other sites
quote:
Original post by Nekosion

The way I do this is to allocate a buffer that you think will NEVER get filled i.e. 512 chars or something.



Just be carefull.
One guy wrote OpenGL info viewer ("GLview", as I remember)
But when I tried it on Quadro with latest driver - CRASH!
I''ve found the reason after some debugging - he had a "buffer that he thought will NEVER get filled" - char [1024].
String with OpenGL extensions list for Quadro was ~1100 characters...

Share this post


Link to post
Share on other sites

  • Advertisement