dereference

Dereference vector pointer to access element

孤街醉人 提交于 2019-11-27 19:54:43
问题 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! 回答1: 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>&

Difference between dereferencing pointer and accessing array elements

此生再无相见时 提交于 2019-11-27 19:25:42
I remember an example where the difference between pointers and arrays was demonstrated. An array decays to a pointer to the first element in an array when passed as a function parameter, but they are not equivalent, as demonstrated next: //file file1.c int a[2] = {800, 801}; int b[2] = {100, 101}; //file file2.c extern int a[2]; // here b is declared as pointer, // although the external unit defines it as an array extern int *b; int main() { int x1, x2; x1 = a[1]; // ok x2 = b[1]; // crash at runtime return 0; } The linker does not type-check for external variables so no errors are generated

ptr->hello(); /* VERSUS */ (*ptr).hello();

杀马特。学长 韩版系。学妹 提交于 2019-11-27 09:40:54
I was learning about C++ pointers and the -> operator seemed strange to me. Instead of ptr->hello(); one could write (*ptr).hello(); because it also seems to work, so I thought the former is just a more convenient way. Is that the case or is there any difference? The -> operator is just syntactic sugar because (*ptr).hello() is a PITA to type. In terms of the instructions generated at the ASM level, there's no difference. In fact, in some languages (D comes to mind), the compiler figures everything out based on type. If you do ptr.hello() , it just works, because the compiler knows that ptr is

Modifying value of char pointer in c produces segfault

雨燕双飞 提交于 2019-11-27 08:53:54
问题 The following code produces a segmentation fault on my system. I can't figure out why. Any help would be appreciated. #include<stdio.h> int main() { char * a = "abc"; *a = 'c'; printf("%c\n", *a); return 0; } 回答1: The standard explicitly lists this as undefined behavior in §J.2: — The program attempts to modify a string literal (6.4.5) If you want to copy it into a local array, do: char a[] = "abc"; a is an array on the stack, and you can modify it freely. 回答2: Attempting to modify a string

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

China☆狼群 提交于 2019-11-27 06:40:43
问题 Is there any difference in performance - or otherwise - between: ptr->a(); and (*ptr).a(); ? 回答1: 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 "*.".

Why does printing a pointer print the same thing as printing the dereferenced pointer?

寵の児 提交于 2019-11-27 05:33:54
From the Rust guide: To dereference (get the value being referred to rather than the reference itself) y , we use the asterisk ( * ) So I did it: fn main() { let x = 1; let ptr_y = &x; println!("x: {}, ptr_y: {}", x, *ptr_y); } This gives me the same results (x=1; y=1) even without an explicit dereference: fn main() { let x = 1; let ptr_y = &x; println!("x: {}, ptr_y: {}", x, ptr_y); } Why? Shouldn't ptr_y print the memory address and *ptr_y print 1? Is there some kind of auto-dereference or did I miss something? huon Rust usually focuses on object value (i.e. the interesting part of the

dereferencing pointer to incomplete type

耗尽温柔 提交于 2019-11-27 05:24:48
问题 I've seen a lot of questions on this but I'm going to ask the question differently without specific code. Is there a way of EASILY determining what is causing the type to be incomplete? In my case I'm using someone elses code and I'm completely sure I don't have the headers right, but (since computers do this stuff much faster and better than human eyeballs) is there a way to get the compiler to say, "hey you think you have type X at line 34 but that's actually missing ." The error itself

Is it considered a bad practice to implement Deref for newtypes?

。_饼干妹妹 提交于 2019-11-27 04:24:24
I often use the newtype pattern, but I am tired of writing my_type.0.call_to_whatever(...) . I am tempted to implement the Deref trait because it permits writing simpler code since I can use my newtype as if it were the underlying type in some situations, e.g. : use std::ops::Deref; type Underlying = [i32; 256]; struct MyArray(Underlying); impl Deref for MyArray { type Target = Underlying; fn deref(&self) -> &Self::Target { &self.0 } } fn main() { let my_array = MyArray([0; 256]); println!("{}", my_array[0]); // I can use my_array just like a regular array } Is this a good or bad practice? Why

Checking if an iterator is valid

纵然是瞬间 提交于 2019-11-27 03:48:23
Is there any way to check if an iterator (whether it is from a vector, a list, a deque...) is (still) dereferencable, i.e. has not been invalidated? I have been using try - catch , but is there a more direct way to do this? Example: (which doesn't work) list<int> l; for (i = 1; i<10; i++) { l.push_back(i * 10); } itd = l.begin(); itd++; if (something) { l.erase(itd); } /* now, in other place.. check if itd points to somewhere meaningful */ if (itd != l.end()) { // blablabla } I assume you mean "is an iterator valid," that it hasn't been invalidated due to changes to the container (e.g.,

“Char cannot be dereferenced” error

故事扮演 提交于 2019-11-27 02:01:39
问题 I'm trying to use the char method isLetter() , which is supposed to return boolean value corresponding to whether the character is a letter. But when I call the method, I get an error stating that "char cannot be dereferenced." I don't know what it means to dereference a char or how to fix the error. the statement in question is: if (ch.isLetter()) { .... .... } Any help? What does it mean to dereference a char and how do I avoid doing so? 回答1: The type char is a primitive -- not an object --