pointers

What are the barriers to understanding pointers and what can be done to overcome them?

Why are pointers such a leading factor of confusion for many new, and even old, college level students in C or C++? Are there any tools or thought processes that helped you understand how pointers work at the variable, function, and beyond level? What are some good practice things that can be done to bring somebody to the level of, "Ah...

How do you pass a function as a parameter in C?

I want to create a function that performs a function passed by parameter on a set of data. How do you pass a function as a parameter in C? ...

Passing more parameters in C function pointers

Let's say I'm creating a chess program. I have a function void foreachMove( void (*action)(chess_move*), chess_game* game); which will call the function pointer action on each valid move. This is all well and good, but what if I need to pass more parameters to the action function? For example: chess_move getNextMove(chess_game* game,...

Pointer Manipulation Help

Hi everyone, I am trying to build a function in C/c++ to sort an array and replace each value with its "score" or rank. It takes in a double pointer array to an array of ints, and sorts the double pointers based on the dereferenced value of the integers. I have tried quite a few times to make it work, but cant get it down. Once again,...

How to keep track of the references to an object?

In a world where manual memory allocation and pointers still rule (Borland Delphi) I need a general solution for what I think is a general problem: At a given moment an object can be referenced from multiple places (lists, other objects, ...). Is there a good way to keep track of all these references so that I can update them when the o...

What is the best way to learn recursion?

When I started in programming I started with c++ and was doing recursion in my second semester in a data structures class. I don't even remember how we started it, I think it was linked lists, but its a concept that I picked up easily and can jump into quickly, even when I haven't written any recursive code in quite some time. What is t...

Pointer to Pointer Managed C++

I have an old C library with a function that takes a void**: oldFunction(void** pStuff); I'm trying to call this function from managed C++ (m_pStuff is a member of the parent ref class of type void*): oldFunction( static_cast<sqlite3**>( &m_pStuff ) ); This gives me the following error from Visual Studio: error C2440: 'static_...

C++ Memory management

I've learned in College that you always have to free your unused Objects but not how you actually do it. For example structuring your code right and so on. Are there any general rules on how to handle pointers in C++? I'm currently not allowed to use boost. I have to stick to pure c++ because the framework I'm using forbids any use of...

Regular cast vs. static_cast vs. dynamic_cast

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts (i.e. MyClass *m = (MyClass *)ptr; all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between th...

Return function pointer to a nested function in C

As the title already states, I'm trying to declare a nested function and return a pointer to that function. I want this function 'not' to return a new function pointer which will return the negation of whatever the original function was. Here is what I have: someType not( someType original ) { int isNot( ListEntry* entry ) { ...

C++ deleting a pointer to a pointer

So I have a pointer to an array of pointers. If I delete it like this: delete [] PointerToPointers; Will that delete all the pointed to pointers as well? If not, do I have to loop over all of the pointers and delete them as well, or is there an easier way to do it? My google-fu doesn't seem to give me any good answers to this quest...

Unsafe C# and pointers for 2d rendering, good or bad?

I am writing a c# control that wraps DirectX 9 and provides a simplified interface to perform 2d pixel level drawing. .NET requires that I wrap this code in an unsafe code block and compile with the allow unsafe code option. I'm locking the entire surface which then returns a pointer to the locked area of memory. I can then write pixel...

Difference between pointer variable and reference variable in C++

I know references are syntactic sugar, so easier code to read and write :) But what are the differences? Summary from answers and links below: A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. A pointer can point to NULL while reference can never point to NULL You can't ta...

C pointers in C#

Is this function declaration in C#: void foo(string mystring) the same as this one in C: void foo(char *) i.e. In C#, does the called function receive a pointer behind the scenes? ...

Find memory leaks caused by smart pointers

Does anybody know a "technique" to discover memory leaks caused by smart pointers? I am currently working on a large project written in C++ that heavily uses smart pointers with reference counting. Obviously we have some memory leaks caused by smart pointers, that are still referenced somewhere in the code, so that their memory does not ...

What use is multiple indirection in C++?

Under what circumstances might you want to use multiple indirection (that is, a chain of pointers as in **foo) in C++? ...

Why can't I convert 'char**' to a 'const char* const*' in C?

The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc & g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care): char **a; const char** b = a; I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows ...

What is the real difference between Pointers and References?

AKA - What's this obsession with pointers? Having only really used modern, object oriented languages like ActionScript, Java and C#, I don't really understand the importance of pointers and what you use them for. What am I missing out on here? ...

In C++ I Cannot Grasp Pointers and Classes

I'm fresh out of college and have been working in C++ for some time now. I understand all the basics of C++ and use them, but I'm having a hard time grasping more advanced topics like pointers and classes. I've read some books and tutorials and I understand the examples in them, but then when I look at some advanced real life examples ...

MIPS Assembly Pointer to a Pointer?

I think I know how to handle this case, but I just want to make sure I have it right. Say you have the following C code: int myInt = 3; int* myPointer = &myInt; int** mySecondPointer = &myPointer; P contains an address that points to a place in memory which has another address. I'd like to modify the second address. So the MIPS cod...