pointers

C++ shared_ptr - attach to a new raw pointer?

I think I'm missing something simple here. I'm using Boost's shared_ptr. shared_ptr<Foo> pA(new Foo()); shared_ptr<Foo> pB(new Foo()); Now, I want to switch pB so it contains the contents of pA, decrementing the ref count of pB. How can I do this? ...

copy two structs in C that contain char pointers

what is the standard way to copy two structs that contain char arrays? Here is some code #include stdio.h> #include string.h> #include stdlib.h> typedef struct { char* name; char* surname; } person; int main(void){ person p1; person p2; p1.name = (char*)malloc(5); p1.surname = (char*)malloc(5); str...

Double free coruption

Hi, I have a class call grid. The class holds two 2d char arrays for storing a grid... The class has two functions for creating the memory for the grid and releasing the memory for the grid. Grid.h private: char **gridOne; char **gridTwo; Grid.cpp void Grid::allocateGridMem() { _gridOne = new char*[gridRowCount()]; _gridTwo =...

Ruby - How to reproduce the pointers/references behavior ?

Hey :) I am currently trying to create an engine for a new application. Design is quite complex and i realy need a way to create a reference or a pointer or what ever way to remotely modify and access the datas. Datas must be kept in a single place and must not be duplicated. Is there a way do get an object by reference ? For example ...

Pointer best practice

I am just starting to learn C and (of course) struggling with pointers :) Given this snippet: int *storage, *storage_p; storage = malloc(sizeof(int[GROW_BY])); storage_p = storage; // do something with storage, using storage_p free(storage); storage = NULL; Is it really necessary to have two variables declared to work with the malloc...

XCode warning: argument 3 discards qualifiers from pointer target type

Hello, XCode is giving me a warning that I can't figure out why. @interface SFHFKeychainUtils : NSObject { } + (BOOL) storeUsername: (NSString *) username andPassword: (NSString *) password forServiceName: (NSString *) serviceName updateExisting: (BOOL) updateExisting error: (NSError **) error; @end In another class I call storeUse...

passing pointers to function that takes a reference?

In C++, when you have a function that takes a reference to an object, how can you pass an object pointer to it? As so: Myobject * obj = new Myobject(); somefunc(obj); //-> Does not work?? Illegal cast?? somefunc(Myobject& b) { // Do something } ...

The purpose behind empty struct ?

Declarations of auto_ptr from C++ Standard Library namespace std { template <class Y> struct auto_ptr_ref {}; template <class X> class auto_ptr { public: typedef X element_type; // 20.4.5.1 construct/copy/destroy: explicit auto_ptr(X* p =0) throw(); auto_ptr(auto_ptr&) throw(); templa...

const pointer assign to a pointer

Why can I not do this: char* p = new char[10]; void SetString(char * const str) { p = str; } SetString("Hello"); I have a const pointer to a char, why can I not assign the const pointer to another pointer? It just seems illogical, as by assigning it to another pointer, you are not essentially violating the const-ness of the ch...

Writing structure into a file in C

I am reading and writting a structure into a text file which is not readable. I have to write readable data into the file from the structure object. Here is little more detail of my code: I am having the code which reads and writes a list of itemname and code into a file (file.txt). The code uses linked list concept to read and write d...

C++. Class method pointers

There is a class class A { public: A() {}; private: void func1( int ) {}; void func2( int) {}; }; I want to add a function pointer which will be set in constructor and points to func1 or func2. So I can call this pointer (as class member) from every class procedure and set this pointer in constructor. How can I do it...

Pointer to object

I have the following: class Person { public: char Name[20]; char Surname[20]; char Address[256]; }; Person myperson; myperson.Name = "Andy"; Person * p_person = new Person(); AlterPersonFunction(p_person); //Coming out of AlterPersonFunction myperson now contains values of p_pers??? void AlterPersonFunction(Person *p_pe...

The use case of 'this' pointer in C++

I understand the meaning of 'this', but I can't see the use case of it. For the following example, I should teach the compiler if the parameter is the same as member variable, and I need this pointer. #include <iostream> using namespace std; class AAA { int x; public: int hello(int x) { this->x = x;} int hello2(int y) {x...

Deleting a nested struct with void pointers as members?

I have the following class: class Stack { struct Link { void* data; Link* next; void initialize(void* dat, Link* nxt); }* head; public: void initialize(); void push(void* dat); void* peek(); void* pop(); void cleanup(); }; The pop method is: void* Stack::pop() { if(head == 0) return 0; void* result = hea...

C++ Problem with destructor called when removing element from STL container

Say I have 2 containers storing pointers to the same objects... std::list<Foo*> fooList; std::vector<Foo*> fooVec; Lets say I remove an object from one of these containers via one if its methods, for example... std::vector<Foo*>::iterator itr = std::find( fooVec.begin(), fooVec.end(), pToObj ); fooVec.erase( itr ); CppReference ...

iPhone, objective c reassign and return pointer of method

Hey guys, lately I have been asking quite a few questions about memory management on the iPhone. Fortunately things are getting clearer. But I still struggle when it gets more complex: So is there something wrong with this in terms of memory mangement? My question and suggestions are in the comments... //I get a text from a textfield NS...

Manipulating data members (C++)

I have a method that takes an object as an argument. Both the caller and argument have the same members (they are instances of the same class). In the method, particular members are compared and then, based on this comparison, one member of the argument object needs to be manipulated : class Object { // members public: someMet...

How to declare triple pointers in array of pointers...

How to declare a triple pointer with array of pointers like i have char *mainmenu[] = {"menu1", "menu2", "menu3"} see the picture How to connect my menu1,2,3 with those from the picture m1p1 m2p1 ??? I just need the syntax please help me ... ...

Why is my multi-dimensional dynamic allocation in C not working?

Hi all, I have been trying to figure out the problem with my allocation and use of a multidimensional dynamically allocated array in C. I'd really appreciate any help. I've tried two approaches. The first: cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **)); for(i=0; i<NUM_REGIONS; i++){ cdr[i] = (double **) malloc(numRating...

Unsafe C# - Instantiate Pointer at lowest memory address and overwrite

Is it possible to instantiate a pointer using (unsafe) C# at any given memory address (which one would be the lowest?) and simply start overwriting the memory with continuous random data until a PC crashes? Could someone provide me with an example? ...