On 3/2/15, Jens Alfke <[email protected]> wrote:
>
>> On Mar 2, 2015, at 10:51 AM, Jeff Kelley <[email protected]> wrote:
>>
>> Aren't NSNumbers now created as tagged pointers when possible? That
>> doesn't cover 100% of the difference between C arrays and NSArray, but it
>> would be a huge portion of it.
>
> Yes, but we don't know which numbers result in a tagged pointer and which
> allocate memory; that's an implementation detail. I know that in 32-bit
> processes only small integers can be stored inside pointers. In 64-bit
> there's a lot more room, of course. It's possible that 32-bit integers and
> floats could be stored that way, although of course not doubles.
>

Not to mention the fact that NSArray is also an implementation detail
and might not actually be a real array as pointed out in this old
article.

http://ridiculousfish.com/blog/posts/array.html

(And keep in mind that cache misses are usually a dominating factor in
high performance situations.)



On 3/2/15, Quincey Morris <[email protected]> wrote:
> On Mar 2, 2015, at 21:17 , Jens Alfke <[email protected]> wrote:
>>
>> The hacky C equivalent would be to put your function in a .h file, again
>> using "T" instead of "int" or "float". Then when you need the int version
>> do:
>>      #define T int
>>      #include "myfn.h"
>>      #undef T
>> Etc. for float.
>
> It seems to me this would be quite tricky to get right, because you couldn't
> have any extern functions (or any methods) that take arguments of type T or
> T*, because the resulting function names (or method selectors) would be
> duplicated. Static functions would still be possible, provided you put the
> different T versions in different source files, but that seems like it would
> escalate the duplication in other ways.
>
> I guess you could use token pasting to make function/method names unique,
> but then you'd still likely want to use macros to invoke the various names
> without too much pain.
>
> All in all, I'd agree that any rational person would do better to copy/paste
> the code instead.
>
>

I'm sure Jens was just illustrating the idea. There are slightly more
complete/elaborate macro solutions, such as the extremely license
friendly, header only, STB stretchy_buffer:
https://github.com/nothings/stb/blob/master/stretchy_buffer.h

You can study the above code or just use it if you need a resizable C
array (vector).

Example usage:

#include "stretchy_buffer.h"
#include <stdio.h>

int main()
{
        int i;
        int* int_array = NULL;
        float* float_array = NULL;

        sb_push(int_array, 1);
        sb_push(int_array, 2);
        sb_push(int_array, 3);

        sb_push(float_array, 1.1);
        sb_push(float_array, 2.2);
        sb_push(float_array, 3.3);


        for(i=0; i<3; i++)
        {
                printf("int_array[%d]: %d\n", i, int_array[i]);
        }
        for(i=0; i<3; i++)
        {
                printf("float_array[%d]: %f\n", i, float_array[i]);
        }


        return 0;
}

Output:
======
int_array[0]: 1
int_array[1]: 2
int_array[2]: 3
float_array[0]: 1.100000
float_array[1]: 2.200000
float_array[2]: 3.300000


-Eric
-- 
Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to