pointers

Deleting an object when multiple pointers are pointing to it?

一笑奈何 提交于 2021-02-07 06:22:44
问题 I've been told that when if I have multiple pointers pointing to the same object, I cannot delete it normally (using the delete keyword). Instead, I've been told that I need to set the pointers to NULL or 0. Given I have: ClassA* object = new ClassA(); ClassA* pointer1 = object; ClassA* pointer2 = object; So to delete pointer1 and pointer2 , do I need to do the following? pointer1 = 0; pointer2 = 0: Once I've set it to NULL, do I still need to use the keyword delete ? Or is just setting it to

C - Setting a struct to null (incompatible types in assignment)

我只是一个虾纸丫 提交于 2021-02-06 12:50:05
问题 I have the following struct: struct elem { int number; char character; }; struct item { struct elem element; }; and the following function: void init(struct item *wrapper) { assert(wrapper != NULL); wrapper->element = NULL; } item->element = NULL yields a incompatible types in assignment . Why is that? Shouldn't setting a struct to NULL be okay? 回答1: In C NULL is generally defined as the following #define NULL ((void*)0) This means that it's a pointer value. In this case your attempting to

C - Setting a struct to null (incompatible types in assignment)

耗尽温柔 提交于 2021-02-06 12:47:32
问题 I have the following struct: struct elem { int number; char character; }; struct item { struct elem element; }; and the following function: void init(struct item *wrapper) { assert(wrapper != NULL); wrapper->element = NULL; } item->element = NULL yields a incompatible types in assignment . Why is that? Shouldn't setting a struct to NULL be okay? 回答1: In C NULL is generally defined as the following #define NULL ((void*)0) This means that it's a pointer value. In this case your attempting to

From []byte to char*

一世执手 提交于 2021-02-06 10:16:50
问题 I want to wrap a C function that takes a char* pointing to (the first element of) a non-empty buffer of bytes. I'm trying to wrap that in a Go function using CGo so that I can pass it a []byte , but I don't know how to do the conversion. A simplified version of the C function's signature is void foo(char const *buf, size_t n); I tried passing a pointer to the first byte in the slice with C.foo(&b[0], C.size_t(n)) That doesn't compile, though: cannot use &b[0] (type *byte) as type *_Ctype_char

Difference between unsigned and signed int pointer

牧云@^-^@ 提交于 2021-02-06 09:41:05
问题 Is there anything such as an unsigned int* which is different from int* . I know that unsigned has a higher range of values. Still, can't int* even point to any unsigned int ? 回答1: int * and unsigned int * are two different pointer types that are not compatible types. They are also pointers to incompatible types. For the definition of compatible types , please refer to § 6.2.7 in the C Standard (C11). Being pointers to incompatible types means that for example that this: unsigned int a = 42;

How to set bool pointer to true in struct literal?

╄→尐↘猪︶ㄣ 提交于 2021-02-06 09:39:10
问题 I have the function below which accepts a bool pointer. I'm wondering if there is any notation which allows me to set the value of the is field to true in the struct literal; basically without to define a new identifier (i.e. var x := true ; handler{is: &x} ) package main import "fmt" func main() { fmt.Println("Hello, playground") check(handler{is: new(bool) }) } type handler struct{ is *bool } func check(is handler){} 回答1: You can do that but it's not optimal: h := handler{is: &[]bool{true}

Dereferencing a pointer to an array?

心已入冬 提交于 2021-02-06 09:14:25
问题 Referring to the line with the comment: Why does adding parenthesis in the example work to print all the contents of the array? The example prints "one", then prints garbage. #include <iostream> int main() { const char* a[3] = { "one", "two", "three" }; const char*(*p)[3] = &a; for(int i = 0; i < 3; i++) { std::cout << *p[i] << std::endl; // this line } return 0; } It works after changing to this: std::cout << (*p)[i] << std::endl; 回答1: p is a pointer to an array of 3 elements like this: ┌───

Dereferencing a pointer to an array?

∥☆過路亽.° 提交于 2021-02-06 09:14:02
问题 Referring to the line with the comment: Why does adding parenthesis in the example work to print all the contents of the array? The example prints "one", then prints garbage. #include <iostream> int main() { const char* a[3] = { "one", "two", "three" }; const char*(*p)[3] = &a; for(int i = 0; i < 3; i++) { std::cout << *p[i] << std::endl; // this line } return 0; } It works after changing to this: std::cout << (*p)[i] << std::endl; 回答1: p is a pointer to an array of 3 elements like this: ┌───

Dereferencing a pointer to an array?

限于喜欢 提交于 2021-02-06 09:13:23
问题 Referring to the line with the comment: Why does adding parenthesis in the example work to print all the contents of the array? The example prints "one", then prints garbage. #include <iostream> int main() { const char* a[3] = { "one", "two", "three" }; const char*(*p)[3] = &a; for(int i = 0; i < 3; i++) { std::cout << *p[i] << std::endl; // this line } return 0; } It works after changing to this: std::cout << (*p)[i] << std::endl; 回答1: p is a pointer to an array of 3 elements like this: ┌───

Dereferencing a pointer to an array?

时间秒杀一切 提交于 2021-02-06 09:12:41
问题 Referring to the line with the comment: Why does adding parenthesis in the example work to print all the contents of the array? The example prints "one", then prints garbage. #include <iostream> int main() { const char* a[3] = { "one", "two", "three" }; const char*(*p)[3] = &a; for(int i = 0; i < 3; i++) { std::cout << *p[i] << std::endl; // this line } return 0; } It works after changing to this: std::cout << (*p)[i] << std::endl; 回答1: p is a pointer to an array of 3 elements like this: ┌───