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.
Related Posts:
- Cleanest way to copy a constant size array in c++11
- too many initializers for ‘int [0]’ c++
- How to dynamically allocate arrays in C++
- How to dynamically allocate arrays in C++
- What is a lambda expression in C++11?
- ld: symbol(s) not found for architecture x86_64 error
- Initializing an array of objects
- Passing an array by reference
- Return array in a function
- What is the ‘override’ keyword in C++ used for? [duplicate]
- What is a smart pointer and when should I use one?
- Meaning of = delete after function declaration
- C++ std::priority_queue uses the lambda expression
- Usage and Syntax of std::function
- Passing an array by reference
- What exactly is nullptr?
- How to convert a char array to a string?
- What is the array form of ‘delete’?
- Difference between `constexpr` and `const`
- Split a string using C++11
- Use new operator to initialise an array
- How do I declare a 2d array in C++ using new?
- lvalue required as left operand of assignment – Array
- Why doesn’t C++ have a garbage collector?
- Compiling C++11 with g++
- What exactly is std::atomic?
- Issue with std::stol – ‘std::invalid_argument’ what(): stol
- terminate called after throwing an instance of ‘std::out_of_range’
- push_back vs emplace_back
- C++ error: “Array must be initialized with a brace enclosed initializer”
- undefined reference to ‘std::cout’
- What is move semantics?
- warning: ISO C++ forbids variable length array
- expression preceding parentheses of apparent call must have (pointer-to-) function type
- C++ terminate called without an active exception
- Correct way of looping through C++ arrays
- Static array vs. dynamic array in C++
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- Difference in make_shared and normal shared_ptr in C++
- Fill array with random numbers within a specified range (C++)
- Remove an array element and shift the remaining ones
- C++: std does not have member “string”
- How to convert vector to array
- push_back vs emplace_back
- How to find the size of an int[]?
- C++: Expression must have a constant value when declaring array inside function
- What’s the syntax for declaring an array of function pointers without using a separate typedef?
- error: use of deleted function
- Printing an array in C++?
- How do you initialise a dynamic array in C++?
- C++ array assign error: invalid array assignment
- cc1plus: error: unrecognized command line option “-std=c++11” with g++
- How to add element to C++ array?
- Reading from .txt file into two dimensional array in c++
- extended initializer lists only available with
- error C2679: binary ‘<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
- Creation of Dynamic Array of Dynamic Objects in C++
- how to initialize an empty integer array in c++
- Check if C++ Array is Null
- Correct way to work with vector of arrays
- Resizing dynamic array in c++
- no match for ‘operator<<’ in ‘std::operator
- Comparing the values of char arrays in C++
- Dynamically allocated string array, then change it’s value?
- Use the auto keyword in C++ STL
- Does C++11 have C#-style properties?
- How do I return a char array from a function?
- creating an array of structs in c++
- Reading data from file into an array
- How to read lines of text from file and put them into an array
- Why can’t we pass arrays to function by value?
- Iterator Loop vs index loop
- Double pointer array in c++
- C++ pass an array by reference
- When is it safe to call this-> in constructor and destructor
- What is a null-terminated string?
- creating dynamic array of string c++
- How well is Unicode supported in C++11?
- creating an array of structs in c++
- How to automatically convert strongly typed enum into int?
- C++ Array of pointers: delete or delete []?
- Why should I use a pointer rather than the object itself?
- Creating folders in C++
- Using cin for char array
- C++ correct way to return pointer to array from function
- Warning: comparison of distinct pointer types
- how to find 2d array size in c++
- convert string to size_t
- Getting error “array bound is not an integer constant before ‘]’ token”
- Using cin to input a single letter into a char
- Different ways to deallocate an array – c++
- Is C++ Array passed by reference or by pointer?
- Compiling C++11 with g++
- ERROR C2039: ‘vector’: is not a member of ‘std’
- c++ vector bubble sort
- Proper way to pass dynamic arrays to other functions
- Array of Linked Lists C++
- How to use bitmask?
- Differences between unique_ptr and shared_ptr
- C++ float array initialization