pointers

Structure memory allocation

struct node{ int data; struct node * next; }; How does the compiler allocate memory for "next" member in when we have not yet allocated memory for the structure "struct node" ...

Making a pointer that points to two bytes

Hello. I'm a complete novice in everything except maybe breathing, so sorry if I'm not being clear, but here goes: I have a function in C which writes bytes to a circuit via an I2C bus, and in the header file it looks like this: BOOL WINAPI JidaI2CWrite(HJIDA hJida, DWORD dwType, BYTE bAddr, LPBYTE pBytes, DWORD dwLen); hJida: Bo...

Map pointers to immutable objects with Hashtable in .NET

I have a Hashtable object which "names" or "map" various fields in a class with a string ref class Interrupt{ Interrupt(){ this->type = 0; this->size = 0; } int type; int size; } Interrupt^ interrupt = gcnew Interrupt(); Hashtable^ map = gcnew Hashtable(); map->Add("InterruptType", interrupt->type); map->Add("Interrup...

Casting between void * and a pointer to member function

Hello Stackoverflow, I'm currently using GCC 4.4, and I'm having quite the headache casting between void * and a pointer to member function. I'm trying to write an easy-to-use library for binding C++ objects to a Lua interpreter, like so: LuaObject<Foo> lobj = registerObject(L, "foo", fooObject); lobj.addField(L, "bar", &Foo::bar); ...

How to pointer-cast Foo** to const Foo** in C++

I have class Fred { public: void inspect() const {}; void modify(){}; }; int main() { const Fred x = Fred(); Fred* p1; const Fred** q1 = reinterpret_cast<const Fred**>(&p1); *q1 = &x; p1->inspect(); p1->modify(); } How would it be possible to do the const Fred** q1 = &p1 via pointer-casting? (I have just been reading...

Convert a pointer to an array in C++

Hi, The CreateFileMapping function returns a pointer to a memory mapped file, and I want to treat that memory mapping as an array. Here's what I basically want to do: char Array[] = (char*) CreateFileMapping(...); Except apparently I can't simply wave my arms and declare that a pointer is now an array. Do you guys have any idea how...

Storing a List of Objects

Let's say I have a generic Object class, and a generic List class. I want to maintain a list of these Objects. Should I store them as List<Object> or List<Object*>? If I use List<Object> and I have a method like: if(some_condition) { Object obj; myObjectList.append(obj); } And my list class only keeps a reference to the objec...

How can I find the size of the memory referenced by a pointer?

GetMem allows you to allocate a buffer of arbitrary size. Somewhere, the size information is retained by the memory manager, because you don't need to tell it how big the buffer is when you pass the pointer to FreeMem. Is that information for internal use only, or is there any way to retrieve the size of the buffer pointed to by a poin...

Passing a modifiable parameter to c++ function

Hi, everyone! Provided, I want to pass a modifiable parameter to a function, what should I choose: to pass it by pointer or to pass it by reference? bool GetFoo ( Foo& whereToPlaceResult ); bool GetFoo ( Foo* whereToPlaceResult ); I am asking this because I always considered it the best practice to pass parameter by reference (1), b...

Marshaling a pointer to an array of types (managed C# -> unmanaged C++)

I am having some trouble settling on a way to represent a structure that contains a pointer to an array of shorts in my managed code. The struct looks like this: typedef struct { short size; unsigned short** shortValues; } UnmanagedStruct; memory for 'shortValues' is allocated inside unmanaged code -- therefore even though th...

Converting this C signature to C# for P/Invoke

I have the following C function: int w_ei_connect_init(ei_cnode* ec, const char* this_node_name, const char *cookie, short creation); ei_cnode looks like this: typedef struct ei_cnode_s { char thishostname[EI_MAXHOSTNAMELEN+1]; char thisnodename[MAXNODELEN+1]; char thisalivename[EI_MAXALIVELEN+1]; ...

using pointers to get values back from a function (c++)

i have a function to calculate several different values: int ASPfile::get_dimensions(int* Lat, int* Lon, vector<double>* Latgrid, vector<double>* Longrid) { _latsize = get_latsize(); _lonsize = get_lonsize(); Lat = &_latsize; Lon = &_lonsize; latgrid = read_latgrid(); longrid = read_longrid(); *Latg...

If I have a void pointer, how do I put an int into it?

I have an array of arbitrary values, so I have defined it as an array of void pointers, so I can point to any kind of information (like int, character arrays, etc). However, how do I actually assign an int to it? Take for example these initializations: void* data[10]; int x = 100; My intuition would think this, but this gives a compi...

When to use pointers, and when not to use them

I'm currently doing my first real project in C++ and so, fairly new to pointers. I know what they are and have read some basic usage rules. Probably not enough since I still do not really understand when to use them, and when not. The problem is that most places just mention that most people either overuse them or underuse them. My ques...

C pointers vs direct member access for structs

Say I have a struct like the following ... typedef struct { int WheelCount; double MaxSpeed; } Vehicle; ... and I have a global variable of this type (I'm well aware of the pitfalls of globals, this is for an embedded system, which I didn't design, and for which they're an unfortunate but necessary evil.) Is it faster to access t...

Circular reference in C++ without pointers

Hi, Is there a way to define circular references without using pointers? I need to have somthing like this: struct A; struct B { A a; }; struct A { B b; }; Thanks! ...

C: differences between pointer and array

Hi, I know that similar questions are posted in SO, but I thought I can ask this in the following the context: char amessage[] = "now is the time"; char *pmessage = "now is the time"; I read from The C Programming Language, 2nd Edition that the above two statements don't do the same thing. I always thought that an array is an conven...

Initialize a pointer to a class with NULL values

I am tring to intialize an array of pointers to a NODE struct that I made struct Node{ int data; Node* next; }; the private member of my other class is declared as Node** buckets; It is currently initialised as buckets = new Node*[SIZE] Is there anyway to initialize the array so that its members point to NULL or some ot...

Passing a constant matrix

Referring to this question and especially the accepted answer of litb, I wonder why the gcc complain about this: void func(const int (*ip)[3]) { printf("Value: %d\n", ip[1][1]); } int main() { int i[3][3] = { {0, 1, 2} , {3, 4, 5}, {6, 7, 8} }; func(i); return 0; } If I eliminate the const the compiler keeps still. Di...

-fno-omit-frame-pointer without optimization

Hi, I was wondering what -fno-omit-frame-pointer will do without optimization? CXXFLAGS = -Wall -ggdb3 -DDEBUG -fno-omit-frame-pointer Isn't it that fomit-frame-pointer auto turned on at all levels of -O (except -O0)? I assume in my example it is -O0 by default. Thanks and regards! ...