Expanding an array within a structure in C - objective-c

I've got a question about what I think boils down to C syntax and memory considerations. I have a callback in an Objective-C class that processes some audio, and one of the arguments is bufferListInOut. It represents a mono audio signal that I'd like to convert to stereo. Here's my code:
static void tap_ProcessCallback(MTAudioProcessingTapRef tap,
CMItemCount numberFrames,
MTAudioProcessingTapFlags flags,
AudioBufferList *bufferListInOut,
CMItemCount *numberFramesOut,
MTAudioProcessingTapFlags *flagsOut){
// Retrieve mono audio data into bufferListInOut
MTAudioProcessingTapGetSourceAudio(tap, numberFrames, bufferListInOut, NULL, NULL, NULL);
// Copy the mono channel's data
float *channelLeft = malloc(numberFrames * sizeof(float));
channelLeft = bufferListInOut->mBuffers[0].mData;
// Attempt to create a second channel which is (currently) a copy of the first
bufferListInOut->mNumberBuffers = 2; // 2 for stereo, 1 for mono
bufferListInOut->mBuffers[1].mNumberChannels = 1;
bufferListInOut->mBuffers[1].mDataByteSize = numberFrames * sizeof(Float32);
bufferListInOut->mBuffers[1].mData = channelLeft;
// Set number of frames out
*numberFramesOut = numberFrames;
}
Some notes:
In this code, the new channel is just a copy of the original, but in practice it will undergo some processing.
The naming is a little weird, but mNumberBuffers is indeed meant to be 2 and mNumberChannels is meant to be 1.
This crashes with an EXC_BAD_ACCESS error on a rendering thread down the line. So, my question is what is the right way to add a new buffer to this struct? I don't delve into C too often, so I'm sure I'm missing some basics here. Any pointers on this would be great. Thanks for reading.

You cannot do what you are attempting, at least in the way you are trying to do it. The type AudioBufferList is declared as:
struct AudioBufferList { UInt32 mNumberBuffers; AudioBuffer mBuffers[1]; };
This is a classic C pattern for a variable sized struct. To create a struct for N buffers a single block of memory is allocated with the size:
sizeof(UInt32) + N * sizeof(AudioBuffer)
The address of that block is assigned to a AudioBufferList * variable and the field mNumberBuffers set to N. You cannot simply increase the mNumberBuffers to increase the size of the array, instead you must either allocate a new complete struct, or realloc the existing one - realloc increases the size of memory block if possible or allocates a new one and copies the existing data into it if not.
Given your tap_ProcessCallback() function is passed a AudioBuuferList * value and does not return one, there is no way it can change the size of the struct (i.e. the number of buffers) it is passed.
As pointed out in comments you are also doing pointer assignment when you intend to copy memory - see memcpy and friends.
So you need a redesign - your goal is possible, just not how and where you've attempted it.
HTH

Related

Why putting pointer* for Strings not for Integers in Objective-C

