Dynamic Arrays in User Defined Types - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html) +--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html) +--- Thread: Dynamic Arrays in User Defined Types (/thread-6369.html) |
Dynamic Arrays in User Defined Types - MystikShadows - 03-08-2005 Hi All, Ok let's say I have the following: Code: TYPE SomeType My Quesions are: 1. Can I have a String without a fixed lenght in a User Defined Type like what I'm doing (Knowing that this would be written to a binary file hence no specific structure or length should be needed). 2. Can I have dynamic arrays in User Defined Type definitions like I have here (who's length (ubound) would change elsewhere in the program)? 3. Could I have dymanic arrays of a type that would also have a dynamic array in it's definition? If so, how many levels deep would be allowed? Thanks all. Dynamic Arrays in User Defined Types - lkd85 - 03-08-2005 Dynamic strings are allowed, but dynamic arrays aren't. The easiest work around is to use a pointer array. This is how C/C++ does it. Code: TYPE SomeType Dynamic Arrays in User Defined Types - MystikShadows - 03-08-2005 Excellent, at least there is a way :-). Thank you Now then could I have the following: Code: TYPE SomeType Provided I do the proper initialization to everything and allocate what's needed? Dynamic Arrays in User Defined Types - VonGodric - 03-08-2005 btw if you have unfixed lenght strings in the udt's then you need to deinit them manually -or you'll have memory leaks. with yourtype.stringfield = "" Dynamic Arrays in User Defined Types - lkd85 - 03-08-2005 Yep. Code: TYPE SomeType Dynamic Arrays in User Defined Types - v3cz0r - 03-09-2005 Always use Callocate() as Allocate() won't clear the contents, so if you do bar->foo = "" (foo being a dyn string), if the contens of foo (the descriptor) aren't 0'ed, the rtlib will try to free an invalid pointer. Dynamic Arrays in User Defined Types - MystikShadows - 03-09-2005 What if I might need to change the number of elements in those PTR things should I use CAlliocate there to? or Allocate if I want what's there to stay preserved so to speak? Dynamic Arrays in User Defined Types - DrV - 03-09-2005 Reallocate * points at C realloc() function * Dynamic Arrays in User Defined Types - Dr_Davenstein - 03-09-2005 This raises a question for me too... I have no experience with pointers. :roll: Is it possible to allocate memory for these variables on the fly? Code: Type ObjType Basically, all the arrays that have 50 members... Code: Start_Id(1 to 50) as Integer ..need to be allocated to Num_Surfaces instead of 50, but I don't know how to do it. Is it even possible to do that using pointers, and how confusing would the code be? This isn't all either... I have alot of stuff in my program that would benefit from this. If it's even possible that is. Dynamic Arrays in User Defined Types - lkd85 - 03-09-2005 Yea! In fact that's how the Quake 2 MD2 structs work. Basically you would do this. Code: Type Vertex You can do that for any struct you have. That's a LOT easier than having id's to arrays if I'm reading your post correctly. Read the above posts too. |