Questions tagged [dereference]

Anything related to pointer dereference, i.e. the process of determining the object which the pointer is referring to. Languages having pointer variables usually have a special operator to perform dereferencing of pointers (e.g. in C and C++, if `p` is a valid pointer, `*p` is the object pointed to by `p`).

Filter by
Sorted by
Tagged with
2 votes
2 answers
55 views

Dereferencing pointer cast from an integer of a valid memory address give Segmentation fault

I was playing around with c pointer and wrote this code #include <stdio.h> int main() { int a = 17; int* p1 = &a; int* p2 = (int*)(&p1); p1 = (int*)(&p2); printf(...
twowr's user avatar
  • 21
0 votes
0 answers
70 views

This c code that demonstrates pass by values and pass by references in C doesn't work right [duplicate]

#include <stdio.h> void passbyvalue(int number); void passbyreference(int* pNumber); void passbyreference1(int** pNumber); int main(int argc, char* argv[]) { int number = 5; int* ...
cougarhound's user avatar
0 votes
0 answers
72 views

Delphi: adequate data structure for headered blocks

As far as I know one of the most used data structure cannot represented by an adequate dta structure directly: we speak about blocks or streams with header. Let's imagine a simple image format, where ...
Zoltán Bíró's user avatar
0 votes
1 answer
49 views

GDB pointer dereference gives wrong address

I'm using GDB and I have a pointer to a c-style string in one of the registers. I want to display that string. However when I try to dereference the pointer in the register, or even manually, it gives ...
FalcoGer's user avatar
  • 2,366
1 vote
1 answer
45 views

Why can't I auto deref when reading from a mutable reference?

I got this question from Rustling: fn vec_loop(mut v: Vec<i32>) -> Vec<i32> { for element in v.iter_mut() { *element = *element * 2; } v } vec_loop multiplies the ...
Kodra's user avatar
  • 149
0 votes
2 answers
145 views

Cannot borrow data in 'Rc' as mutable

I've been writing a ray tracer in Rust and I've come across a problem. I have defined the following struct: pub struct HitRecord{ mat : Rc<RefCell<dyn Material>, ... } The Material ...
yomag1234's user avatar
-1 votes
1 answer
40 views

C++ iterator reference non-exist value

I want to create two iterator p1 & p2 to keep pointing to certain elements in a multiset(or list), but weird thing is *p1 *p2 return a value NOT even in the multiset/list. And in for-loop ...
Joelin Zou's user avatar
0 votes
2 answers
66 views

Why do I get the error when I try to dereference a pointer to an array?

I'm very basic at C and I don't understand something along these codes. In the first piece of code I am declaring an array called arr with predefined values and I also declare a pointer of type int ...
RobertC's user avatar
0 votes
0 answers
54 views

Is there a Rust signature signaling that any type that coerces into T would be allowed? [duplicate]

So I just started learning about Rust and the Implicit Deref Coercion. A "problem" I now often tumble over, is that this automatic deref does not work inside of other types, e.g. as Item ...
David S.'s user avatar
  • 313
-2 votes
2 answers
84 views

simple address book program with struct in C

I have a problem related with this program : #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct people { char full_name [100]; char age [100]; ...
DeadlyKitten999's user avatar
0 votes
2 answers
90 views

C# how dereferencing works behind the scenes

This code is from book named "Pro C# with .NET 6" and I wonder what happens behind the scenes while the reference to reference(ref Person p) is passed to the method. for example when we do p....
Aka's user avatar
  • 33
0 votes
1 answer
95 views

Is there a way to dereference a pointer stored in memory?

I'm trying to store a pointer in a variable located on memory. How could I dereference it? I'm trying to do it like this: pointer: db 0 ; the pointer variable var: db 44 ; the normal variable ...
batatinha_espacial's user avatar
0 votes
0 answers
6 views

Invalid conversion from 'int*' to 'int' from section "Subscripts and Pointers" in C++ Primer

This code is an excerpt from C++ Primer fifth edition. I keep getting this error, "Invalid conversion from 'int*' to 'int'. Any ideas on how to fix this? Also, can anyone clarify the difference ...
Albert Wang's user avatar
0 votes
3 answers
84 views

Difference between pointer to pointer and 2d array

EDIT This is not a question on array decay from char [1] and char *. I know what array decay is for 1D arrays. It however seems different for char [1][1] and char ** since they are not even compatible ...
spinosarus123's user avatar
0 votes
0 answers
126 views

Android 13 camera crashes : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR) : /vendor/bin/mm-qcamera-daemon

In android 13, the camera crashes upon launching. The camera worked perfectly fine in android 12, however, in android 13 it crashes. I am not sure what is the root cause of the issue. I have attached ...
Antóin's user avatar
1 vote
2 answers
152 views

Are there differences between .to_owned(), .clone() and dereferencing (*)?

