pointers

Can pointers to different types have different binary representations?

只愿长相守 提交于 2021-02-18 06:29:51
问题 I wonder if C++ implementations are allowed to represent pointers to different types differently. For instance, if we had 4-byte sized/aligned int and 8-byte sized/aligned long , would it be possible to represent pointers-to- int / long as object addresses shifted right by 2/3 bits, respectively? This would effectively forbid to convert a pointer-to- long into a pointer-to- int . I am asking because of [expr.reinterpret.cast/7]: An object pointer can be explicitly converted to an object

What is the type of a pointer to a 2D array?

谁都会走 提交于 2021-02-17 15:15:33
问题 I know that the following is not correct: int arr[2][3] = {}; //some array initialization here int** ptr; ptr = arr; But I am quite surprised that the following lines actually work int arr[2][3] = {}; //some array initialization here auto ptr = arr; int another_arr[2][3] = {}; //some array initialization here ptr = another_arr; Can anyone possibly explain what is the type assigned to ptr in the second block of code, and what happened underneath? 回答1: Well, arrays decay to pointers when used

What is the type of a pointer to a 2D array?

社会主义新天地 提交于 2021-02-17 15:14:55
问题 I know that the following is not correct: int arr[2][3] = {}; //some array initialization here int** ptr; ptr = arr; But I am quite surprised that the following lines actually work int arr[2][3] = {}; //some array initialization here auto ptr = arr; int another_arr[2][3] = {}; //some array initialization here ptr = another_arr; Can anyone possibly explain what is the type assigned to ptr in the second block of code, and what happened underneath? 回答1: Well, arrays decay to pointers when used

returning pointer to a local variable [duplicate]

若如初见. 提交于 2021-02-17 07:08:04
问题 This question already has answers here : How to access a local variable from a different function using pointers? (10 answers) Closed 4 years ago . What happens when a pointer to a local variable is returned by a function? for example int* foo() { int local; int* ptr = &local; return ptr; } will compiler issue a warning or will it compile and produce unexpected results?? 回答1: Similar kind of question has already been asked : Stack Overflow, local pointer There are somethings in C which are

returning pointer to a local variable [duplicate]

你离开我真会死。 提交于 2021-02-17 07:06:29
问题 This question already has answers here : How to access a local variable from a different function using pointers? (10 answers) Closed 4 years ago . What happens when a pointer to a local variable is returned by a function? for example int* foo() { int local; int* ptr = &local; return ptr; } will compiler issue a warning or will it compile and produce unexpected results?? 回答1: Similar kind of question has already been asked : Stack Overflow, local pointer There are somethings in C which are

Passing a Pointer into a Function and Modifying it

好久不见. 提交于 2021-02-17 05:21:07
问题 New to both stackoverflow and C/C++. Im working through implementing a binary tree and have a simple question. Lets say I have the following: struct Node { int data; Node* right_child; Node* left_child; }; void addNode(Node* tree, int new_data){ if(tree == NULL){ Node* new_tree = new Node; new_tree->data = new_data; new_tree->right_child = NULL; new_tree->left_child = NULL; tree = new_tree; } } int main(){ Node* tree = new Node; tree = NULL; addNode(tree, 3); cout << tree->data << endl; /

Where does Go define that p.Field (as opposed to (*p).Field) is a valid syntax even when p is a pointer to a struct?

寵の児 提交于 2021-02-17 02:17:23
问题 Here is my Go program. package main import ( "fmt" ) type Person struct { Name string } func main() { p := &Person{"Jack"} // The following statement makes sense. We dereference the // pointer to reach the Person object and then retrieve its Name // field. fmt.Println((*p).Name) // But the following statement does not make sense. How does // this code work by retrieving the Name field directly from the // pointer without dereferencing it? fmt.Println(p.Name) } Here is the output. $ go run foo

C++ exception return type why char*

我的梦境 提交于 2021-02-16 17:57:22
问题 Could someone explain why self-written C++ exception, that inherit from exception return a char * and not a string? class myexception: public exception { virtual const char* what() const throw() { return "My exception happened"; } } myex; Source : http://www.cplusplus.com/doc/tutorial/exceptions/ 回答1: Since std::exception was designed as a base class for all exceptions, the interface is written in such a way that specializations do not require code that may throw. They may use it, but they do

De-referencing void pointer to a pointer array

混江龙づ霸主 提交于 2021-02-16 15:39:26
问题 I'm using a threading library given to me at school and having trouble understanding how to pass a reference of an array of pointers to a method, or rather, I'm having trouble de-referencing and using the pointer array. The situation that I (believe I) understand is this: int main(void) { Foo* bar = new Foo(); // Passing instance of Foo pointer by reference CThread *myThread = new CThread(MyFunction, ACTIVE, &bar); return 0; } UINT __stdcall MyFunction(void *arg) { Foo* bar = *(Foo*)(arg);

De-referencing void pointer to a pointer array

≯℡__Kan透↙ 提交于 2021-02-16 15:39:25
问题 I'm using a threading library given to me at school and having trouble understanding how to pass a reference of an array of pointers to a method, or rather, I'm having trouble de-referencing and using the pointer array. The situation that I (believe I) understand is this: int main(void) { Foo* bar = new Foo(); // Passing instance of Foo pointer by reference CThread *myThread = new CThread(MyFunction, ACTIVE, &bar); return 0; } UINT __stdcall MyFunction(void *arg) { Foo* bar = *(Foo*)(arg);