I learnt Swift firstly , maybe this should have been after learning Obj-C, so now I trying full understand Obj-C, I have trouble with issue below :
Obj-C variables
NSInteger score = 556;
NSString *name = #"matt";
in Swift everything is same for variables
var score = 556
var name = "matt"
Why objective-c use for strings * although integers are not * using, Why there is a such a different ? It is related run time ?
Can you explain to me with practical examples ? Thanks
You know, without pointers Objective-C is just Swift with semicolons.
Suppose you need to allocate a region of memory for your own use. You can store integers there, or an image, or a dictionary of objects or a sound clip. The pointer is your handle on that piece of memory. It tells you primarily the location of the region of memory you got, but in Objective-C, it also keeps track of e.g. a reference count so that you do not need to worry about allocating and releasing it. So things can get pretty complex pretty quick.
Let us assume the simplest possible scenario. You need some memory to store an object. Say a string, but that does not matter. Also, to all the purists out there, this is a general discussion not meant to be technically correct, but to illustrate the idea to OP.
First you will alloc that object
NSString * p = [NSString alloc];
This will ask the OS for a region of memory of the correct size to store a string and the OS will respond with a pointer that points to the newly allocated area, that address now stored in p. This is just an area of memory and you can do what you want with it. You need not even store a string there but things can go pear shaped pretty quick if you wander off like that.
Now the OS just gave you a region of memory. That memory is not blank, it holds some left over bits from whatever it was used for before. So your first task is to clear the memory - to initialise it. So the next step is to initialise it
p = [p init];
After this the area of memory has been prepared by some initialiser, e.g. it was zeroed or loaded some default values into the area of memory for a more complex class.
Note in reality this is a bit different - see the edit below.
In C you have alloc and malloc and calloc to do this. Some of these will just allocate memory and others will allocate the memory and set it all to 0 - clearing all the bits. In Objective-C you typically do this as a single step
p = [[NSString alloc] init];
Since the OS allocated a region of memory you need to tell it when you are done to free it, e.g.
[p release];
or in C you'd use free when done.
Now this is just academic, as ARC will do this automatically for you by keeping an internal reference counter to the object to determine when it goes out of scope and then to release it, but I digress. Let us just for now assume you need not worry about releasing that block of memory back to the OS, ARC will take care of it for you.
Let us also for now assume a more complex class, say
p = [[Invoice alloc] init];
Then when you e.g. do
p.items = 100;
the compiler will know that it needs to copy the integer value of 100 to the correct place inside that block of memory and it will do so for you. There are many variations on this theme, but basically as you assign values to ivars those values gets copied into the allocated area of memory and as you read them they get copied back from the correct spot into the local variable as well.
On the other hand, if you assign a value to a simple int variable
int items = 100;
this is not copied to your pre-allocated area. In stead, the message or function has a special local working area called a stack where these variables are created and later discarded dynamically. So the statement above will load the value 100 onto the stack and will note the position so that later if you refer to items it will know where to find and read or write its value on the stack.
Likewise
Invoice * p = [[Invoice alloc] init];
is loaded on the stack, but here the pointer's value or memory address is loaded on the stack, not the contents it points to. The fact that it is a pointer indicates to you that it points to some region of interest somewhere else.
If later you do
Invoice * q = p;
you load (on the stack) the same pointer value of p into q, and now p and q both point to the same area of memory where the actual object is stored. So pointers add this one layer of separation between the value of the pointer that can be interpreted as pointing to an address in memory, and the data that is stored at this address. This can go on, e.g. you can and do get pointers to pointers and so on.
Since you can not allocate memory yourself on the dynamic stack, you can not store objects on the stack, but you can store pointers to objects there.
Pointers give you incredible power as you access memory directly. You can e.g. write a general function to allocate memory or to write memory to disk or to clear memory or to transfer a block of memory through the network or to load a block of memory to a screen area. Modern compilers take care of these things for us so it does not sound like such a big deal, but the powerful thing is that you use the same routine for all your objects. If your array stores pointers then you can store anything in the array as another example.
Pointers also outlive the stack. The stuff you put on the stack e.g.
int items = 100;
only survives as long as you are inside the function. The stack is discarded when the function returns so you need a specially allocated area of memory to communicate in a way that transcends functions and sometimes even applications.
Here is a deeper example, using only integer pointers, not Objective-C object ones, to further illustrate the difference between pointers and values on the stack.
In C you'd do
int * p = malloc ( 10 * sizeof( int ) );
which will allocate enough space to store 10 integers and return the pointer to the allocated space in p. p is now on the stack and contains the address of the area of memory just returned.
Next if you do
int q = 123;
the value 123 is stored onto the stack and under the moniker q which the compiler will translate to this specific location on the stack as you work with q e.g. assign a value to it.
Next, if you do
* p = q;
* ( p + 1 ) = q / 2;
this will load the value of q (123) from the stack and into the area of memory pointed to by p, which points to the first of the 10 integers. Next you calculate q / 2 and store that value into the location of the next integer, the 1 after the first, pointed to by p and referenced by * ( p + 1 ). The compiler can do this easily as it knows the size of an integer so it knows how to translate
* ( p + 1 )
into the correct area that points to the integer shifted 1 away from the base p.
This is another topic with pointers, it allows for incredibly efficient pointer arithmetic that you can use to traverse areas of memory. Also, this is why sizeof is such an important keyword in C as it is directly linked to how the language handles memory through pointers.
In C, when done, you need to release the area back to the OS so you'd do
free( p );
when done.
This seems simple enough, but in practise it is difficult to keep track of all the allocated regions of memory. It easily happens that you do not release one or that you load data into an area already released. The former leaks memory and the latter can have disasterous consequences.
This is why Objective-C's ARC represents such a big step in the development of the language as it takes care of this for you. In the ARC world the allocation and freeing of memory, a BIG topic in pointers, is no longer that important or visible - so it can also shield some understanding of pointers from its users.
In e.g. Swift this allocation and freeing happens completely behind the scenes and you do not really worry about allocating or freeing memory directly. This makes it difficult to understand the difference between pointing to memory and the memory being pointed to, which is at the heart of pointers.
You know, without pointers Objective-C is just Swift with semicolons.
I started and now ended with this somewhat controversial statement.
Why?
When I was a programmer struggling with the concept of pointers, I had a mind to give it up completely. This was before objects and you could (try to) do all you needed to on the stack and side step pointers that way.
I just came from Pascal (where you do not use pointers that much) and struggled with C, especially with pointers, and then read somewhere that, without pointers, C is just Pascal with curly braces! I knew C was powerful, more so than Pascal, but to grasp for that I had to master pointers.
Note - as mentioned by Sulthan, Pascal also has pointers and it is very debatable if one is more powerful than the other.
Here is a nice sample to illustrate a lot of these concepts, also how you can use pointers to iterate through memory.
int main(int argc, const char * argv[]) {
#autoreleasepool
{
// insert code here...
int a = 10; // a loaded on stack
int * b = & a; // b now points to a
NSLog ( #"a pointer %p value %d", & a, a );
NSLog ( #"b value %p points to %d", b, * b );
int * p = malloc ( 10 * sizeof ( int ) );
int * q = p;
// Show p on the stack and * p allocated somewhere
// Note the values, & p agrees with & a of earlier e.g.
NSLog ( #"p address %p on stack but points to %p", & p, p );
for ( int i = 0; i < 10; i ++ )
{
* q = i; // loads value i into area pointed to by q
NSLog ( #"i now %d", i );
// Note the huge difference between addresses on the stack and addresses alloc'd
NSLog ( #"\tp %p p + i %p value pointed to is %d", p, p + i, * ( p + i ) );
NSLog ( #"\tq %p value pointed to is %d", q, * q );
q ++; // Increment pointer, not value, now points to next int
}
// Of course ...
free ( p );
}
return 0;
}
EDIT Objective-C alloc
I mention in the text that the memory needs to be initialised after an Objective-C alloc. I could not find any documentation now, but I am pretty sure that Objective-C's alloc, e.g. something like
NSString * s = [NSString alloc];
will in fact both allocate and clear or zero the memory. The init is not really meant to zero but for the class to initialise its members properly.
But, since we are discussing pointers and since you are looking for programming examples, what better than to write a piece of code to test this.
The code below will create two Objective-C classes of your choice. One will only be alloc'd and one will be alloc'd and init'd. Then it compares these two classes byte for byte using pointers to see if there is a difference. Another nice example of using pointers.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
// The object we will test
#define TEST_OBJECT NSFileManager
int main(int argc, const char * argv[]) {
#autoreleasepool {
// Allocate some complex class
TEST_OBJECT * obja = [TEST_OBJECT alloc];
TEST_OBJECT * obji = [[TEST_OBJECT alloc] init];
// Size of this class
unsigned long n = class_getInstanceSize ( TEST_OBJECT.class );
NSLog ( #"Object size is %lu", n );
NSLog ( #"\tLocation in memory %p", obja );
NSLog ( #"\t %p", obji );
// Get pointers to the two instances
void * p = ( __bridge void * ) obja;
void * q = ( __bridge void * ) obji;
// Compare the two
int cmp = memcmp( p, q, n );
NSLog ( #"Result is %d - %#", cmp, cmp ? #"different" : #"same" );
// Dump the actual bytes side by side
for ( int i = 0; i < n; i ++ )
{
int l = ( unsigned ) ( * ( ( unsigned char * ) p + i ) ); // Left
int r = ( unsigned ) ( * ( ( unsigned char * ) q + i ) ); // Right
NSLog ( #"Byte %3d | %3d | %3d | %#", i, l, r, l != r ? #"***" : #"" );
}
}
return 0;
}
If you understand the difference between a value type and a reference type in Swift, then in Objective-C using a pointer simply denotes a reference type.
During assignments, either the value is copied:
// swift
var score = 10
// obj-c
NSInteger score = 10;
or the reference is copied, or, in other words, we are not assigning the value itself but the pointer to the value:
// swift
class MyObject {}
var object = MyObject();
// obj-c
#interface MyObject: NSObject
#end
MyObject *object = [[MyObject alloc] init];
Also note that in Objective-C string (NSString), arrays (NSArray) and similar are all reference types. Even numeric types which are value types (NSInteger) have reference type wrappers (e.g. NSNumber) because otherwise you cannot put them into an array. Even null value has a corresponding reference type NSNull.
Of course, even with value types you can use a pointer:
NSInteger score = 10;
NSInteger *scorePointer = &score; // assign pointer to score
*scorePointer = 20; // change value of score through the pointer
NSLog(#"%#", #(score)); // 20
In the same way you can also have pointers to other pointers, which is useful in many situations (e.g. inout method parameters).

What is the meaning of "float *newAudio"

Sorry for my newbie question. Please refrain from down voting me because I am learning C. I am an experienced programmer on other languages, but not on C. I am trying to learn C at the same time I am trying to understand a library for iOS called Novaine, on Github.
This library has a module with the following signature:
Novocaine *audioManager = [Novocaine audioManager];
[audioManager setInputBlock:^(float *newAudio, UInt32 numSamples, UInt32 numChannels) {
}];
So, the internal block is receiving numSamples, numChannels and newAudio and newAudio is of kind float pointer?
What kind of object is this? an array of floats? if this is an array, how do I access its values? How do I know the number of elements it has?
This is a pointer to float value. Nothing strange here. It is often use to point some area in memory. You don't know the size of this are. Might be a single float but also can be larger, continuous space in memory.
You don't know what type of object is stored there. float doesn't mean that floats are stored there. It could be declared as void * as well. Again, it is just a space in memory. By typing float * you just give the compile a hint that when you move newAudio pointer (doing or example newAudio ++) it will move a pointer by a sizeof(float) number of bytes.
Based on method fingerprint I assume that this is a pointer to the first element of some buffer which size is probably numSamples * numChannels * size of single sample
This part of memory should be allocated first, to make sure that it's reserved for you:
float *newAudio = calloc(numSamples * numChannels, sizeof(float));

Pointer to specified number of values

How can I specify that a method should take as parameter a pointer to a location in memory that can hold a specified number of values? For example, if I have:
- (void)doSomethingWith:(int *)values;
I'd like to make it clear that the int * passed in should point to an allocated space in memory that's able to hold 10 such values.
To directly answer your question, use an array argument with a bounds, e.g.:
- (void)takeTenInts:(int[10])array
Which specifies that the method takes an array of 10 integers.
Only problem is the C family of languages do not do bounds checking, so the following is valid:
int a[10], b[5];
[self takeTenInts:a]; // ok
[self takeTenInts:b]; // oops, also ok according to the compiler
So while you are specifying the size, as you wish to do, that specification is not being enforced.
If you wish to enforce the size you can use a struct:
typedef struct
{
int items[10];
} TenInts;
- (void)takeTenInts(TenInts)wrappedArray
Now this doesn't actually enforce the size at all[*], but its as close a you can get with the C family (to which the word "enforcement" is anathema).
If you just wish to know the size, either pass it as an additional argument or use NSArray.
[*] It is not uncommon to see structures in C following the pattern:
typedef struct
{
// some fields
int data[0];
} someStruct;
Such structures are dynamically allocated based on their size (sizeof(someStruct)) plus enough additional space to store sufficient integers (e.g. n * sizeof(int)).
In other words, specifying an array as the last field of a structure does not enforce in anyway that there is space for exactly that number of integers; there may be space for more, or fewer...
Why use "(int *)" when you have the power (and "count") of "NSArray" to work with?
But anyways, looking at this potentially related question, couldn't you just do a "sizeof(values)" to get the size of a statically/globally allocated pointer?
If that doesn't work (which would be in the case of a dynamically allocated array), you really would probably need some kind of "count:" parameter in your "doSomethingWith:" method declaration.
There are a several ways. You could just name the method appropriately:
- (void)doSomethingWithTenInts:(int *)tenInts;
Or you could use a struct:
typedef struct {
int values[10];
} TenInts;
- (void)doSomethingWithTenInts:(TenInts *)tenInts;
Or you could make the user tell you how many ints he is giving you:
- (void)doSomethingWithInts:(int *)ints count:(int)count;

Replace array with another array in C

Out of pure curiosity, I started playing with array's in ways that I have never used before. I tried making a data structure array, and set it equal to another:
typedef struct _test {
float value;
} test;
Simple enough struct, so I tried this:
test struct1[10];
test struct2[20];
struct1 = struct2;
I didn't think this would work, and it didn't even compile. But, this interests me a lot. Is it possible to take an array of 10 and increase the size to 20, while copying the data?
Objective-C
I am actually doing this with Objective-C, so I'd like to hear from the Objective-C people as well. I want to see if it is possible to change the size of struct1 in this file.
#interface Object : NSObject {
test struct1;
}
Remember: This is only out of curiosity, so everything is open to discussion.
Something else that is not exactly pertinent to your question but is interesting nonetheless, is that although arrays cannot be assigned to, structs containing arrays can be assigned to:
struct test
{
float someArray[100];
};
struct test s1 = { /* initialise with some data*/ };
struct test s2 = { /* initialise with some other data */ };
s1 = s2; /* s1's array now contains contents of s2's array */
This also makes it possible to return fixed-length arrays of data from functions (since returning plain arrays is not allowed):
struct test FunctionThatGenerates100Floats(void)
{
struct test result;
for (int i = 0; i < 100; i++)
result.someArray[i] = randomfloat();
return result;
}
As others have said, arrays allocated like that are static, and can not be resized. You have to use pointers (allocating the array with malloc or calloc) to have a resizable array, and then you can use realloc. You must use free to get rid of it (else you'll leak memory). In C99, your array size can be calculated at runtime when its allocated (in C89, its size had to be calculated at compile time), but can't be changed after allocation. In C++, you should use std::vector. I suspect Objective-C has something like C++'s vector.
But if you want to copy data between one array and another in C, use memcpy:
/* void *memcpy(void *dest, const void *src, size_t n)
note that the arrays must not overlap; use memmove if they do */
memcpy(&struct1, &struct2, sizeof(struct1));
That'll only copy the first ten elements, of course, since struct1 is only ten elements long. You could copy the last ten (for example) by changing &struct2 to struct2+10 or &(struct2[10]). In C, of course, not running off the end of the array is your responsibility: memcpy does not check.
You can also you the obvious for loop, but memcpy will often be faster (and should never be slower). This is because the compiler can take advantage of every trick it knows (e.g., it may know how to copy your data 16 bytes at a time, even if each element is only 1 byte wide)
You can't do this in C with static arrays, but you can do it with dynamically allocated arrays. E.g.,
float *struct1, *struct2, *struct3;
if(!(struct1 = malloc(10 * sizeof(float))) {
// there was an error, handle it here
}
if(!(struct2 = realloc(struct1, 20 * sizeof(float))) {
// there was an error, handle it here
// struct1 will still be valid
}
if(!(struct3 = reallocf(struct2, 40 * sizeof(float))) {
// there was an error, handle it here
// struct2 has been free'd
}
In C, I believe that's a good place to use the realloc function. However, it will only work with dynamically allocated arrays. There's no way to change the memory allocated to struct1 by the declaration test struct1[10];.
In C arrays are constants, you can't change their value (that is, their address) at all, and you can't resize them.
Clearly if you declare your array with a fixed size, test struct1[10] then it cannot be resized. What you need to do is to declare it as a pointer:
test *struct1;
Then you must use malloc to allocate the array and can use realloc to resize it whilst preserving the contents of the original array.
struct1 = malloc(10*sizeof(*struct1));
//initialize struct1 ...
test *struct2 = realloc(struct1, 20*sizeof(*struct1));
If you're using Objective C, you know you can just use NSMutableArray, which automatically does the realloc trick to reallocate itself to store however many objects you put in it, up the limit of your memory.
But you're trying to do this with struct? What would that even mean? Suppose you increase the amount of memory available to struct1 in Object. It's still a struct with one member, and doesn't do anything more.
Is the idea to make Object be able to contain an expanded struct?
typedef struct _test2 {
float value;
NSObject *reference;
} test2;
But then you still can't access reference normally, because it's not a known part of Object.
Object *object2;
...
NSLog(#"%#", object2.struct1.reference); // does not compile
If you knew you had one of your modified objects, you could do
Object *object2;
...
NSLog(#"%#", ((test2)(object2.struct1)).reference);
And also you could still presumably pass object2 to anything that expects an Object. It only has any chance of working if struct1 is the last member of Object, and don't mess with subclassing Object either.
Some variety of realloc trick might then work, but I don't think realloc in particular, because that's intended to be used on objects that are allocated with malloc, and the details of what C function is used to allocate objects in not exposed in Objective C, so you shouldn't assume it's malloc. If you override alloc then you might be able to make sure malloc is used.
Also you have to watch out for the fact that it's common in Objective C for more than one pointer to an object to exist. realloc might move an object, which won't be semantically correct unless you correct all the pointers.

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?