pointers

C++ by-reference argument and c linkage

Hi, I have encountered a working (with XLC8 and MSFT9 compilers) piece of code, containing a c++ file with a function defined with c linkage and a reference argument. This bugs me, as references are c++ only. The function in question is called from c code, where it is declared as taking a pointer argument to the same type in place of th...

pointer to array

I'm wondering, can you make a pointer to a group of variables in an array? like this array[20]{'a','b','c',...} pointer = array[6 through 10]; so then you could say... *pointer[0] == array[6]; and *pointer[5] == array[10]; and the length of *pointer 5 == sizeof(*pointer) \ sizeof(type); OK Let me explain what I'm trying to ...

What do I need to know about memory in C++?

I've been doing my best to learn C++ but my previous training will fall short in one major issue: memory management. My primary languages all have automatic garbage collection, so keeping track of everything has never really been necessary. I've tried reading up on memory management in C++ online, but I have this shaking suspicion that...

Can using 0L to initialize a pointer in C++ cause problems?

In this question an initializer is used to set a pointer to null. Instead of using value of 0 value of 0L is used. I've read that one should use exactly 0 for null pointers because exact null pointer representation is implementation-specific. Can using 0L to set a pointer to null cause problems while porting? ...

Is C++ the single language that have both pointers and references?

Amongst the programming languages I know and those I've been exposed to, C++ looks like the only one to have both pointers and references. Is it true? ...

Associated pointers in derived type? gFortran vs. Intel

Hello, I would like to check if a pointer inside a derived type has already been defined or not. I wrote the following simple code to show you my problem: program test implicit none type y real(8), pointer :: x(:) end type y type(y), pointer :: w(:) allocate(w(2)) allocate(w(1)%x(2)) write(*,*) associated(w(1)%x), associated(w(...

[C++] Why aren't pointers initialized with NULL by default?

I guess this have been answered before, but I just couldn't find the answer here or on Google, but I think that it is because I couldn't type the right question... Can someone please explain why aren't pointers initialized to NULL? Example: void test(){ char *buf; if (!buf) // whatever } The program wouldn't ste...

Is there a use for uninitialized pointers in C or C++?

In one of the comments in this question, it was brought out that initializing C++ pointers by default would break compatibility with C. That's fine, but why would something like this matter? I would think the only time it would actually matter is if I wanted an uninitialized pointer for some reason. But I can't think of a reason why I...

Pointer to a casted Pointer?

I've come across pointers to casted pointers (not sure that this is the correct term) in C such as: *(long *) p = 10; I could never for the life of me understand what it means, or, the other example: *(void *) NULL, or *(char *) 0; I just can't wrap my head around it, could someone please explain this to me, and save me from partial br...

Why is pointer-to-pointer being used in this prog ?

The following program shows how to build a binary tree in a C program. It uses dynamic memory allocation, pointers and recursion. A binary tree is a very useful data-structure, since it allows efficient insertion, searching and deletion in a sorted list. As such a tree is essentially a recursively defined structure, recursive programming...

C# -- Create Managed Array from Pointer

Dear Sirs, I'm trying to create a Managed Array of doubles from an array of bytes. I have the problem working currently, but I wanted to optimize. Here's some code that I would like to work: private unsafe static double[] _Get_Doubles(byte[] _raw_data) { double[] ret; fixed (byte* _pd = _raw_data) { double* _pret =...

C. Segmentation Fault when function modifies dynamically allocated 2d array

What i need is a function that modifies given pointer to 2d matrix like this: void intMatrixAll(int row, int col, int **matrix); Now, a function should allocate memory and the matrix could be used. Rows and cols are given at run-time. #include <stdio.h> #include <stdlib.h> #define PRINTINT(X) printf("%d\n", X); void intMatrixAll(in...

Testing for a non-null pointer, and returning null otherwise

I'm wondering whether it's considered okay to do something like this. if ( p_Pointer != NULL ) { return p_Pointer; } else { return NULL; } Without the else, whatever. The point is that if the pointer is null, NULL is going to be returned, so it would seem pointless wasting a step on this. However, it seems useful for debugging pur...

Checking for null before pointer usage

Most people use pointers like this... if ( p != NULL ) { DoWhateverWithP(); } However, if the pointer is null for whatever reason, the function won't be called. My question is, could it possibly be more beneficial to just not check for NULL? Obviously on safety critical systems this isn't an option, but your program crashing in a b...

Pass by reference in C

I'm trying to use pass by reference in C so that the function can modify the values of the parameters passed to it. This is the function signature: int locate(char *name, int &s, int &i) However when I try to compile it I get this error that refers specifically to the above line: error: expected ‘;’, ‘,’ or ‘)’ before '&' token...

pointer vs handles in C (are the terms used to convey separate things?)

Recently, I read a white paper by an individual who refers to a pointer to a struct as a handle. The author was clearly someone who had written C code on the windows platform previously. Googling indicates that windows programmers interact with system components via handles. I am wondering if it is common practice for windows programm...

How to serialize shared/weak pointers?

I have a complex network of objects connected with QSharedPointers and QWeakPointers. Is there a simple way to save/load them with Boost.Serialization? So far I have this: namespace boost { namespace serialization { template<class Archive, class T> void save(Archive& ar, QSharedPointer<T> const& ptr, unsigned version...

Is Pointer-to- " inner struct" member forbidden ?

Hi! i have a nested struct and i'd like to have a pointer-to-member to one of the nested member: is it legal? struct InnerStruct { bool c; }; struct MyStruct { bool t; bool b; InnerStruct inner; }; this: MyStruct mystruct; //... bool MyStruct::* toto = &MyStruct::b; is ok but: bool MyStruct::* toto = &MyStruct::in...

nearest neighbor mapping of 1D index for 2D array into a smaller 2D array

This is in C. I have two 2D arrays, ArrayA and ArrayB, that sample the same space. B samples a different attribute than ArrayA less frequently than ArrayA, so it is smaller than A. Just to try to define some variables: ArrayA: SizeAX by SizeAY, indexed by indexA for a position posAX, posAY ArrayB: SizeBX by SizeAY, indexed by indexB...

PHP Memory Overflow?

Hi all, Im currently building a php framework... again. I have a class called config. its pretty simple, its called like so: $conf = config::get('general'); $conf is now an array full of config goodies. the class sceleton is like so: final class config { private static $configs = array(); public static function get($name) { ret...