pointers

Is it possible to match against a NULL pointer in Rust?

淺唱寂寞╮ 提交于 2021-01-27 06:10:12
问题 Calling is_null() feels a bit odd: fn do_stuff(ptr: *const i32) -> Option<i32> { if ptr.is_null() { None } else { Some(do_transform(*ptr, 42)) } } 回答1: As of Rust 1.9, there's a function as_ref that converts a raw pointer to an Option<&T> , and a mutable variant as_mut: Your code would look something like fn do_stuff(ptr: *const i32) -> Option<i32> { let ptr = unsafe { ptr.as_ref() }; ptr.map(|x| do_transform(x, 42)) } 来源: https://stackoverflow.com/questions/37466676/is-it-possible-to-match

Is it possible to match against a NULL pointer in Rust?

为君一笑 提交于 2021-01-27 06:08:47
问题 Calling is_null() feels a bit odd: fn do_stuff(ptr: *const i32) -> Option<i32> { if ptr.is_null() { None } else { Some(do_transform(*ptr, 42)) } } 回答1: As of Rust 1.9, there's a function as_ref that converts a raw pointer to an Option<&T> , and a mutable variant as_mut: Your code would look something like fn do_stuff(ptr: *const i32) -> Option<i32> { let ptr = unsafe { ptr.as_ref() }; ptr.map(|x| do_transform(x, 42)) } 来源: https://stackoverflow.com/questions/37466676/is-it-possible-to-match

advantage of passing pointer to a struct as argument?

我只是一个虾纸丫 提交于 2021-01-27 05:47:36
问题 Our coding style is we pass a pointer to a struct to a function, when we are modifying the contents of the structure. However, when we are not modifying the contents of a struct, is there still any reason to prefer passing a pointer to a struct to a function? 回答1: The advantage is in the size being passed: when you pass a large struct , the compiler generates code to make a copy of that struct if you pass it by value. This wastes CPU cycles, and may create a situation when your program runs

advantage of passing pointer to a struct as argument?

懵懂的女人 提交于 2021-01-27 05:45:48
问题 Our coding style is we pass a pointer to a struct to a function, when we are modifying the contents of the structure. However, when we are not modifying the contents of a struct, is there still any reason to prefer passing a pointer to a struct to a function? 回答1: The advantage is in the size being passed: when you pass a large struct , the compiler generates code to make a copy of that struct if you pass it by value. This wastes CPU cycles, and may create a situation when your program runs

What exactly is invalidation of reference/pointer?

本小妞迷上赌 提交于 2021-01-27 05:28:41
问题 I cannot find any definition for invalidation of pointers/references in the Standard. I ask because I just found out that C++11 forbids copy-on-write (COW) for strings. As far as I understand, if COW was applied then p would be still a valid pointer and r a valid reference after the following commands: std::string s("abc"); std::string s2(s); char * p = &(s2[0]); char & r = s2[0]; s2[1] = "B"; Just they would no longer point/refer to the first character of s2 , but merely to the first

Access to variadic function' arguments without va_list in C

若如初见. 提交于 2021-01-27 04:42:13
问题 Is it possible to iterate through variadic function' arguments using pointer (void pointer?) to last named argument? (I know that's not the right way to work with variadic arguments, but I'm still interested if that would work) Setting pointer to the end of the string doesn't work, because after I start moving the pointer, it points to other strings used in the program. #include <stdio.h> #include <string.h> #include <stdlib.h> void form_date(MON* datePtr, int dayMonth, int dayYear, int month

C standard regarding pointer arithmetic outside arrays

时光毁灭记忆、已成空白 提交于 2021-01-27 04:38:52
问题 I read lot of things about pointer arithmetic and undefined behavior (link, link, link, link, link). It always ends up to the same conclusion: Pointer arithmetic is well defined only on array type and between array[0] and array[array_size+1] (one element past the end is valid with regard to the C standard). My question is: Does it means that when the compiler sees a pointer arithmetic not related to any array (undefined behavior), it could emit what it want (even nothing) ? Or is it more a

C standard regarding pointer arithmetic outside arrays

 ̄綄美尐妖づ 提交于 2021-01-27 04:38:23
问题 I read lot of things about pointer arithmetic and undefined behavior (link, link, link, link, link). It always ends up to the same conclusion: Pointer arithmetic is well defined only on array type and between array[0] and array[array_size+1] (one element past the end is valid with regard to the C standard). My question is: Does it means that when the compiler sees a pointer arithmetic not related to any array (undefined behavior), it could emit what it want (even nothing) ? Or is it more a

Function argument binding rules for passing an array by reference vs passing pointer

∥☆過路亽.° 提交于 2021-01-27 04:33:27
问题 To prevent any confusion, I very much understand the difference between arrays and pointers, the concept of decay-to-pointer, and the concept of passing an array by reference in C++, etc. My question here is specifically about the rules used by the compiler to select a function from a set of function overload candidates, when one overload takes an array reference, and the other overload takes a pointer. For example, suppose we have: template <class T, std::size_t N> void foo(const T (&arr)[N]

Fortran interface to call a C function that returns a pointer to an array

a 夏天 提交于 2021-01-23 11:05:55
问题 After much searching, I found what I believe to be the closest answer to my problem is on Stack Overflow (SO) at Fortran interface to call a C function that return a pointer, (posted nearly 10 years ago!) I quote this because using that example keeps the code simple and still illustrates my problem. I want to return an array that has been created/memory allocated in C++ and be able to analyse the answer in Fortran, because that is where the bulk of the code for this application lies. My