dereference

Is it possible to dereference variable id's? [duplicate]

随声附和 提交于 2019-11-26 17:48:20
This question already has an answer here: Get object by id()? 7 answers Can you dereference a variable id retrieved from the id function in Python? For example: dereference(id(a)) == a I want to know from an academic standpoint; I understand that there are more practical methods. martineau Here's a utility function based on a comment made by "Tiran" in the discussion Hophat Abc referenced that will work in both Python 2 and 3: import _ctypes def di(obj_id): """ Inverse of id() function. """ return _ctypes.PyObj_FromPtr(obj_id) if __name__ == '__main__': a = 42 b = 'answer' print(di(id(a))) # -

Dereferencing this pointer gives me -46, but I am not sure why

丶灬走出姿态 提交于 2019-11-26 15:28:18
This is a program I ran: #include <stdio.h> int main(void) { int y = 1234; char *p = &y; int *j = &y; printf("%d %d\n", *p, *j); } I am slightly confused about the output. What I'm seeing is: -46 1234 I wrote this program as an experiment and wasn't sure what it was going to output. I was expecting possibly one byte from y . What is happening "behind-the-scenes" here? How does dereferencing p give me -46 ? As pointed out by others, I had to do explicit casting to not cause UB. I am not changing that line from char *p = &y; to char *p = (char *)&y; so that I am not invalidating the answers

At what point does dereferencing the null pointer become undefined behavior?

谁说胖子不能爱 提交于 2019-11-26 14:48:51
问题 If I don't actually access the dereferenced "object", is dereferencing the null pointer still undefined? int* p = 0; int& r = *p; // undefined? int* q = &*p; // undefined? A slightly more practical example: can I dereference the null pointer to distinguish between overloads? void foo(Bar&); void foo(Baz&); foo(*(Bar*)0); // undefined? Okay, the reference examples are definitely undefined behavior according to the standard: a null reference cannot exist in a well-defined program, because the

Checking if an iterator is valid

浪子不回头ぞ 提交于 2019-11-26 12:41:29
问题 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 } 回答1: I assume

Why does the arrow (->) operator in C exist?

冷暖自知 提交于 2019-11-26 11:58:07
The dot ( . ) operator is used to access a member of a struct, while the arrow operator ( -> ) in C is used to access a member of a struct which is referenced by the pointer in question. The pointer itself does not have any members which could be accessed with the dot operator (it's actually only a number describing a location in virtual memory so it doesn't have any members). So, there would be no ambiguity if we just defined the dot operator to automatically dereference the pointer if it is used on a pointer (an information which is known to the compiler at compile time afaik). So why have

PHP 5.3.10 vs PHP 5.5.3 syntax error unexpected &#39;[&#39;

馋奶兔 提交于 2019-11-26 11:38:02
问题 Is it possible that this PHP code line if ($this->greatestId()[\"num_rows\"] > 0) works in PHP 5.5 and returns an error in 5.3?? PHP Parse error: syntax error, unexpected \'[\' in /var/www/app/AppDAO.php on line 43 How can I change it to work under PHP 5.3? 回答1: Array dereferencing became available in PHP 5.4 That's why this doesn't work in PHP 5.3. So you have an extra step where you need to get the array value from your function call and then you can use it: $variable = $this->greatestId();

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

故事扮演 提交于 2019-11-26 09:42:01
问题 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!(\"

Is a closure for dereferencing variables useful?

陌路散爱 提交于 2019-11-26 07:45:46
问题 I\'m not sure whether or when it is useful (to improve performance) to dereference variables. var x = a.b.c.d[some_key].f; while (loop) { do_something_with(x); } seems to be better than while (loop) { do_somthing_with(a.b.c.d[some_key].f); } Is that needed or is this done automagically by smart JavaScript engines? But my actual question is whether I should do this, for example, in a library. (function() { var slice = Array.prototype.slice; Function.prototype.x = function x() { var args =

Why can&#39;t I treat an array like a pointer in C?

ぐ巨炮叔叔 提交于 2019-11-26 07:42:43
问题 I see this question a lot on SO. Maybe not in so many words... but time and again there is confusion on how arrays are different from pointers. So I thought I would take a moment to Q&A a few points about this. For purposes of this Q&A we\'re going to assume a 32-bit system and the following have been declared: char * ptr = \"hello\"; char arr[10] = \"hello\"; int iarr[10] = {0}; Here\'s a list of questions that surmise the confusion I see on SO. As I see new ones I\'ll add to my list of Q&A

meaning of “referencing” and “dereferencing”

无人久伴 提交于 2019-11-26 07:01:05
问题 I read different things on the internet and got confused, because every website says different things. Speaking about C. I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing is accessing the value of the variable that the pointer points to. So I got confused. Can I get a simple but thorough explanation about \"referencing and de-referencing\"? 回答1: Referencing means taking the address of an existing