Different c array declaration [duplicate] - objective-c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sizeof array clarification
I have 2 arrays declared
GLfloat gCubeVertexData[216] = { list of numbers};
and an array declared:
GLfloat *resultArray = malloc(sizeof(GLfloat) * [arrayOfVerticies count]);
for(int i = 0; i < [arrayOfVerticies count]; i++)
{
resultArray[i] = [[arrayOfVerticies objectAtIndex:i] floatValue];
}
why is it when I do sizeof(gCubeVertexData) I get 864 ( a GLflot is 4 bits so divided by 4 and you get 216)
and when I do sizeof(resultArray) I get 4? event though if I were to print out resultArray[100] I get the correct number, and there is a lot more than 4 numbers stored?

Because gCubeVertexData is an array, and resultArray is a pointer. In the case of an array, the compiler knows how many bytes it is required to allocate for the array, so it explicitly knows a size (in the case of variable-length arrays in C99, it can also be computed easily at runtime, perhaps by messing with the stack pointer).
However, in the case of malloc(), the compiler has no knowledge about the size of the memory pointed by the pointer (that size can be obtained in a non-standard and platform-dependent way only anyways...), so it just returns the size of the variable itself, which is a pointer in this case, so you'll get back sizeof(GLfloat *) in the end.

Because with sizeof(resultArray) you are getting the size of the pointer to the first element.

The type of resultArray is simply GLfloat *, i.e. "pointer to GLfloat, and your machine uses 4 characters to store a pointer. The size information associated with the pointer is not visible to the sizeof operator.
Therefore, sizeof resultArray == sizeof (GLfloat *), which is what you're seeing.

Look at the declaration of gCubeVertexData and resultArray. The first is an array with 216 elements, the latter is just a pointer. C (and thus C++ and Objective-C) allow to use pointers to access arrays, but that does not mean they have the same type.

Related

arithmetic on pointer to interface which is not a constant size for this architecture

I've taken a look at several other questions that all deal with this error, but all of the answers basically just say "don't do pointer math". The code I have is as follows:
MyObject * __strong * array = (MyObject * __strong *)calloc(sizeof(MyObject*), 5);
array[0] += 4;
I want to know why I can't do pointer math. According to the error, the MyObject* values that are pointed to are of different sizes, but that shouldn't matter, right? It's just an array of pointers - who cares how big the stuff they point to is?
I'm guessing I probably just don't understand all the nuances of ARC.
Your variable "array" is a pointer to a pointer and yes, you can consider it as an array of pointers. The size of your object "MyObject" is not constant obviously and the compiler is right not allowing you do such operation.
It is not clear what your wanted to do with your variable.
array[0] += 4;
Takes "MyObject *" from the first element of your array and want to add to it offset of 4, but to calculate such offset compilers wants to know the size of the object.
May be you wanted to do something like
array = array + 4;
or
array += 4;

Creating a dynamic 2D pointer array of NSObjects in Objective C

I want to store a 2D array of NSObjects using C pointer arrays. I read another StackOverflow question which said that it's possible to do this as follows:
id myArray [10][10];
However I want to allocate the memory dynamically because I don't know how big the table will be before hand.
I understand how to create a 2D pointer array for standard C variable types but I don't know how to do it for the id type. If I were using an int, I'd do something like this:
int ** myArray = (int**) calloc (10, sizeof(int*));
for(int i = 0; i<10; i++) {
myArray[i] = (int *) calloc(10, sizeof(int));
}
Any ideas how to do this with the id data type?
If you're using ARC (which you probably should be) creating a C array of id type objects is going to be more trouble than it's worth. You need to both calloc and free the array in the usual (non id way) and you also need to annotate the id objects like id __strong myArray = … The other thing that's really counterintuitive but is required to make sure the elements in myArray are deallocated correctly is to explicitly set each element of myArray to nil before you free myArray.
So anyway, it's a lot of trouble and there are several gotchas to work around. You should just use an NSMutableArray of NSMutableArrays. With the latest versions of llvm you can still access the arrays using "C style" syntax, like myArray[x][y] = someObject;.

Objective-C ARC and passing C arrays of objects

