Do I still need to set ptr to NULL if I use memset earlier?
No need. You can also use the calloc()
instead of the malloc()
, it will set memory to zero without requiring an additional call to the memset()
and may be faster.
Is memset(&ptr, 0, sizeof(ptr)); the same as ptr=NULL;?
In C, memset(&ptr, 0, sizeof(ptr))
is not necessarily the same as ptr=NULL
since the ISO/IEC 9899:1999 clause 17.7 paragraph 3 says that NULL
expands to an implementation-defined null pointer constant
Thus not necessarily an all-zero bit pattern.
Posix has not had a different guarantee than the C standard. However, the next revision draft says that
POSIX additionally guarantees that any pointer object whose representation has all bits set to zero, perhaps by memset() to 0 or by calloc(), will be interpreted as a null pointer.
Hence it seems that Posix will guarantee that memset(&ptr, 0, sizeof(ptr))
will have the same effect as ptr=NULL
(and this is mostly likely already the case on all existing implementations). However, the statements will not necessarily assign identical bit patterns to ptr
.
It is important to notice that the C standard (and Posix, also with the mentioned update) does not guarantee that the bit pattern of a null pointer is unique. Thus, in principle ptr=NULL
could assign a different bit pattern to ptr
than memset(&ptr, 0, sizeof(ptr))
as long as this bit pattern is also a null pointer.
The standard does guarantee (clause 6.3.2.3 paragraph 4) that
Any two null pointers shall compare equal.
Thus if ptr
and ptr2
are both null pointers, then ptr == ptr2
evaluates to true
. However, memcmp(&ptr, &ptr2, sizeof(ptr))
may not evaluate to 0
.
How to memset for pointer-to-pointer
The memset
statment below is setting all the memory allocated by proto
to 0
. So proto.p_aliases
is pointing to 0x000000
then you're tring to dereference it which leads to segmentation fault.
memset(&proto,0,sizeof(proto));
*proto.p_aliases
is the content of the adresse pointed to by proto.p_aliases
so you cannot print it as the adresse is NULL
. You can only print the adresse pointed to. So you have to change your print statment to:
printf("proto.p_aliases = %s\n", proto.p_aliases); // Not *proto.p_aliases
And because you're using the %s
formating it will print (null)
.
If you want to use memset
with *proto.p_aliases
then you have to allocate memory for it then proto.p_aliases
is no longer pointing to NULL
. In this case you can print. Then content of it's adresse.
proto.p_aliases = malloc(sizeof(char *));
memset(&(*proto.p_aliases), 0, sizeof(*proto.p_aliases));
printf("*proto.p_aliases %s", *proto.p_aliases);
How to memset char array with null terminating character?
Options one and two are just wrong. The first one uses the size of a pointer instead of the size of the array, so it probably won't write to the whole array. The second uses sizeof(char*)
instead of sizeof(char)
so it will write past the end of the array. Option 3 is okay. You could also use this
memset( buffer, '\0', sizeof(char)*ARRAY_LENGTH );
but sizeof(char)
is guaranteed to be 1.
Initialize a pointer to a class with NULL values
First of all, the simplest solution is to do the following:
Node** buckets = new Node*[SIZE]();
As litb previously stated, this will value initialize SIZE
pointers to null pointers.
However, if you want to do something like Node **buckets
and initialize all of the pointers to a particular value, then I recommend std::fill_n
from <algorithm>
Node **buckets = new Node*[SIZE];
std::fill_n(buckets, SIZE, p);
this will set each Node*
' to p
after allocation.
In addition, if you want the Node to have sane member valuesupon construction, the proper way is to have a constructor. Something like this:
struct Node {
Node() : data(0), next(NULL){}
Node(int d, Node *n = NULL) : data(d), next(n) {}
int data;
Node* next;
};
That way you can do this:
Node *p = new Node();
and it will be properly initialized with 0 and NULL, or
Node *p = new Node(10, other_node);
Finally, doing this:
Node *buckets = new Node[N]();
will construct N
Node
objects and default construct them.
Is it common practice to memset reallocated memory to 0?
So my question is, as memset does not necessarily mean setting values
to NULL and the for loop solution seems a bit tedious – is it really
needed to set the newly allocated memory?
realloc
doesn't initialize values of the newly allocated memory segment.
So it is needed to initialize the memory if you are planning to read values of that (uninitialized) memory. Because reading values from that uninitialized memory will trigger undefined behaviour.
By the way, safe way to use realloc
(since it can fail) is:
// Since using realloc with size of 0 is tricky and useless probably
// we use below check (from discussion with @chux)
if (new_size == 0)
dosmth();
else
{
new_p = realloc(p, new_size);
if (new_p == NULL)
{
// ...handle error
}else
{
p = new_p;
}
}
C memset - elegantly add a null terminator
The question suggests that chars_to_pad
points to memory allocated using malloc()
. Another alternative is to use calloc()
instead. This function automatically zero-initializes the allocated memory, so there is no need to zero the allocation in a separate step.
An example might look like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
size_t arr_sz = 11;
char *arr = calloc(arr_sz, sizeof *arr);
memset(arr, '*', arr_sz - 1);
puts(arr);
return 0;
}
Program output:
**********
Related Topics
Why Is This Simd Multiplication Not Faster Than Non-Simd Multiplication
Defining Operator< for a Struct
Specifying a Concept for a Type That Has a Member Function Template Using Concepts Lite
Does Std::Bind Work with Move-Only Types in General, and Std::Unique_Ptr in Particular
Is There a Readable Implementation of the Stl
Protocol Buffer Over Socket in C++
To Stl or !Stl, That Is the Question
Calculating Execution Time in C++
How to Make a Recursive Rule in Boost Spirit X3 in VS2017
The Fastest Way to Retrieve 16K Key-Value Pairs
What Are Consequences of Forcing Qobject as a Parent of Qwidget
Why Do Sin(45) and Cos(45) Give Different Results
C++11: "Narrowing Conversion Inside { }" with Modulus
How to Design Proper Release of a Boost::Asio Socket or Wrapper Thereof
Better Variable Exploring When Debugging C++ Code with Eclipse/Cdt
What's the Difference Between Cstdlib and Stdlib.H
Why Do I Need to Repeat Template Arguments of My Base Class in Member Initalizer List