pointers

Are there benefits of passing by pointer over passing by reference in C++?

Are there benefits of passing by pointer over passing by reference in C++? Lately, I have seen a number of examples that pass the a pointer instead of passing by reference. Are there benefits to doing this? Example: func(SPRITE *x); with a call of func(&mySprite); vs. func(SPRITE &x); with a call of func(mySprite); ...

What does a const pointer-to-pointer mean in C and in C++?

I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I knew what was going on until a colleague told me that: const MyStructure** ppMyStruct; means "ppMyStruct is a pointer to a const pointer to a (mutable) MyStructure" (in C++). I would have thought it meant "ppMyStruct is a pointer to a pointer to a con...

How does one declare an array of constant function pointers in C?

I need to declare an array of pointers to functions like so: extern void function1(void); extern void function2(void); ... void (*MESSAGE_HANDLERS[])(void) = { function1, function2, ... }; However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do n...

How to find out if a pointer is on the stack on PC/Visual C++

[This is for PC/Visual C++ specifically (although any other answers would be quite illuminating :))] How can you tell if a pointer comes from an object in the stack? For example: int g_n = 0; void F() { int *pA = &s_n; ASSERT_IS_POINTER_ON_STACK(pA); int i = 0; int *pB = &i; ASSERT_IS_POINTER_ON_STACK(pB); } so o...

Implementing a tree from scratch

Hello everyone, I'm trying to learn about trees by implementing one from scratch. In this case I'd like to do it in C# Java or C++. (without using built in methods) So each node will store a character and there will be a maximum of 26 nodes per node. What data structure would I use to contain the pointers to each of the nodes? Basica...

C Pointer confusion

Hi! I want to store a string in memory and read it later: $$->desc.constant->base.id = (char*)malloc(200); sprintf($$->desc.constant->base.id, "%f", $1); printf("->%s\n", $$->desc.constant->base.id); //LINE A printf("->%i\n", $$->desc.constant); //LINE B //SOME OTHER CODE //Then, later on in a function call: printf("%i", expr->desc...

Accessing a bidimensional(or tridimensional) array through a pointer

When you have an array like this: int foo[3][2][2]; and you make: int *bar = &foo[0][0][0]; Is this the way it works? *bar == foo[0][0][0]; *(bar+1) == foo[0][0][1]; *(bar+2) == foo[0][1][0]; *(bar+3) == foo[0][1][1]; *(bar+4) == foo[1][0][0]; I'm not sure and have a bit of code dependent on if that works. ...

ANSI C: Assigning arrays and pointers to arrays

The following is a simplified version of what I'm trying to do, because I'm sure you don't want to wade through an entire set of structs and function prototypes for a particle system. float const materials[24][4][4] = {{{...}}}; typedef struct EmitterStruct { float *material[4][4]; } Emitter; typedef struct ParticleStruct { float mater...

How do I pass pointers to a DLL using Win32:API?

Hi, I am trying to passing in 3 pointers to a DLL function. I have: { $code=1; $len=100; $str=" " x $len; $function = new Win32::API(DLLNAME,'dllfunction','PPP','V'); $function->Call($code,$str,$len); } The DLL is defined as void dllfunction(int* a, char* str, int* len); The DLL will modify all the variables pointed by the three ...

In objective-c, how can I tell the difference between a Class and an instance of a class?

Let's say I have a generic pointer in objective-c. This pointer could either be a Class object, or it could be an instance of that class. Is there any way to tell the difference between the two? Example: id pointerA = [someClass class]; id pointerB = [[someClass alloc] init]; bool pointerAIsAClass = is_this_a_Class(pointerA); // shou...

Have you come across any reason for three levels of indirection?

Just flicking through one of my favourite books (Ellen Ullman's The Bug) and there is a small bit where one programmer confronts another over three levels of indirection: ***object_array = ***winarray; I get the idea of double indirection - a way of passing a pointer into a function, and allowing it to point to an object created withi...

Special Meaning for Pointer Value 0x7c7c7c7c

While debugging a Linux app, I found a pointer with the suspicious value 0x7c7c7c7c. Does that particular value indicate anything? (I ask because I know from my MSVC days that in a debug build, values like 0xcdcdcdcd or 0xdddddddd would be stored into heap blocks that were uninitialized, freed, or otherwise invalid. Some people use ma...

Can you declare a pointer as extern in C++?

I have the following bit of legacy C++ code that does not compile: #include <stdio.h> #include <iostream> extern ostream *debug; GCC (g++) complains: "expected initializer before ‘*’ token" Looking around it seems more common to declare these as external references, like this: extern ostream& debug; Why is a pointer not valid, bu...

What is the origin of the term "baller" which means "pointer"?

I've seen the term "baller" used in a couple of C++ interview tests. It means "pointer" as best as I can tell from questions like "Describe the difference between a baller and a reference." I thought that perhaps it was an Indian term due to some Google deduction, but a couple of Indian co-workers who went to school in India said they've...

Execution speed of references vs pointers

I recently read a discussion regarding whether managed languages are slower (or faster) than native languages (specifically C# vs C++). One person that contributed to the discussion said that the JIT compilers of managed languages would be able to make optimizations regarding references that simply isn't possible in languages that use po...

C++ char array with stdin

I am trying to get the size of an array populated by stdin: char *myArray; cin >> myArray cout << sizeof(myArray); This returns 4 when I enter a string greater with a length greater than 4 e.g. "40905898" Where am i going wrong? ...

What is the point of pointer types in C++?

Let's say I have some pointers called: char * pChar; int * pInt; I know they both simply hold memory addresses that point to some other location, and that the types declare how big the memory location is pointed to by the particular pointer. So for example, a char might be the size of a byte on a system, while an int may be 4 bytes.. ...

In C arrays why is this true? a[5] == 5[a]

As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a] Joel says that it's because of pointer arithmetic but I still don't understand. Why does a[5] == 5[a] ? Edit: The accepted answer is great. For a lower level view of how this works, se...

Check for pointer definedness in C++

How do I check if a variable, specifically a pointer, is defined in C++? Suppose I have a class: class MyClass { public: MyClass(); ~MyClass() { delete pointer; // if defined! } initializePointer() { pointer = new OtherClass(); } private: OtherClass* pointer; }; ...

smart pointers + "this" considered harmful?

In a C++ project that uses smart pointers, such as boost::shared_ptr, what is a good design philosophy regarding use of "this"? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-sta...