I'm sorry if this is a bit of a C-noob question: I know I need to swot up on my pointers. Unfortunately I'm on a deadline so don't have time to work through a whole book chapter, so I'm hoping for a bit more targeted advice.
I want to store some objective-C objects in a C array. I'm using ARC. If I were on the Mac I'd be able to use NSPointerArray instead, but I'm on iOS and that's not available.
I'll be storing a three-dimensional C array: conceptually my dimensions are day, height, and cacheNumber. Each element will either be a pointer to an objective-C object, or NULL.
The number of caches (i.e. the size of the cacheNumber dimension) is known at compile time, but the other two are not known. Also, the array could be very large, so I need to dynamically allocate memory for it.
Regarding ownership semantics, I need strong references to the objects.
I would like the whole three-dimensional array to be an instance variable on an objective-C object.
I plan to have a method that is - tableForCacheNumber:(int)num days:(int*)days height:(int*)height. That method should return a two-dimensional array, that is one specific cache number. (It also passes back by reference the size of the array it is returning.)
My questions:
What order should I put my dimensions so that I can easily return a pointer to the subarray for one specific cache number? (I think it should be first, but I'm not 100%.)
What should the return type of my method be, so that ARC doesn't complain? I don't mind if the returned array has an increased reference count or not, as long as I know which it's doing.
What type should my instance variable that holds the three dimensional array be? I think it should just be a pointer, since that ivar just represents the pointer to the first item that's in my array. Correct? If so, how do I specify that?
When I create the three-dimensional array (for my ivar), I guess I do something like calloc(X * Y * Z, sizeof(id)), and cast the result to the type for my ivar?
When accessing items from the three-dimensional array in the ivar, I believe I have to dereference the pointer each time, with something like (*myArray)[4][7][2]. Correct?
Will the two-dimensional array I return from the method be similarly accessed?
Do I need to tag the returned two-dimensional array with objc_returns_inner_pointer?
I'm sorry once again that this is a bit of a bad Stack Overflow question (it's too long and with too many parts). I hope the SO citizens will forgive me. To improve my interweb karma, maybe I'll write it up as a blog post when this project has shipped.
First off: while you don't have NSPointerArray, you do have CFMutableArrayRef and you can pass any callbacks you want for retain/release/description, including NULL. It may be easier (and performance is something you can measure later) to try that first.
Taking your points in order:
you should define your dimensions as [cacheNumber][days][height], as you expect. Then cache[cacheNumber] is a two-dimensional array of type id *[][]. As you've said performance is important, be aware that the fastest way to iterate this beast is:
for (/* cacheNumber loop */) {
for (/* days loop */) {
for (/* height loop */) {
//...
}
}
}
it should be of type __strong id ***: that's a pointer to a pointer to a pointer to id, which is the same as array of (array of (pointer to id)).
your ivar needs to be __strong id **** (!), because it's an array of the above things.
you guess incorrectly regarding allocating the array.. If you're using a multidimensional array, you need to do this (one dimension elided for brevity):
- (__strong id * * *)someArray {
__strong id * * *cache = (__strong id * * *)malloc(x*y*sizeof(void *));
id hello = #"Hello";
cache[0] = (__strong id * *)malloc(sizeof(void *)); //same for cache[1..x-1]
cache[0][0] = &hello; // for all cache[x][y]
return (__strong id * * *)cache;
}
correct, that is how you use such a pointer.
yeah, the two-D array works in the same way, sans the first dimension.
I don't think so, you're handing out __strong object pointers so you should be grand. That said, we're at about the limit of my ability with this stuff now so I could well be wrong.
Answering my own question because this web page gave me the missing bit of info I needed. I've also upvoted Graham's answer, since he was very helpful in getting my head round some of the syntax.
The trick I was missing is knowing that if I want to refer to items in the array via the array[1][5][2] syntax, and that I don't know the sizes of my array at compile time, I can't just calloc() a single block of data for it.
The easiest to read (although least efficient) method of doing that is just with a loop:
__strong Item ****cacheItems;
cacheItems = (__strong Item ****)calloc(kMaxZooms, sizeof(Item ***));
for (int k = 0; k < kMaxZooms; k++)
{
cacheItems[k] = (__strong Item ***)calloc((size_t)daysOnTimeline, sizeof(Item **));
for (int j = 0; j < daysOnTimeline; j++)
{
cacheItems[k][j] = (__strong Item **)calloc((size_t)kMaxHeight, sizeof(Item *));
}
}
I'm allocating a three dimensional array of Item *s, Item being an objective-C class. (I have of course left out the error handling code in this snippet.)
Once I've done that, I can refer to my array using the square brackets syntax:
cacheItems[zoom][day][heightToUse] = item;
The web page I linked to above also describes a second method for performing the memory allocations, that uses only one call to calloc() per dimension. I haven't tried that method yet, as the one I've just described is working well enough at the moment.
I would think of a different implementation. Unless it is a demonstrable (i.e. you have measured and quantified it) performance issue, trying to store Objective-C objects in plain C arrays is often a code smell.
It seems to me that you need an intermediate container object which we will call a Cache for now. One instance will exist for each cache number, and your object will hold an NS(Mutable)Array of them. Cache objects will have properties for the maximum days and height.
The Cache object would most easily be implemented with an NSArray of the objects in it, using simple arithmetic to simulate two dimensions. Your cache object would have a method -objectAtDay:Height: to access the object by its coordinates.
This way, there is no need at all to worry about memory management, ARC does it for you.
Edit
Given that performance is an issue, I would use a 1D array and roll my own arithmetic to calculate offsets. The type of your instance variable would be:
__strong id* myArray;
You can only use C multilevel subscripts (array[i][j][k]) if you know the range of all the dimensions (except the first one). This is because the actual offset is calculated as
(i * (max_j * max_k) + j * max_k + k) * sizeof(element type)
If the compiler doesn't know max_j and max_k, it can't do it. That's precisely the situation you are in.
Given that you have to use a 1D array and calculate the offsets manually, the Apple example will work fine for you.

