Questions tagged [pointers]

22005 questions
1

votes
2

answer
146

Views

deep copy of objects containing unordered_map of unique_ptr

I have the following situation: I need to create a deep (!) copy of a variable from type A. This variable contains a vector of type B. Class B is not copyable, because it contains a std::unordered_map whos value is a unique_ptr (which is not copyable). I would like to avoid making B copyable via a...
user7431005
1

votes
2

answer
51

Views

Assign values to a pointer which was initialised to NULL before

Can someone tell me why do I get this segmentation fault? Is it because I have set a pointer to NULL and passed it to a function? Should I use arrays or pointers when I want to send an array to function? #include #include int* ComputeFibo(int _size); void PrintFibo(int* _arr, int _size); int mai...
Sexy mf
0

votes
1

answer
11

Views

Defected value when passing a matrix by pointer

I am using an algorithm where I need to copy a 2D matrix in a more sized 2D matrix. When I am working in the same main stack it works well but when I pass the pointer of the copied matrix to another function, the values became corrupted. The steps to generate the problem: 1- Copy a 2D square matrix...
1

votes
1

answer
49

Views

Different addresses of a pointer and an array in a union

I want to get the address of the first element of an array. This array is in an union with a pointer. Code: #include #include #include union l_array_0_uint8_t { uint8_t *pnt; uint8_t array[8]; }; int main() { union l_array_0_uint8_t u; uint8_t* aa = &(u.array[0]); uint8_t* pa = u.pnt; printf('arr...
peter tomson
1

votes
2

answer
42

Views

Send a string array to a function by pointer or pointer-to-pointer with mem alloc?

Both of these functions fill the TCHAR sStringA and sStringB. The question is which one is preferred and more efficient in returning a result? #include #include void FunctionA(TCHAR *sString1, DWORD dwLen) { _tcscpy_s(sString1, dwLen, L'String1'); return; } void FunctionB(TCHAR **sString2, DWORD d...
JeffR
1

votes
2

answer
47

Views

Fail to delete a pointer

I am just learning pointer and dynamic memory allocation with keywords new and delete. Below is my C++ code to test my understanding. #include using namespace std; int main() { // Variable to be pointed to by a pointer cout
K_inverse
1

votes
1

answer
53

Views

Array in C containing Incorrect Values

The goal of this program is to add the first and last elements of an array together and set that value as the first element of an output array, and then continue moving inwards as such. All of the sums will be stored in an output array. For this program, the rules stipulate that I may only use point...
Matthew Thibodeau
1

votes
2

answer
27

Views

How to display the value to which a pointer referred? [duplicate]

This question already has an answer here: Function returning address of local variable error in C 3 answers Displaying the value to which a pointer referred in the local function makes sense for me, calling this local function in the main function, the value to which the pointer refered changed. #...
1

votes
2

answer
43

Views

Optimize calls to allocator when replacing the content of a shared pointer

Consider this program: #include struct T { T() {} }; void do_something(std::shared_ptr ptr) { // Do something with ptr; might or might not leave // other copies of ptr in other variables of the // program } int main() { std::shared_ptr ptr = std::make_shared(); do_something(ptr); // ptr might or mi...
1

votes
1

answer
34

Views

How do the hex numbers after a jump instruction translate into a memory address?

I have this in my code disassembly: I see the jump instruction code E9, but how the numbers after E9 translate into the memory address shown?
Arush Agarampur
1

votes
2

answer
36

Views

After sending a pointer to an external function to make it null, it doesn't change to null [duplicate]

This question already has an answer here: Passing by reference in C 17 answers I have created a pointer of type 'Vector' (see code). After creation I want to send a pointer of type Vector (meaning Vector*) to a function called 'VectorDestroy(Vector* _vector), which frees the struct from memory and...
Majd Kharman
1

votes
2

answer
47

Views

why different string pointers to the same content have the same adders in c? [duplicate]

This question already has an answer here: C optimisation of string literals 2 answers I assign two different pointers with the same content(not value!) to strings and when I print they address I saw they have the same adders and the none pointer string has different adders, can someone explain why...
Hillel Horgen
1

votes
1

answer
42

Views

Embedded C: Can I pass members of a static struct to function in another file, IF a pointer to the member is passed by a fn in the same file

I have a struct like this: typedef struct { uint8_t var_1; uint8_t var_2; }TYPE_struct_variables; static TYPE_struct_variables variables; For argument sake, I want to pass one of the members in this struct down another layer towards the metal. For this example, this is a data struct for an external...
Medicineman25
1

votes
3

answer
47

Views

Runtime error when using push_back() on smart pointer to vector

