如何在动态分配的内存中移动参数?
How to move arguments in dynamically allocated memory?
我很难理解如何使用内存指针。我有两个指向先前分配的 myMemory 的指针(名称、地址、可选信息)。这些字符串的大小可以变化。它们像这样存储在 myMemory 中:
-------------------------------------------------------------------
| int ID 4Bytes | name 6bytes | adress 8Bytes| optionalInfo 8Bytes|
-------------------------------------------------------------------
假设我需要增加或减少名称的大小并进行更改。我怎样才能正确使用 realloc 不丢失任何我想要的信息?如何将内存中的参数转移到 free space 以获取新名称?我需要使用最少的内存。
这是我的结构,禁止更改。
struct myStruct{
char* name;
char* adress;
char* optionalInfo;
void* myMemory;
};
编辑:myMemory 的大小已知。此结构中的名称、地址和可选信息是指向存储在 myMemory
中的字符串的指针
让我们根据这个内存布局来考虑这个问题...
-------------------------------------------------------------------
| int ID 4Bytes | name 6bytes | adress 8Bytes| optionalInfo 8Bytes|
-------------------------------------------------------------------
...忽略它与 struct myStruct
成员的关系,但 myMemory
.
除外
Lets say I need to increase or decrease the size of the name and
change it. How can I correctly use realloc to not loose any
information I want?
只要您 realloc
的大小至少与已占用的数据一样大,重新分配就不会丢失任何数据。也不会相对于块的起始地址移动任何内容。但是请注意,重新分配的块可能与原始块位于不同的位置,如果是,则原始块将被释放,并且不能再访问。
How can I shift my arguments in memory to free
space for a new name?
这项工作最明显的工具是 memmove()
功能。因此,假设您想将标记为 "name" 的区域的大小从 6 个字节增加到比如说 10 个字节,而不覆盖任何其他数据,您首先需要重新分配:
void *temp = realloc(x.myMemory, old_size + 4);
由于 realloc()
不能保证一定会成功,因此在确定它确实成功之前,您不能更新 x.myMemory
指针:
if (temp) { // if temp is non-null
然后您可以在较大的 space 中移动地址和可选信息数据,以便为更多名称信息腾出空间:
memmove(((char *)temp) + new_offset_of_address_data,
((char *)temp) + old_offset_of_address_data,
combined_size_of_address_and_optionalInfo_data);
需要转换为类型 char *
,因为指针算法是根据指向类型的单位定义的,因此根本没有为指向 void
的指针定义。
不要忘记将新指针分配给您的结构:
x.myMemory = temp;
}
我很难理解如何使用内存指针。我有两个指向先前分配的 myMemory 的指针(名称、地址、可选信息)。这些字符串的大小可以变化。它们像这样存储在 myMemory 中:
-------------------------------------------------------------------
| int ID 4Bytes | name 6bytes | adress 8Bytes| optionalInfo 8Bytes|
-------------------------------------------------------------------
假设我需要增加或减少名称的大小并进行更改。我怎样才能正确使用 realloc 不丢失任何我想要的信息?如何将内存中的参数转移到 free space 以获取新名称?我需要使用最少的内存。
这是我的结构,禁止更改。
struct myStruct{
char* name;
char* adress;
char* optionalInfo;
void* myMemory;
};
编辑:myMemory 的大小已知。此结构中的名称、地址和可选信息是指向存储在 myMemory
中的字符串的指针让我们根据这个内存布局来考虑这个问题...
------------------------------------------------------------------- | int ID 4Bytes | name 6bytes | adress 8Bytes| optionalInfo 8Bytes| -------------------------------------------------------------------
...忽略它与 struct myStruct
成员的关系,但 myMemory
.
Lets say I need to increase or decrease the size of the name and change it. How can I correctly use realloc to not loose any information I want?
只要您 realloc
的大小至少与已占用的数据一样大,重新分配就不会丢失任何数据。也不会相对于块的起始地址移动任何内容。但是请注意,重新分配的块可能与原始块位于不同的位置,如果是,则原始块将被释放,并且不能再访问。
How can I shift my arguments in memory to free space for a new name?
这项工作最明显的工具是 memmove()
功能。因此,假设您想将标记为 "name" 的区域的大小从 6 个字节增加到比如说 10 个字节,而不覆盖任何其他数据,您首先需要重新分配:
void *temp = realloc(x.myMemory, old_size + 4);
由于 realloc()
不能保证一定会成功,因此在确定它确实成功之前,您不能更新 x.myMemory
指针:
if (temp) { // if temp is non-null
然后您可以在较大的 space 中移动地址和可选信息数据,以便为更多名称信息腾出空间:
memmove(((char *)temp) + new_offset_of_address_data,
((char *)temp) + old_offset_of_address_data,
combined_size_of_address_and_optionalInfo_data);
需要转换为类型 char *
,因为指针算法是根据指向类型的单位定义的,因此根本没有为指向 void
的指针定义。
不要忘记将新指针分配给您的结构:
x.myMemory = temp;
}