In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Why don't use pointers to pass arrays directly in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn why you don't use pointers to pass arrays directly in C++.
Do not directly use the pointer to pass the array Reason (reason)
Pointer + size style interfaces can easily cause errors. At the same time, a simple pointer (to an array) must be guessed by the callee according to certain conventions.
Example (sample)
Consider (consider the following code):
Void copy_n (const T* p, T* Q, int n); / / copy from [p:p+n) to [q:q+n)
What happens if the number of elements in the array that Q points to is less than n? The answer is to overwrite some memory that may be irrelevant. What if the number of elements in the array pointed to by p is less than n? The answer is to read some extraneous memory. In either case, there is no defined behavior and can cause serious errors.
Translator's note: the difficulty of this kind of error lies in the distance between the location of the problem and the location of the problem, and the possible problems are different each time.
Alternative (optional)
Consider using a clear span:
Void copy (span r, span R2); / / copy r to r2Example, bad (negative example)
Consider (consider the following code):
Void draw (Shape* p, int n); / / poor interface; poor codeCircle arr [10]; / /... draw (arr, 10)
Passing 10 to formal parameter n may cause an error: the most general convention is to assume [0VH n) (left closed and right open), but there is no explanation anywhere. The worst part is the compilation behavior of the call to draw (): there is an implicit conversion from array to pointer (array degenerate) and implicit conversion from Circle to Shape. Draw () has no way to traverse the array safely because it cannot know the number of elements.
Translator's note: the draw function has no way to know either the size of the array or the type of the element.
Optional: use support classes that ensure the correctness of the number of elements and avoid dangerous implicit conversions. For example:
Void draw2 (span); Circle arr [10]; / /... draw2 (span (arr)); / / deduce the number of elementsdraw2 (arr); / / deduce the element type and array size
Void draw3 (span)
Draw3 (arr); / / error: cannot convert Circle [10] to span
Draw2 () (two calls to) passes the same amount of information to draw. The reason for this result is that (the draw2 function) infers that its argument is the range of Circles.
Exception (exception)
Use zstring and czstring instead of C style, a string that ends with 0. Use str::string_view or string_span from GSL in this case to avoid range errors.
Translator's note: errors will occur when using span in this case.
Enforcement (implementation recommendations)
(Simple) (Bounds) Warn for any expression that would rely on implicit conversion of an array type to a pointer type. Allow exception for zstring/czstring pointer types.
(simple) ((range)) any expression contains an implicit conversion from array to pointer, alarm. As an exception, pointers of type zstring/czstring are allowed.
(Simple) (Bounds) Warn for any arithmetic operation on an expression of pointer type that results in a value of pointer type. Allow exception for zstring/czstring pointer types.
(simple) ((range)) if the expression of the pointer is calculated and a pointer value is generated, an alarm is given. You can make an exception when the pointer type is zstring/czstring.
At this point, I believe you have a deeper understanding of "Why don't you use pointers to pass arrays directly in C++?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.