dereference

Is it considered good style to dereference `new` pointer?

邮差的信 提交于 2019-11-29 17:45:45
问题 To avoid keep having to use -> and instead work directly with the object, is it acceptable practice to do: obj x = *(new obj(...)); ... delete &obj; 回答1: This is not just poor practice, but: Leaking memory (most likely, unless you are using some pattern that is not visible from the code you provided), since obj will store a copy of the original object created by the new expression, and the pointer to that object returned by new is lost; Most importantly, undefined behavior , since you are

Terminology question on “dereferencing”?

笑着哭i 提交于 2019-11-29 16:20:08
In PHP, the following code is valid $a=array(0);$a[0]; but that one is invalid: array(0)[0] What is the terminology corresponding to that behaviour? (has it anything to do with "dereferencing"?) What is the motivation behind such a behaviour (besides user spite :-P) I am looking for the general terminology , not necessarily the terminology associated with PHP. (Other example: in MATLAB, the following is valid: s = size(M) s(0) but that is invalid: size(M)(0) In both PHP and MATLAB, adding parenthesis does not help, i.e., (array(0))[0] and (size(M))(0) are both invalid) That's called Array

Regarding dereferencing 'void *' pointer

对着背影说爱祢 提交于 2019-11-29 14:55:09
I am not able to find out how to remove the above warning from the below line of code. data is is void pointer and as part of callback function will be receiving string in the data pointer. As I have typecast the void pointer but compiler still showing the warning. There are basically two warnings showing up on the below line. 1. dereferencing 'void *' pointer 2. taking address of expression of type 'void service_ind = atoi((const char*)&data[at_response.param[0].start_of_value_index]) ? TRUE:FALSE ; Below are required information void * data; AT_PARSER_RESPONSE at_response; typedef struct { /

Meaning of the ampersand '&' and star '*' symbols in Rust

好久不见. 提交于 2019-11-29 11:37:43
问题 Despite thoroughly reading the documentation, I'm rather confused about the meaning of the & and * symbol in Rust, and more generally about what is a Rust reference exactly. In this example, it seems to be similar to a C++ reference (that is, an address that is automatically dereferenced when used): fn main() { let c: i32 = 5; let rc = &c; let next = rc + 1; println!("{}", next); // 6 } However, the following code works exactly the same: fn main() { let c: i32 = 5; let rc = &c; let next = *rc

L value vs R value in C

ⅰ亾dé卋堺 提交于 2019-11-29 02:32:30
I am answering a textbook question from this textbook . I am learning about pointers in C and have come across l-values and r-values. From my understanding: l-values are values that are defined after they are executed (++x) r-values are values that are not defined after they are executed (x++) It that correct? The question I wanted to answer (with my attempts): a) Which of the following C expressions are L-Values? 1. x + 2 Not a L value 2. &x Is a L value 3. *&x Is a L value 4. &x + 2 Not a L value 5. *(&x + 2) Is a L value 6. &*y Is a L value b) Is it possible for a C expression to be a L

Why would code explicitly call a static method via a null pointer?

与世无争的帅哥 提交于 2019-11-28 23:33:26
问题 I've seen code like this in a couple of old projects: class Class { static void Method() {} }; ((Class*)0)->Method(); This code contains undefined behavior because it includes dereferencing a null pointer (no matter what happens afterwards). It really makes no sense - the cast is there to feed the type name to the compiler and whoever wrote the code above could have written this instead: Class::Method(); and the latter would be okay. Why would anyone write the former code? Is it a known idiom

C programming: Dereferencing pointer to incomplete type error

假如想象 提交于 2019-11-28 21:04:58
I have a struct defined as: struct { char name[32]; int size; int start; int popularity; } stasher_file; and an array of pointers to those structs: struct stasher_file *files[TOTAL_STORAGE_SIZE]; In my code, I'm making a pointer to the struct and setting its members, and adding it to the array: ... struct stasher_file *newFile; strncpy(newFile->name, name, 32); newFile->size = size; newFile->start = first_free; newFile->popularity = 0; files[num_files] = newFile; ... I'm getting the following error: error: dereferencing pointer to incomplete type whenever I try to access the members inside

Dereference vector pointer to access element

大城市里の小女人 提交于 2019-11-28 16:19:48
If i have in C++ a pointer to a vector: vector<int>* vecPtr; And i'd like to access an element of the vector, then i can do this by dereferncing the vector: int a = (*vecPtr)[i]; but will this dereferencing actually create a copy of my vector on the stack? let's say the vector stores 10000 ints, will by dereferencing the vecPtr 10000 ints be copied? Thanks! sergtk 10000 int s will not be copied. Dereferencing is very cheap. To make it clear you can rewrite int a = (*vecPtr)[i]; as vector<int>& vecRef = *vecPtr; // vector is not copied here int a = vecRef[i]; In addition, if you are afraid that

How to understand the pointer star * in C?

99封情书 提交于 2019-11-28 14:38:22
问题 I'm struggling with the pointer sign *, I find it very confusing in how it's used in both declarations and expressions. For example: int *i; // i is a pointer to an int But what is the logic behind the syntax? What does the * just before the i mean? Let's take the following example. Please correct me where I'm wrong: char **s; char *(*s); // added parentheses to highlight precedence And this is where I lose track. The *s between the parantheses means: s is a pointer? But a pointer to what?

C++ - Difference between (*). and ->?

浪尽此生 提交于 2019-11-28 12:00:47
Is there any difference in performance - or otherwise - between: ptr->a(); and (*ptr).a(); ? Since you are asking for it in the comments. What you are probably looking for can be found in the Standard (5.2.5 Class member access): 3 If E1 has the type “pointer to class X,” then the expression E1->E2 is converted to the equivalent form (*(E1)).E2; The compiler will produce the exact same instructions and it will be just as efficient. Your machine will not know if you wrote "->" or "*.". [Edit] If the variable is defined as T* (where T is some type) then both -> and * are the same (unless ptr is