Skip to main content
gamedev.net
🔒 Locked

finding length of array

Started by krez Jan 5, 2002 at 6:42 PM 6 replies 350+ views
Original Post
krez
krez
is there any way to find the memory allocated for an array? for example, i want to use a char* as a parameter to a function, and copy some stuff into it. but, the memory is allocated and deleted outside the function. so, how can i find out if the char* was allocated with enough space (at least 10, i.e. char* feh = new char[10] ? i basically just want to copy some crap in there, but only if there is enough room for it. --- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
krez
krez
here''s the thing... i am basically making a static class function solely for the purpose of returning a text equivalent of the classes properties:

static bool toString(char* daString)
{
strcpy(m_name, daString); // m_name == name property (char*)
};

i want to check that daString is large enough to hold the text. the docs say that the destination char* for strcpy() must be allocated large enough to hold the src char*. so basically, daString must be allocated for 10 chars or i will have problems.
i guess i will just make sure i am careful when using the function. this is not a big problem, since it is for testing only, and i''m then going to add graphics when i know my calculations work. but, i was hoping, just so i could make "safe" functions in the future.
oh well. thanks anyways

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
krez
krez
ok, i though up a workaround here... which is not the original problem, but i figure it is better to ask here instead of starting a new post.

char* CLookUp::fetchStuff(int fIndex)
{
switch (fIndex)
{
case 0: return "Item 1";
case 1: return "Something else";
case 2: return "da da da";
default: return "foo";
};
};

this will return a char* based on the index you pass it... it compiles, and works... so the important part is, does anyone know if this will cause any memory problems or anything at all? i can directly
cout << CLookUp::fetchStuff(1); 
, so i''m not allocating any memory... no need to delete[] anything...
so, is there anything bad that i am missing?

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Null and Void
Null and Void
That works, but this is safer (and faster ):
  
char *CLookUp::fetchStuff(int fIndex) {
static char *Table[] = {
"Item 1",
"Something else",
"da da da",
"foo"
};
};
return Table[fIndex];
}

Since those strings have to be in memory all the time anyway (otherwise the char pointer returned wouldn''t be valid) making it static clarifies the point. Making it into a table just makes it easier to return by index.

[Resist Windows XP''s Invasive Production Activation Technology!]
EvilCrap
EvilCrap
you could write your own string gimmic, where the first char is the length of the memory. when you wanted work with the string, you would always send &string[1].
the string would need to be less than 255 chars where 256 = NULL, for use with standard funcs.

  
char* Str = new char[20];
Str[0] = 20;
strcpy(&Str[1], "Hello World!!\0";
cout << &Str[1];


its kinda like a lazy struct...
and you can always write an accessor func to clean up ur code.
  
char* str(char* Str)
{
return &Str[1];
}


Edited by - evilcrap on January 6, 2002 9:51:45 AM
EvilCrap
EvilCrap
OR BETTER YET, write your own string class.
OR BETTER YET, use the std string class.
OR BETTER YET, write a string class better than the std one, and use that one.

why use a class? because you want to screw with 2 peices of data... and that calls for a direct association, a class.
krez
krez
well, as i said this is for debugging my combat calculations, after which i''m getting rid of it all anyways and moving it over to graphics. i considered making a class, or using STL string, or making my own string class... but it isn''t that important. thanks though
NULL and VOID: thanks for the "static" tip and the table idea!

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.