Page 1 of 1

Dynamic Memory allocation example

PostPosted: Fri Mar 08, 2013 9:50 pm
by Fojam
So I was bored one day, and I was like "hey why don't I mess around with game editor a bit?" So I decided to make a String object, (which was one of my major beefs with game editor is the fact you cant make classes) and so I created a struct called string

I made it so that it dynamically allocates memory for the string. only problem is, I think I found a weird bug in game-editor with the strlen(char*) function

I made two examples of strings. one using linked memory addresses, and the other using a dynamically resizing array.

Please tell me what you think and what is the problem with strlen (if you know)

Edit: to clarify, I am not "back". I just thought I would make a little utility in c, and see if it worked in game-editor

Re: Dynamic Memory allocation example

PostPosted: Sat Mar 09, 2013 2:08 am
by skydereign
Fojam wrote: I think I found a weird bug in game-editor with the strlen(char*) function

Normally in this kind of post you'd say what the problem you think you found is...
http://www.cplusplus.com/reference/cstring/strlen/ wrote:size_t strlen ( const char * str );

Get string length
Returns the length of the C string str.

The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

Re: Dynamic Memory allocation example

PostPosted: Sat Mar 09, 2013 3:04 am
by Fojam
See that's the thing. I have no idea how to explain it! Just run it and watch it for a minute

Re: Dynamic Memory allocation example

PostPosted: Sat Mar 09, 2013 3:16 am
by skydereign
Fojam wrote:See that's the thing. I have no idea how to explain it!

You can at least explain the symptoms. Otherwise you wouldn't know it is a problem. How long must I stare at it? I switched the fps to 200 and watched it for a while, and nothing has changed.

Re: Dynamic Memory allocation example

PostPosted: Sat Mar 09, 2013 12:32 pm
by Fojam
I meant to say calloc, but the bug is that random characters, a lot of time it will be text from the code, will flash after hello world. This happens on both of them.

Re: Dynamic Memory allocation example

PostPosted: Wed Mar 13, 2013 8:00 pm
by Fojam
Ah that was totally my bad! it's not a bug, I just forgot to null terminate the string.