Crystal Space
Welcome, Guest. Please login or register.
August 29, 2014, 06:39:59 am

Login with username, password and session length
Search:     Advanced search
9008 Posts in 2043 Topics by 8357 Members
Latest Member: Msyexelcarl03
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  initializing the size of a csArray
« previous next »
Pages: [1] Print
Author Topic: initializing the size of a csArray  (Read 1941 times)
scandido
Newbie
*
Posts: 10


View Profile
« on: July 11, 2007, 08:05:27 pm »

Sorry if this is a dumb question but I can't seem to discern from the API exactly how to set the initial capacity for csArray. For example, if I'm trying create a n x n array of integers (and I don't want to 'Push' every value into the array), is the only appropriate method:

Code:
csArray< csArray<int> > arr;
arr.SetSize (n);
for (unsigned int i = 0; i < n; i++) arr[i].SetSize (n);

I know there are some capacity attributes but I can't seem to figure out how to use them. Any help is greatly appreciated!

- Sal
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #1 on: July 11, 2007, 09:51:55 pm »

There is no constructor that takes a size to initialize with (specifying a capacity allocates that amount of memory, but doesn't set the size itself).

So you could either do it your way, or perhaps utilize the fact that SetSize() can fill the newly added entries with some "value":
Code:
csArray<int> temp;
temp.SetSize (n);
csArray< csArray<int> > arr;
arr.SetSize (n, temp);
Logged
scandido
Newbie
*
Posts: 10


View Profile
« Reply #2 on: July 12, 2007, 02:43:13 am »

Am I correct in thinking that setting the capacity will allocate memory but will not allow access to the array until I set the size?

Is there a plan to add a constructor that allows specifying an initial size for the array or is there a design reason to exclude it?

Thanks,
Sal
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #3 on: July 12, 2007, 03:48:34 am »

Am I correct in thinking that setting the capacity will allocate memory but will not allow access to the array until I set the size?

Yes.

Is there a plan to add a constructor that allows specifying an initial size for the array or is there a design reason to exclude it?

No.

I can't think of a design reason apart from "was there first". Obviously the ctor can't be changed from taking a capacity to taking a size since that would break about all existing code using csArray<>. However it would be possible to do something like a capacity + size constructor.
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #4 on: July 12, 2007, 03:49:52 am »

No.

FWIW, "No" to "Is there a plan to add a constructor ..." - absence of such a plan on our side doesn't mean we would reject patches to do that ... Wink
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 4.176 seconds with 16 queries.