How to build a C array, how is it dynamically in a loop, return it and save the link?

Now I have this setting:

NSMutableArray, which stores two instances of NSMutableArray.

A for-loop moves through a dataset and writes values ​​to these arrays. This is for a large chart / chart to be displayed using OpenGL ES.

An NSMutableArray containing two other NSMutableArray objects is returned by the method, and the caller assigns it to the save property.

The eggs are this: there can be up to 2,000 values, and I do not like to create all these NSNumber objects.

Now I hope there is an easy way to do this with C.

Before I go through the loop, I know the number of data points.

Now I want to reorganize this to get lightweight C-arrays that contain only old float values. I know how to create a C array of float values, but not very dynamically:

CGFloat values[ ] = { 0, 2.5f, 30.2f, 2.5f, 50.95f, 200.55f, 930.2f, 122.1f, }; 

Questions:

1) How can I create such an array dynamically in a loop?

2) How would I put two arrays in one array?

3) How about memory management? The method returns this multidimensional C-array, and the recipient needs to assign it to an instance variable (property). It needs to be kept for a while. How can I create an instance variable to store such a C-array without knowing its exact size?

+4
source share
2 answers

For your first question, you can dynamically create an array using malloc() and use a pointer variable to store a reference to its first element. For example, this will create an array of 8 CGFloat s:

 CGFloat *a = malloc(8 * sizeof a[0]); 

If a not NULL, you can access a[0] through a[7] usual way. You can return this CGFloat * value to another function, and when you are done with the array, you must pass this value to free() .

In the second question, you can create an array of two pointers for two dynamic arrays:

 CGFloat *a[2] = { NULL, NULL }; a[0] = malloc(8 * sizeof a[0][0]); a[1] = malloc(16 * sizeof a[1][0]); 

Now you can access a[0][0] through a[0][7] and a[1][0] through a[1][15] (as long as a[0] and a[1] not NULL )

However, there is a wrinkle here: you cannot directly return arrays to C, so you can no longer return a from a function. Instead, you can use two levels of dynamic arrays, but it probably makes sense to store an array of pointers inside the holding object in the first place and pass a reference to this array to a function that fills it.

This means that your containing object will contain the CGFloat *a[2] field, and your function that allocates dynamic arrays and populates them will include the CFloat *dest[] parameter. This function will start with something like:

 dest[0] = malloc(8 * sizeof dest[0][0]); dest[1] = malloc(16 * sizeof dest[1][0]); 

... and then fill in the values ​​as usual.

For the third question, the array created by malloc() will live until this pointer is passed to free() . As already mentioned, you can happily return this value and save it in another place if you are not free() until you are done with it.

+3
source

This will give you a new array:

 CGFLoat* myArray = malloc(numberOfFloat * sizeof(float)); 

You can do things like myArray[6] = 0; .

When you are done with this, you must call free(myArray) , or you will have a memory leak.

Given this, your instance variable will just be CGFloat* .

+3
source

All Articles