Struct Pointer Not Providing the Correct Size in sizeof() method

I'm having a issue getting the size of a struct pointer after allocating the memory using malloc or realloc. I've worked around this by keeping track of the memory in a separate counter, but I would like to know if this is a bug or if there is a way to properly query the size of a struct pointer.
Sample code demonstrates that no matter how much memory I allocate to the struct pointer it always returns 4 when querying using the sizeof() method.
typedef struct {
int modelID;
int bufferPosition;
int bufferSize;
} Model;
Model *models = malloc(10000 * sizeof(Model));
NSLog(#"sizeof(models) = %lu", sizeof(models)); //this prints: sizeof(models) = 4
4 is the correct answer, because "models" is a pointer, and pointers are 4 bytes. You will not be able to find the length of an array this way. Any reason you're not using NSArray?
If I understand you correctly you want to get at the size of the allocated buffer.
sizeof if the wrong way to go since it is evaluated at compile time. The size of the buffer is a runtime concept.
You would need a way to query you C library to return the allocation size for the pointer to the buffer.
Some systems have a way to get that kind of information, for instance malloc_size on Mac OS.
4 is the correct answer.
Pointers point to a memory location which could contain anything. When you are querying the size of a pointer, it gives the size of the memory location which holds the pointer, which in your case is 4.
For example
int *a = pointing to some large number;
int *b = pointing to a single digit number;
In the above case, both a and b have the same size irrespective of where they are pointing to.
For more information, have a look at this post size of a pointer
sizeof(myvar) will return size of pointer. in 32bit environment it equals to 4(bytes).
why don't you use sizeof (Model) instead?

How to build a C array like this dynamically in a loop, return it, and keep a reference?

Right now I have this setup:
An NSMutableArray which stores two NSMutableArray instances.
A for-loop walks over a set of data and writes values into these arrays. It's for a big diagram / chart which is going to be displayed with OpenGL ES.
The NSMutableArray containing two other NSMutableArray objects is returned by a method, and the caller assigns it to a retaining property.
The pitty is this: There can be up to 2.000 values, and I don't like to create all those NSNumber objects.
Now I hope there's a lightweight way to do this with C.
Before I walk the loop I know the number of data points.
Now I want to refactor this so that I get lightweight C-arrays that hold just plain old float values. I know how to create a C-array of float values, but not really dynamically:
CGFloat values[ ] = {
0, 2.5f,
30.2f, 2.5f,
50.95f, 200.55f,
930.2f, 122.1f,
};
Questions:
1) How can I create an array like this dynamically in a loop?
2) How would I put two of those arrays into one array?
3) What about the memory managament? The method returns that multidimensional C-array, and the receiver needs to assign that to an instance variable (property). It needs to be kept around for a while. How could I create an instance variable to hold such an C-array, without knowing it's exact size in advance?
For your first question, you can dynamically create an array using malloc(), and use a pointer variable to store a reference to the first element of it. For example, this will create an array of 8 CGFloats:
CGFloat *a = malloc(8 * sizeof a[0]);
If a is non-NULL, you can then access a[0] through a[7] in the 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().
For second question, you can create an array of two pointers to the two dynamic arrays:
CGFloat *a[2] = { NULL, NULL };
a[0] = malloc(8 * sizeof a[0][0]);
a[1] = malloc(16 * sizeof a[1][0]);
You can now access a[0][0] through a[0][7] and a[1][0] through a[1][15] (as long as a[0] and a[1] are not NULL).
However, there is a wrinkle here: You cannot directly return arrays in C, so you cannot return a from a function anymore. You could instead use two levels of dynamic arrays, but it probably makes more sense to store the array of pointers within the retaining object in the first place, and pass a reference to this array to the function that fills it in.
This means that your containing object would include the CGFloat *a[2] field, and your function that allocates the dynamic arrays and fills them would include a CFloat *dest[] parameter. That function would 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 normal.
For the third question, the array that is created by malloc() will live until that pointer is later passed to free(). As mentioned, you can happily return that value and store it elsewhere, as long as you don't free() it until you're done with it.
This will get you a new array:
CGFLoat* myArray = malloc(numberOfFloat * sizeof(float));
You can do things like myArray[6] = 0;.
When you're done with it, you have to call free(myArray), or you will have a memory leak.
With this in mind, your instance variable will just be a CGFloat*.