With this code on the rust playground: fn main() { let a = &[1, 2, 3]; let mut o = a.to_owned(); let mut c = a.clone(); let mut d = *a; o[0] = 7; c[0] = 8; d[...
fadedbee's user avatar
  • 43.1k
0 votes
0 answers
8 views

Invalid read of size 1 at address

So, I am writing a simple shell program and in my function main, I called “strcmp” to check if the command entered is “env” and “exit”. My code compiles fine but when I run valgrind to check error and ...
Oke Adenike's user avatar
-3 votes
2 answers
117 views

Confusion about C pointers

I am sending this message to clear my confusion that I could not manage and handle. The foo1 function code should work. I am giving the code details for you. When I run the code, the result is a ...
synapsis's user avatar
-1 votes
1 answer
68 views

Implementing Builder Pattern in Rust [duplicate]

I am trying to implement the builder pattern in Rust, but am running into some issues. I am new to the ownership paradigm that Rust follows, so maybe some knowledge gap there has resulted in the issue,...
r0nald_mcdonald's user avatar
0 votes
1 answer
125 views

How to dereference std::unique_ptr<int[]>?

The operator* works for std::unique_ptr<std::array<int, 5>> but not for std::unique_ptr<int[]>. But why? Coming from cppreference: These member functions are only provided for ...
digito_evo's user avatar
  • 3,276
1 vote
1 answer
76 views

Why can't I use the Deref trait inside Drop in Rust?

I implemented the Deref trait for the MyString struct which allows me to use an object of type MyString via *o or o.deref(). But when I implement the Drop trait for MyString then the already ...
Frank Schwidom's user avatar
0 votes
1 answer
89 views

double pointer to the char array

I have been trying to figure out how double pointer works with char * and char []. What I want to do is to assign a double pointer to the char * or char [] and then change the content. #include <...
Lucky Im's user avatar
3 votes
1 answer
153 views

Why do I need to implement `From` for both a value and a reference? Shouldn't methods be automatically dereferenced or borrowed?

The behaviour of a Rust method call is described in The Rust Reference. It states that "when looking up a method call, the receiver may be automatically dereferenced or borrowed in order to call ...
scottwillmoore's user avatar
1 vote
1 answer
123 views

When a type is `Copy` should a method move (`self`) or borrow (`&self`) the type?

In Rust, when a type is Copy, should a method move (self) or borrow (&self) the type? When a method from a non-Copy type is called, there is a significant difference between a move (self) and a ...
scottwillmoore's user avatar
0 votes
1 answer
48 views

Dereference buffer after ReadFile (MASM)

I have a crude program to read a 4-byte file (0x00000060 or 96). The file is 4-byte long for the sake of simplicity, I don't know its length at the start. I can't figure out how to dereference the ...
GreatCorn's user avatar
0 votes
0 answers
16 views

How to model all the permutations of pointer/dereferencing syntax from C or Rust in a different programming language syntax?

I am working on a custom programming language with minimal syntax, and wondering how I can properly account for and handle the various pointer/dereferencing syntax in C or Rust. Say that every ...
Lance's user avatar
  • 76k
-1 votes
1 answer
50 views

How come we have to dereference the pointer returned from strbrk(), but don't have to derefence the pointer returned from strrchr()?

How come we have to dereference the pointer returned from strbrk(), but don't have to derefence the pointer returned from strrchr()? So I have the code below: #include <stdio.h> #include <...
Magical Bologna's user avatar
2 votes
1 answer
123 views

Must lvalues of type T identify objects of type T? If `p` has type `T *`, does `&*p` require `p` to actually point to an object of type `T`?

In C++, given ST *p, *p produces an lvalue (expr.unary.op#1), and if the static type ST is primitive converting *p to an rvalue actually accesses and reads *p (conv.lval, basic.lval#11). But suppose ...
Blaisorblade's user avatar
  • 6,448
-2 votes
1 answer
45 views

Confusion in dereferencing pointer to 2d and 3d vectors

I'm unable to wrap my head around dereferencing 2d and 3d vectors. First, I'm creating a 2d vector (of size 4x3) and filling it up: std::vector<std::vector<float>> two_dim (4, std::vector&...
Polar Joker's user avatar
0 votes
1 answer
87 views

C++ dereference class attribute pointing to another class

I have 3 classes: Student Idea Game The class student is a person. This person can generate Ideas of different types (games, car or some other type of idea). So the Game class Extends the Idea class....
Kristian Vasilev's user avatar
-1 votes
1 answer
64 views

The program will crash when I run .\pass_args.exe -a 5 -q (when command line argument are not given in pairs)

I am struggling to identify the bug which cause the program to crash when the code is run with odd number of command line arguments. e.g. The program can be launched with command line arguments: w : ...
Jayden's user avatar
  • 25
0 votes
1 answer
55 views

"*value += how_much" vs "*value += *how_much" in rust?

In the following rust code, why do both of the functions mutate_value() work with and without dereferencing the how_much param? Shouldn't this not work since I am adding a reference to the value? fn ...
Vraj Shah's user avatar
  • 576
-3 votes
1 answer
97 views

type &var = *ptr VS type var = *ptr

Let foo be: class Foo { public: Foo(int a) { aa = a; cout << "foo built. " << aa << endl; } ~Foo() { cout << "foo DIED. "...
salehjg's user avatar
  • 51
1 vote
1 answer
130 views

Dereferenced Pointer to Vector Element in Object doesn't Update Value

I am trying to make a toy Entity Component System to improve on my C++ skills. To do so, I have an ECS_Manager Class, which uses templates to have multiple storage classes for each data type I'd like ...
user3047023's user avatar
2 votes
1 answer
63 views

why does dereferencing a Box<T> not complain about "moving out of shared reference", when T is not a Copy?

As the title states, why does the following code compile? use std::ops::Deref; struct Foo {} fn main() { let b = Box::new(Foo{}); let pb = *b; //works // let pb = *b.deref(); // ...
soupybionics's user avatar
  • 4,250
1 vote
1 answer
174 views

How to properly dereference all addresses of a pointer in Cython?

I'm learning the basics of Cython 3.0 and I'm trying to understand why one of my approaches to dereferencing a pointer (to multiple memory addresses) in order to initialize the values in those ...
Eric's user avatar
  • 25
0 votes
1 answer
52 views

Pointer don't lets auto variable to get deleted after the function call [duplicate]

When I use a pointer instead of referencing operator in a function pointer do not allow auto variable to get deleted after function call. I got Segmentation fault (core dumped) from this code because ...
A_normal_guy's user avatar
0 votes
2 answers
1k views

Dereference of a possibly null reference warning

I have an object called conversationService. When I try to call a method on the object I get a warning: 'conversationService' may be null here. CS8602: Dereference of a possibly null reference. The ...
Totte's user avatar
  • 1
0 votes
1 answer
69 views

struct using a pointer, ... in something not a struct or union

I defined a structure called coordonnees declared as a pointer, I want to append values to the pointer a_visiter but it's not working. How can I fix that? Here is the struct code: typedef struct ...
Elas's user avatar
  • 21
1 vote
0 answers
46 views

Why the write function does not print the 'asd' integer variable? [duplicate]

I'm currently learning pointers and the write function. I've written the code below. My question is the write functions does not print anything at all. In the write function's manual, it says that the ...
Huseyin Donmez's user avatar
2 votes
2 answers
91 views

Why does dereferencing a char** value (pointer-to-pointer-to-char) differ from dereferencing a char*[] (pointer-to-char array)?

If a char* array is defined with at least one value, the first value can be indexed via "arr[0]" and printed successfully with the format specifier "%s". However, if a char** is ...
Ryan Pereira's user avatar
0 votes
1 answer
76 views

Does dereferencing pointer cast violate strict aliasing?

Does this violate strict aliasing (or any other spec rules)? void *get_int_ptr(void) { static int ival = 0; return (void*)&ival; } #define GET_INT() *((int*)get_int_ptr()) void main() { ...
Edwin Skeevers's user avatar
0 votes
0 answers
232 views

Trying to update Rocket.rs managed state with error - cannot assign to data in dereference of `rocket::State<Timer>`

I am new to rust and I am trying to update some Rocket state in a request handler. Here is an example of my code: #[derive(Debug, Serialize, Deserialize)] struct Timer { playing: bool, paused: ...
CUGreen's user avatar
  • 3,056
1 vote
3 answers
88 views

Why would someone take an address, typecast the pointer, and then dereference it?

I ran into a weird code snippet while following an image processing guide. The language is C. What is the purpose of taking an address, casting the pointer, then dereferencing it? I am new to C, so I ...
Mustafa 's user avatar
3 votes
2 answers
104 views

How can I distinguish a pointer from a dereference in C?

int value =5; void testPointer( int* pa, int* pb) { *pa = *pb +5; *pb = value; value += 10; } How can I distingush both from each other? I dont get it
Basbla's user avatar
  • 41
0 votes
1 answer
77 views

How to solve this problem (throw an exception)

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> void input_count(int*); int* input_values(int); void show_result(int**, int); int main() { int count = 0; ...
프린이's user avatar
-1 votes
1 answer
60 views

Rust Refs & Derefs

Would like to check if my understandings are correct: case 1 fn main() { let x = 5; let y = x; assert_eq!(5, y); println!("y = {}", y); } Q1) {assert_eq!(5, &y)...
bonchef's user avatar
  • 371
1 vote
1 answer
110 views

iterator dereferencing cost a huge time

I solved a problem with Set operations like upperbound, iterator dereferencing etc. It solves in around 20 seconds. The general problem is I am iterating over group of numbers (i*(i-1)/2) until it is ...
Azzurro94's user avatar
  • 479
0 votes
1 answer
383 views

Rust - implementing trait for Deref: the parameter type `T` may not live long enough

I have a trait: trait Foo { fn bar(&self) -> Cow<str>; } And I want to implement it for any type that implements Deref with a target of a type that implements Foo. Basically: impl<...
Cameron Delong's user avatar
2 votes
1 answer
54 views

Why does deref coercion not work with `From::from`?

From the section in the book: Deref coercion converts a reference to a type that implements the Deref trait into a reference to another type. It happens automatically when we pass a reference to a ...
jsstuball's user avatar
  • 4,239

1
2 3 4 5
24