pointers

How to define a pointer pointing to a constexpr variable?

会有一股神秘感。 提交于 2021-01-27 19:09:34
问题 In C++ Primer 5th, it says that constexpr imposes a top-level const on the objects it defines. So how I can I declare a pointer with a constexpr specifier imposing a low-level const, i.e. a pointer pointing to a constexpr object? 回答1: A constexpr object is an object just like any other. The fact that its value is computed at compile time does not alter this. Often, the compiler will seek to avoid actually emitting code to create const values and objects if it knows that they will never be

Convert pointer to int and back to typed object

点点圈 提交于 2021-01-27 17:33:49
问题 I need a simple way to convert a pointer to an int, and then back to the pointer. I need it as an int for portability, since an int is easier to carry around in my project than a pointer. Start player and return pointer: SoundPlayer* player = new FxPlayerTiny(); return (int)player; // this works, but is it correct? Stop player using pointer int: FxPlayerTiny* player = static_cast<FxPlayerTiny*>((int*)num); // this gives me an error FxPlayerTiny* player = (FxPlayerTiny*)((int*)obj); // this

What is the difference between 2d array pointers and *arr[]?

谁都会走 提交于 2021-01-27 17:33:29
问题 I'm trying to understand what is the difference between an array like this: int arr[2][2] = {{0, 1}, {2, 3}}; int* pArr = (int*)arr; for(int i = 0; i < 4; i++) { printf("%d ", pArr[i]); } and this: int* foo[2] = {arr1, arr2}; // Let's say the length of arr1 is 3 and arr2 is 1 int* pFoo = (int*)foo; for(int i = 0; i < 4; i++) { printf("%d ", pFoo[i]); } They look pretty much the same to me but the output is completely different. I'm getting strange results, if i do what i gave here as an

Allocating memory for unions and difference between union pointer and union of pointers

≡放荡痞女 提交于 2021-01-27 14:30:44
问题 Since my question here couldn't be confidently answered, I ask here again in hope that someone knows for sure: Is there any difference (besides syntactical) between a pointer to a union and a union that contains pointers to its elements? The generated assembly in this example is identical. As long as I'm never accessing the other members, is it allowed to allocate memory for only one of the members (which isn't the largest)? Regarding the 2nd question, 6.5.2.1 of the C89 draft says: The size

In c++ how to check a pointer lies within a range?

房东的猫 提交于 2021-01-27 13:53:03
问题 Intuitively to check whecker pointer p lies in [ a , b ) one will do a<=p && p<b However, comparing pointers from two arrays results in unspecified behavior and thus we cannot safely say p is in [ a , b ) from this comparison. Is there any way one can check for this with certainty? (It would be better if it can be done for std::vector<T>::const_iterator , but I don't think it's feasible.) 回答1: Here's a partial solution. You can leverage the fact that the comparison would invoke unspecified

what's the difference about p.a and p->a where p is pointer?

旧时模样 提交于 2021-01-27 12:52:57
问题 Is there any difference about p.a and p->a where p is pointer? or they are just same thing. 回答1: The . operator is actually the operator for structure member access. struct Foo { int bar; int baz; } aFoo; aFoo.bar = 3; If you have a pointer to a struct, (very common) you can access its members using pointer dereferencing and the . operator. struct Foo *p; p = &aFoo; (*p).baz = 4; The parentheses are needed because . has higher precendence than * . The above dereferencing a member of a

Store pointer address in malloced memory

和自甴很熟 提交于 2021-01-27 12:30:37
问题 This feels like a silly question, but I just can't work out a clean solution and can't find a similar question in the mass of other pointer related questions. I have some dynamically allocated memory of unknown type and want to store a pointer inside it at the start. Dynamic memory returned by malloc should be suitably aligned so I don't think I have to worry about alignment when writing to the start of the allocated block. This is my code, which works, but I'm representing a pointer as a 64

Safely moving a C++ object

て烟熏妆下的殇ゞ 提交于 2021-01-27 07:10:16
问题 I’ve heard some words of warning against shipping an object to another memory location via memcpy , but I don’t know the specific reasons. Unless its contained members do tricky things that depend on memory location, this should be perfectly safe … or not? EDIT: The contemplated use case is a data structure like a vector , which stores objects (not pointers to objects) in a continuous chunk of memory (i.e. an array). To insert a new object at the n -th position, all objects starting at

Storing a pointer to a stack value (Golang)

大憨熊 提交于 2021-01-27 06:33:12
问题 I'm trying an experiment in Go, to see what happens if I store a pointer to a variable on the stack, and then access that variable after the original variable has left scope. package main import "fmt" var p chan bool; // a temp struct type v struct { a int } func another_thread(vx *v) { // this code should be executed after a() returns so vx should be a pointer to a value that's no longer on the stack fmt.Printf("another_thread(): %p\n", vx); vx.a = 4 // am I updating a dangling pointer that

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

若如初见. 提交于 2021-01-27 06:10:31
问题 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