Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zero-out entire char pointer

Tags:

c

char

pointers

I am trying to zero-out an entire char pointer. If I perform the statement:

memset(myCharPointer, 0, sizeof(myCharPointer));

it only zeros-out the first 4 bytes because that is the size of a char pointer on my system.

So how can I ensure that the data is completely set to 0? Setting it to NULL does not wipe out the entire char pointer.

like image 376
Brian Avatar asked Jul 30 '26 10:07

Brian


2 Answers

sizeof(myCharPointer) is going to give you a sizeof(char*), which is the size of a pointer, which is usually four bytes. A sizeof(char) is going to give you 1 byte.

To do the memset you need to know how long your data is. e.g.,

memset(myCharPointer, 0, myCharPointerLen);

like image 70
paleozogt Avatar answered Jul 31 '26 23:07

paleozogt


if you know the length of the char array you are pointing to you can do this:

memset(myCharPointer, 0, sizeof(*myCharPointer) * length);

Also, I believe you should use the sizeof the pointer contents instead of that of the pointer itself.

like image 22
Argote Avatar answered Jul 31 '26 23:07

Argote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!