0

I have such array with size of 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:

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:

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

How to completely clear this array from memory that sizeof(arr) would give 0?

3
  • 3
    Since this is a static array you cannot remove the variable from memory... Commented Aug 5, 2016 at 6:22
  • sizeof(arr)/ sizeof(char*); --> sizeof(arr)/ sizeof(arr[0]); Commented Aug 5, 2016 at 6:27
  • arr[i] = '\0' --> arr[i] = NULL Commented Aug 5, 2016 at 6:46

6 Answers 6

3

sizeof(arr) is the size of three char*. It doesn't change when you set each of the pointers to 0.

How to completely clear this array from memory that sizeof(arr) would give 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. Using an incorrect format specifier is undefined behaviour.

Sign up to request clarification or add additional context in comments.

2 Comments

If I don't need to clear array ... what about memory leak?
Memory leak can happen only if you are using dynamic memory allocation such as using malloc() or calloc() or realloc() or other functions that return pointer to dynamically allocated memory such as strdup(). There's no memory leak in your code. The array arr has three pointers allocated on automatic storage (aka, stack) and each of the pointers point to a string literal respectively. You haven't allocated memory for any of these yourself.
1

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.

Comments

1

No, no, no. There are several different issues here:

  1. If you want to clear a block of memory, use 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;

  3. The sizeof operator tells you the size of your array.

  4. The strnlen() tells you the length of your string.

Even if you've allocated 3 bytes for your array, the actual length of the string might be 0, 1 or 2.

It can never be "3" ... because you need at least one byte for your terminator.

Comments

1

How to completely clear this array from memory that sizeof(arr) would give 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.

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);

Comments

1

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

  • Here arr is an array of 3 char pointers. Each element is of size 8 bytes (on 64 bit system).
  • 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)
  • Above for loop will only initialize the pointers to NULL.

How to completely clear this array from memory that sizeof(arr) would give 0?

It is a static allocation (either an auto/global variable) . memory assigned for arr cannot be cleared.

note : In this case "CERN", "0", "ALK" are probably stored in read only segment.

Comments

-1

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?

4 Comments

This is not an answer. It is a comment.
it is the answer since it explains the concept. If the OP understands the concept he understands that the question doesn't make sense.
it is a link only answer and does not add any value as an answer.
we agree to disagree

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.