Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

C

WhoWho IndiaMember Posts: 1

Hi, I need to explain a piece of code which I don't understand how it works, and this is very important for me because I cannot move if I will not understand this basic thing.

If __binaries_ is a pointer to array of pointers to char array, then what exactly does the line with memory allocation:

size_t * binary_sizes;
char ** binaries;
binaries = (char **)malloc( sizeof(char *)*nDevices );

can you describe to me to understand the proccess of memory allocation?

The code then continues:

    int i;
    for( i = 0; i < nDevices; i++ )
    {
        if( binary_sizes[i] != 0 )
        {
            binaries[i] = (char *)malloc( sizeof(char)*binary_sizes[i] );
        }
        else
        {
            binaries[i] = NULL;
        }
    }

nDevices is 18, but I cannot understand what happens there.

Tagged:

Comments

  • WhoWho IndiaMember Posts: 1

    I wondered why anybody would allocate memory 18x for every character, but this is something different. I think I start to understand it. There is yet one more command which is there before the binaries are initiated:

    [code]binary_sizes = (size_t )malloc( sizeof(size_t)nDevices );[/code] ... and then a function is called to get the sizes of binaries and to save them in the binary_sizes array. So the binaries contains chains of chars or char arrays, but this does not mean they are 18. Though, there is 18 pointers, only 1st element has length of 11000, but the rest of elements have 0 length. Also from the function for sizes detection and from the binary_sizes array it is obvious, that it is not array which would have elements of equal length, but every element can be different length.

Sign In or Register to comment.