Home>

Hello.

In order to create an array that can dynamically allocate memory in C, I wrote a simple code referring to a site.
However, there are a number of unclear points, and I don't understand the logic of "what we do and why."
I'd like to understand this reason, but I'd like to know it, but it is unclear mainly in the following points.


Whole process

  1. (Initialization) Store the type, number, number of reservations, and memory I want to put in the array in the structure.
  2. (Add to array) First, secure the memory area required for the entire array? Then is memcpy copying additional data to an empty location in memory?
  3. (Read) Returns the address of the element I want to read ...? ? ?

Questions

    Is the
  1. _group_offset function a code to reserve a memory area in advance? However, the theory of reading the value with this function is unknown.
  2. Is the address copied by memcpy in the
  3. _group_assign function? However, since memcpy should be transcribed in order from the first character string of offset, isn't the order of addition reversed?

# include<stdlib.h>
#include<string.h>
#include<stdio.h>

typedef struct Group {
    size_t size;
    size_t capacity;
    size_t element_size;
    void * data;
} Group;

static void * _group_offset (Group * group, size_t index)
{
      / * Unknown part 1 (I don't know what i am doing) * /
    / * Just return 40 byte block + (index * 4 byte) ...? * /
    return group->data + (index * group->element_size);
}
static void _group_assign (Group * group, size_t index, void * element)
{
    / * Unknown part 2 (The copied offset is left untouched, but then the value can be accessed ???) * /
    / * General pointer with 40 byte block return * /
    void * offset = _group_offset (group, index);
    / * copy element to offset for group->element_size * /
    memcpy (offset, element, group->element_size);
}
int main ()
{
    Group group;
    group.size = 0;
    group.capacity = 10;
    / * 4 bytes * /
    group.element_size = sizeof (int);
    / * Allocate a 40-byte block (reserved holding number * int type byte size) * /
    group.data = malloc (group.capacity * group.element_size);
    int a = 8;
    int b = 9;
    int c = 23;
    int d = 93232;
    int e = 914444;
    int f = 95555;
    _group_assign (&group, group.size,&a);
    / * Increased to one group * /
    group.size ++;
    _group_assign (&group, group.size,&b);
    group.size ++;
    _group_assign (&group, group.size,&c);
    group.size ++;
    _group_assign (&group, group.size,&d);
    group.size ++;
    _group_assign (&group, group.size,&e);
    group.size ++;
    _group_assign (&group, group.size,&f);
    group.size ++;
    / * It is unknown why the value of the corresponding position can be acquired * /
    / * Why is 93232 returned here? ? ? * /
    int answer = * (int *) _ group_offset (&group, 3);
}
c
  • Answer # 1

      

    1. Is the _group_offset function a code to reserve a memory area in advance?

    No. Allocating memory is done with malloc in the main function.
    This is a function that returns the address of the memory to be accessed.

    /* 40 bytes block + (index * 4 bytes) ... just return? * /
    return group->data + (index * group->element_size);

    No.
    Calculates the memory address that is indexed backward from the start address.

      

    2. Is the address copied by memcpy in the _group_assign function?

    The data actually placed at the specified address.

      

    But memcpy should be transcribed in order starting from the first character string of offset.

    memcpy (copy destination, copy source, copy length)


    For the time being, try limiting the data type to int.
    The code is complicated because of the use of void pointers.

  • Answer # 2

      Is the

    _group_offset function a code to reserve a memory area in advance?

    No. Just calculating the address,not securing memory.

      Is the address copied by memcpy in the

    _group_assign function?

    No. Copying the contents given aselement.

  • Answer # 3

      Is the

    _group_offset function a code to reserve a memory area in advance?

    No. Look at the code
    Think about where the address returned by this function is
    Unauthorized access


      

    return group->data + (index * group->element_size);

    data is void *, isn't it w
    Is this code going to compile? w

  • Answer # 4

      Is the

    _group_offset function a code to reserve a memory area in advance? However, the theory of reading the value with this function is unknown.

    No.
    If you look inside, you will understand, but
    return group->data + (index * group->element_size);
    The address dynamically allocated withmalloc + (element index * 1 element size)is returned.

    That is, the start address of the specified element of the array is returned.
    You might rename it to_group_get_element_address.

      Is the address copied by memcpy in the

    _group_assign function? However, since memcpy should be transcribed in order from the first character string of offset, isn't the order of addition reversed?

    No.
    The address is not copied,
    Copy the data pointed to by the address.

  • Answer # 5

    First, _group_offset ()
    return group->data + (index * group->element_size);
    This returns a position advanced by (index * group->element_size) bytes from group->data.

    So in _group_assign ()
    offset indicates the above position. Therefore,
    memcpy (offset, element, group->element_size);
    Copies the contents of group->element_size bytes from the position pointed to by element to the previous position.


    It's easy to understand if you draw a picture ...

    First 40 bytes of memory allocated
    [0] [1] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] ... [39]

    The pointer group.data points to the position [0].

    Because element_size is 4,
    For example, if you pass 2 as the index to _group_offset (),
    The return value is a pointer that points to the position advanced by 2 * 4, that is, the position of [9].