1

I need to copy elements from uint8_t array to float array.

I wrote simple function that came to my mind instantly.

float *uint8_t_mas_to_float_mas(uint8_t *src, int size)
{
    float *dst = NULL;
    if (!src)
        return NULL;

    dst = (float*)calloc(size, sizeof(float));

    if (!dst)
        return NULL;

    for (int i = 0; i < size; i++)
        dst[i] = (float)src[i];

    return dst;
}

But I think it's not effective and unfortunatelly I cant come up with something else.

Can someone help?

Thank you.

  • 2
    Could you please define "not effective"? – Eregrith Apr 22 '15 at 12:07
  • 2
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Apr 22 '15 at 12:09
  • Sorry, **float** array not int array. – Maksim Sukhanov Apr 22 '15 at 12:14
  • 1
    Also, there is little reason to zero-fill an array who's content is about to be *completely* reassigned with value-overwrites (assuming that is the real unstated intention). `malloc(size * sizeof *dst)` should be sufficient. And you took the time to check `src` for `NULL`. May want to check `size` for `> 0` (which should be `size_t` regardless). – WhozCraig Apr 22 '15 at 12:14
  • By "not effective" I mean that this very simple and naive and maybe there is another way. – Maksim Sukhanov Apr 22 '15 at 12:16
  • @МаксимСуханов This is the correct way. You're not just "copying" data; you're transforming *values*. – WhozCraig Apr 22 '15 at 12:17
  • 1
    I am using Visual Studio, and it forces me to cast the result. – Maksim Sukhanov Apr 22 '15 at 12:17
  • Hmm, you say * Visual Studio ... forces .. to cast the result*. My own one does not. Wouldn't you source be a `cpp` file resulting in a C++ compilation ? – Serge Ballesta Apr 22 '15 at 13:05
  • @SergeBallesta My source file is `.cu`. I'm using cuda and write in C. – Maksim Sukhanov Apr 22 '15 at 13:16
  • @SergeBallesta My visual studio says: `error : a value of type "void *" cannot be assigned to an entity of type "float *"` – Maksim Sukhanov Apr 22 '15 at 13:17
  • I do not know cuda. But I suspect that MSVC compiles the file as if it was C++. Just try to put a class in it or a `new` , and I suppose it will compile it without any problem ... A C++ compiler almost accept a C source, the main difference being not allowing automatic cast of pointer to any to `void *` and back. – Serge Ballesta Apr 22 '15 at 13:46
  • @SergeBallesta Cuda project uses nvcc compiler, but yes it accepts C++ code. But still it throws error that I wrote above. – Maksim Sukhanov Apr 22 '15 at 13:50

1 Answers1

0

You should write it like this (equivalent):

float *uint8_t_mas_to_float_mas(uint8_t *src, int size)
{
    if (!src)
        return NULL;

    float *dst = (float*)malloc(size*sizeof(float));

    if (!dst)
        return NULL;

    for (int i = 0; i < size; i++)
        dst[i] = (float)src[i];

    return dst;
}

This is completely OK. Let the optimizer do it's job. The bottleneck is the malloc call - so you don't need to do anything here, unless you are copying millions of elements.

UniversE
  • 2,419
  • 17
  • 24
  • Thank you. In the comments to question WhozCraig said the same way. I think this is true. Thank you I really appretiate your help. – Maksim Sukhanov Apr 22 '15 at 13:27