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
- (Initialization) Store the type, number, number of reservations, and memory I want to put in the array in the structure.
- (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?
- (Read) Returns the address of the element I want to read ...? ? ?
Questions
-
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. 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?
# 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);
}
-
Answer # 1
-
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 as
element
. -
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].
Related articles
- html - i want to know why the tag turns red
- the code diverges for some reason
- it is about the task of c language
- read the file and calculate the total c++
- i want to find the fade position with opencv
- about the c language life game
- i want to find the sum of a sequence in c language
- c - i want to find the standard deviation of 10 real data
- i just want to practice the code
- i don't know if the strings match
- i don't know why you can't type
- i want to read the data of another file in c language
- about memory allocation of union
- c - i want to set the 10's and 1's to 0 for items with a tax of 10,000 yen or more
- c - the "hello" to be displayed one by one
- find the value of sin once
- c - i don't know why it loops endlessly
- c - variables are stored in memory in the same order
- ruby on rails - why is the root directory in scss path specification not "~/"?
- jsp - i don't know why the red wavy lines come out
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.
No.
Calculates the memory address that is indexed backward from the start address.
The data actually placed at the specified address.
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.