I am trying to fill a vector, by dereferencing a smart-pointer. During runtime the program crashes after one iteration of the first 'for' loop used for entering the variable input. using namespace std; class Measurement { protected: int sample_size; string label; shared_ptr data; public: // paramet...
user2013904
1

votes
1

answer
47

Views

Process returned -1073741819 (0xC0000005) in C

I'm making a program in which I need to first convert an integer to a string (char array) for that I used itoa() function which is working fine, after that I want to also concate it to a string, but it's not working and giving me following error: Process returned -1073741819 (0xC0000005) My Code: in...
Usman Developer
0

votes
1

answer
22

Views

How to access struct members using pointers

I created a structure in a header file to store student information. I initialize these member variable in a function called createStudent(). The createStudent() function returns a pointer to a newly initialized student. Now, when I test that this all works I get some unexpected results. The code c...
Unks
-1

votes
0

answer
24

Views

Create a polymorphic function that sorts shapes by area (parameter is vector of pointers)

BIG EDIT: unfortunately was not showing up in my function sortTheShapes parameter. I had to create multiple classes in order to calculate the area of a shape. I have my base class (Shape) and several derived classes such as circle, rectangle, and triangle. My task now is to create the polymorphic f...
Evan
1

votes
1

answer
676

Views

Unwanted creation of object at same memory address

I haven't been working with C++ for a long time and now I need to do some small 'project'. I reviewed few topics trying to find solution for my problem with no results... Here is 'pseudo-code' (Pair is my class): Pair** pairs; I get size of table from input and create table of pointers: pairs = new...
bart.z33
0

votes
1

answer
22

Views

Pointer in C only updates once when method is called

I am doing a program to mimic the hare and turtle race. I dont understand why my pointer for the turtle movement only updates once when the condition is met multiple times. #include #include #include #define SIZE 70 void turtleMove(char raceTrack[SIZE], char *tPtr); int main() { char raceTrack[S...
RyM
0

votes
0

answer
6

Views

Why do pointers implement interface with pointer receivers, but not vice versa?

Not a duplicate of X does not implement Y (... method has a pointer receiver) There are many questions regarding errors that arise from mixing regular and pointer receivers when implementing an interface on a type. I understand the fixes and proper design concepts for this, but don't understand why...
robbieperry22
1

votes
3

answer
1k

Views

Making std::to_string work with void pointers, like operator<<? [closed]

I have some code which std::to_string()'s a variable whose type is a template parameter. Suppose the code is: template std::string foo(const T& t) { return std::string('I got ') + std::to_string(t); } Now, sometimes this parameter needs to be a void *; and I would like to get the address in the...
einpoklum
0

votes
2

answer
31

Views

Can only pointers hold NULL values?

I was trying to set a struct to NULL but the compiler was saying 'invalid initializer', but the moment I made a pointer for that struct it worked properly. struct node { int value; struct node * left; struct node * right; }; struct node a = NULL; /* doesn't work */ struct node * b = NULL; /* works f...
pvs1209
1

votes
1

answer
61

Views

C: Initialize array of character pointers (strings) and iterate through a file using fgets to put value in these strings [closed]

I want to create an array of character pointers (strings) of length 6 But I want to initialize them by iterating through a file which has \n separated exactly 6 lines char* a[6]; FILE *fp = fopen('test.txt', 'r'); for (int i = 0; i < 6; i++) { fgets(a[i], MAXLEN, fp); } But it gives me a segmentatio...
Rayclank
1

votes
2

answer
52

Views

Confused by double pointer traversal of linked list

So I was watching this Ted Talk w/ Linus Torvalds wherein he outlines this example: https://www.youtube.com/watch?v=o8NPllzkFhE&t=14m26s I was intrigued by it, so I wanted to code it up for myself: void remove_node(node* entry) { node** indirect = &head; while (*indirect != entry) { indirect = &(*in...
Jackson Lenhart
1

votes
2

answer
93

Views

std::shared_ptr copy constructor thread safety

std::shared_ptr specification guarentees that only one thread will invoke delete on the internal pointer. This answer has a really nice explanation about the required memory ordering on the shared_ptr refrence count manipulation in order to guarentee that the delete will be invoked on a synchronized...
Nina Kaprez
1

votes
4

answer
156

Views

c - const pointer to const data (gcc - duplicate 'const' declaration specifier)

I am programming a module on a microcontoller for interfacing it's EEPROM to receive some user data from there. Since you can not overwrite the EEPROM easily I want to return a const pointer to const data. Now my functions prototype looks like this: const struct userData const* getEEPROMDataAtIndex(...
Paul Würtz
1

votes
1

answer
56

Views

Golang *bytes.Buffer nil causes fatal error [closed]

I had the same issue as https://github.com/golang/go/issues/26666 because I have a wrap function for my http requests. Sometimes I need to request: body := new(bytes.Buffer) json.NewEncoder(body).Encode(h) req('POST', 'http://example.com', body) And sometimes it's simply: req('GET', 'http://example...
Raimonds Linde
1

votes
1

answer
87

Views

Why is the destructor not called for Box::from_raw()?

I am passing a raw pointer to two different closures and converting the raw pointer to a reference using Box::from_raw() and the program is working fine. However, after converting the raw pointer to reference, the destructor should be called automatically as the documentation says: This function is...
A-B
1

votes
1

answer
76

Views

Difference: &ary1D[0] and &ary1D [duplicate]

This question already has an answer here: What is array decaying? 8 answers Is an array name a pointer? 9 answers I have in my main-Function an double Array: double ary1D[4] = {1.1, 2.2, 3.3, 4.4}; and have a function call: print1D_A(&ary1D[0],Num); my function: void print1D_A(double *ary1D, int...
Fkcm95
1

votes
3

answer
67

Views

Not able to understand this C program

I am new to programming, not getting the code below. This program checks if a character c is in the string s. int is_in(char *s, char c){ while(*s){ if(*s==c) return 1; else s++; return 0; } The main thing confusing me is, how the while loop will stop, as, I think s++ will go through all over the m...
Ashis Ghosh
1

votes
2

answer
83

Views

C Function Pointer Differences

I have the following C Code: #include int max(int x, int y, int (*compare)(int, int)) { return (*compare)(x,y) ? x : y; } int greater_than(int x, int y) { if(x > y) return 1; else return 0; } int main(void) { printf('%d',max(6,7,greater_than)); } It's just there to test function pointers. But the c...
Nova Shadowflame
1

votes
2

answer
51

Views

C Programming: malloc() inside a function

I would like to allocate memory in a function and then use that space in the main(). All is fine in the function, but I never can access the data in the main(). There are two memory allocation instructions for simulating a two dimensionnal array of chars Here's my code: #include #include #include...
Noury
1

votes
1

answer
54

Views

C++ Why emplacing object in vector segfaults?

I want to create a vector of 'Act' objects that contain pointers to either 'Eat' or 'Drink' dynamically allocated objects. The new objects are being emplaced like so: action_vector.emplace_back(Act::BehaviorType::eat); However, it is seg-faulting and I can't figure out why. I thought that emplace_ba...
user2520385
1

votes
2

answer
29

Views

Set char pointer to NULL after using in a function

In C, I have a function in which I am getting a string as a parameter and then after using it, I want to destroy it, because I have to call it in an infinite loop and getting Process returned -1073741819 (0xC0000005) after 5 minutes. Here's my function: void renderText(char *text) { //use it here an...
Usman Developer
10

votes
0

answer
332

Views

Swapping Property GetMethod implementation runtime

I'm currently trying to swap a property get implementation by replacing it with a bit of IL. I was using this question as reference: How to replace a pointer to a pointer to a method in a class of my method inherited from the system class? The only difference I have is that my method is declared via...
Stijn Bernards
1

votes
2

answer
88

Views

Object representation of char * and void *

There is a claim in the Standard at 6.2.5(p28): A pointer to void shall have the same representation and alignment requirements as a pointer to a character type. I don't really understand that. The object representation is defined as a content of unsigned char[sizeof(the_type)] obtained by memcpy....
Some Name
1

votes
1

answer
55

Views

Opaque struct that has some private operations

I'm designing a library and came to an issue about separating operations that are public and library private. I have the following library interface: libmylib.h: typedef struct lib_context lib_context; int init_library_context(const char **paths, size_t path_num, lib_context **ctx_out); void relea...
St.Antario
1

votes
2

answer
72

Views

Why is my code failing to copy one string to another? [closed]

I want to write a C function called append() the target of which would be to append a character at the end of a given string. But, I am having trouble to get my code work because of some pointer-related issue. #include void append(char ** string, int length, char ch) { printf('%s', string); char *...
user366312
1

votes
3

answer
62

Views

Reassign a smart pointer in a function without passing ownership?

Background With normal pointers, I can do something like the following void conditional_reassign(MyClass* ptr) { if (my_condition) { delete ptr; ptr = new MyClass(new_param); } } And I can pass in the pointer I want to change like following MyClass* ptr = new MyClass(old_param); conditional_reassign...
Woofas
1

votes
1

answer
43

Views

What should we return at the functions with char *func(char *string) ?? Need Brief explanation about how this is different from char[] in c?

I need to know how char * functions works and how we can manipulate char * array in a function and return it. Take help of following code for explanations and why it is not printing anything?? And I can't use std::string. It was a function based question in which i could not use std::string. and ha...
sachin bhandari

View additional questions