简体   繁体   English

如何清除char *的内存数组?

[英]How to clear in memory array of char*?

I have such array with size of 24 byte :我有这样一个大小为24 byte数组:

char* arr[3] = {"CERN", "0", "ALK"};
printf("%ld\n", sizeof(arr));

Then I try to clear this array from memory by assigning \\0 to each element of array:然后我尝试通过将\\0分配给数组的每个元素来从内存中清除这个数组:

for (size_t i = 0; i < sizeof(arr)/ sizeof(char*); ++i) {
    arr[i] = '\0';
}

But when I want to check the size of array, it still gives me 24 byte :但是当我想检查数组的大小时,它仍然给了我24 byte

printf("%ld\n", sizeof(arr));
> 24

How to completely clear this array from memory that sizeof(arr) would give 0 ?如何从sizeof(arr)给出0内存中完全清除这个数组?

sizeof(arr) is the size of three char* . sizeof(arr)是三个char*的大小。 It doesn't change when you set each of the pointers to 0 .当您将每个指针设置为0时,它不会改变。

How to completely clear this array from memory that sizeof(arr) would give 0?如何从 sizeof(arr) 给出 0 的内存中完全清除这个数组?

There's no way to "clear" an array allocated on automatic storage.没有办法“清除”在自动存储上分配的数组。 You really don't need to "clear" it at all.你真的根本不需要“清除”它。

Note that you should use %zu to print a size_t value, which is what sizeof operator yields.请注意,您应该使用%zu打印size_t值,这是sizeof运算符产生的值。 Using an incorrect format specifier is undefined behaviour .使用不正确的格式说明符是未定义的行为

No, no, no.不,不,不。 There are several different issues here:这里有几个不同的问题:

  1. If you want to clear a block of memory, use memset()如果要清除内存块,请使用memset()

  2. If you want to zero out a string, all you need to do is set the FIRST character to null: arr[0] = 0;如果你想将一个字符串清零,你需要做的就是将第一个字符设置为空: arr[0] = 0;

  3. The sizeof operator tells you the size of your array. sizeof 运算符告诉您数组的大小。

  4. The strnlen() tells you the length of your string. strnlen()告诉您字符串的长度。

Even if you've allocated 3 bytes for your array, the actual length of the string might be 0, 1 or 2.即使您为数组分配了 3 个字节,字符串的实际长度也可能是 0、1 或 2。

It can never be "3" ... because you need at least one byte for your terminator.它永远不会是“3”......因为你的终止符至少需要一个字节。

You have assigned zeroes to the array, but that's all.您已为数组分配了零,但仅此而已。 You have not changed (and cannot, since you didn't malloc() it) the amount of memory allocated to the array, only cleared that data inside it.你没有改变(也不能,因为你没有malloc()它)分配给数组的内存量,只是清除了它里面的数据。

How to completely clear this array from memory that sizeof(arr) would give 0 ?如何从sizeof(arr)给出0内存中完全清除这个数组?

It is not possible given your declaration.鉴于您的声明,这是不可能的。

You'll have to come up with a different logic to come up with 0 -- the number of items in arr that are not NULL.您必须想出不同的逻辑才能得出0 —— arr中不为 NULL 的项目数。

int my_own_array_size(char* arr[], int numElements)
{
   int count = 0;
   for ( int i = 0; i < numElements; ++i )
   {
      if ( arr[i] != NULL )
         ++count;
   }
   return count;
}

and then use it as:然后将其用作:

int count = my_own_array_size(arr, 3);

char* arr[3] = {"CERN", "0", "ALK"}; char* arr[3] = {"CERN", "0", "ALK"};

  • Here arr is an array of 3 char pointers .这里arr是一个包含 3 个字符指针的数组。 Each element is of size 8 bytes (on 64 bit system).每个元素的大小为 8 字节(在 64 位系统上)。
  • So size of arr will always be 24 (3* sizeof(void *)) irrespective of the memory referenced by pointers (viz. An address or NULL, which is further interpreted as char or string.This has nothing to do with arr size)所以arr 的大小总是24 (3* sizeof(void *))与指针引用的内存无关(即地址或 NULL,它被进一步解释为字符或字符串。这与arr大小无关)
  • Above for loop will only initialize the pointers to NULL.上面的for 循环只会将指针初始化为 NULL。

How to completely clear this array from memory that sizeof(arr) would give 0?如何从 sizeof(arr) 给出 0 的内存中完全清除这个数组?

It is a static allocation (either an auto/global variable) .它是静态分配(自动/全局变量)。 memory assigned for arr cannot be cleared.无法清除为arr分配的内存。

note : In this case "CERN", "0", "ALK" are probably stored in read only segment.注意:在这种情况下, “CERN”、“0”、“ALK”可能存储在只读段中。

You may a look at this post since it explains the difference between statically and dynamically allocated memory:你可以看看这篇文章,因为它解释了静态和动态分配的内存之间的区别:

What and where are the stack and heap? 堆栈和堆是什么以及在哪里?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM