pointers

Creating own FILE* pointer in C?

Hi, I had just a look at the stdio.h where I could find the FILE structure definition: typedef struct { int level; /* fill/empty level of buffer */ unsigned flags; /* File status flags */ char fd; /* File descriptor */ unsigned char hold; /* Unget...

How are array and pointer types handled internally in C compilers? ( int *a; vs. int a[]; )

I need a language lawyer with authoritative sources. Take a look at the following test program which compiles cleanly under gcc: #include <stdio.h> void foo(int *a) { a[98] = 0xFEADFACE; } void bar(int b[]) { *(b+498) = 0xFEADFACE; } int main(int argc, char **argv) { int a[100], b[500], *a_p; *(a+99) = 0xDEADBEEF; *(b+499...

If I store a member as an object, will I incur an object copy during constuction?

If the constructor for Door looks like this: Door::Door(Doorknob doorknob) : m_doorknob(doorknob) { } Then you would instantiate a Door like this: Doorknob doorknob; Door door(doorknob); // Does an object copy of doorknob occur here? It seems like if you store Doorknob as a pointer, you can explicitly avoid the copy: Door::Door(D...

Ensuring correct Double Pointer passing method at compile-time in C++

Hi there, in the past we encountered various memory-leaks in our software. We found out that these happened mostly due to incorrect usage of our own "free"-Methods, which free Queue-Message-Data and the likes. The problem is, in our deepest tool-functions there are two methods to free up dynamically allocated memory, with the followin...

Error calling function and passing a reference-to-pointer with a derived type

Can somebody explain why the following code is not valid? Is it because the offset for the variable named d is different than the variable named b? class Base { public: int foo; }; class Derived : public Base { public: int bar; }; int DoSomething( Base*& b ) { return b->foo; } Base* b = new Derived; Derived* d = new Derived; int mai...

How do I wrap this C function, with multiple arguments, with ctypes?

Hi everyone! I have the function prototype here: extern "C" void __stdcall__declspec(dllexport) ReturnPulse(double*,double*,double*,double*,double*); I need to write some python to access this function that is in a DLL. I have loaded the DLL, but each of the double* is actually pointing to a variable number of doubles (an array), and ...

Pointers in and out of DLLs

Is it possible to pass a pointer to an object into a DLL, initialize it, and then use the initialized pointer in the main application? If so how? Are there any good articles on the subject, perhaps a tutorial? I have read this article http://msdn.microsoft.com/en-us/library/ms235460.aspx But that did not seem to get me any where. Maybe ...

Doing pointer math in a c++ class: Is it "legit"?

Ah-hoi, hoi, I'm wondering if it's ok to do something like the following: class SomeClass { int bar; }; SomeClass* foo = new SomeClass(); int offset = &(foo->bar) - foo; SomeClass* another = new SomeClass(); *(another+offset) = 3; // try to set bar to 3 Just Curious, Dan O ...

Are member functions guaranteed to be ready before the creation of any object?

Consider this example: #include <iostream> class myclass { public: void print() { std::cout << "myclass"; } }; int main() { myclass* p = 0x0; // any address p->print(); // prints "myclass" } I didn't call the member function print through an object of type myclass. Instead I called it from a pointer to a random place in...

Update properties of objects in an IEnumerable<>

Hi, I am working on some software that should be used for a special type of experiment. The experiments are performed using: 1) A "Chip" (basically an XY grid of known dimensions). 2) Each Chip contains "Electrodes", identified by their X and Y coordinate on the chip and by a unique ID. Each electrode can also hold or not hold a sampl...

ctypes pointer question

Hi everyone - I was reading the ctypes tutorial, and I came across this: s = "Hello, World" c_s = c_char_p(s) print c_s c_s.value = "Hi, there" But I had been using pointers like this: s = "Hello, World!" c_s = c_char_p() c_s = s print c_s c_s.value Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> c...

How can I allocate memory and return it (via a pointer-parameter) to the calling function?

I have some code in a couple of different functions that looks something like this: void someFunction (int *data) { data = (int *) malloc (sizeof (data)); } void useData (int *data) { printf ("%p", data); } int main () { int *data = NULL; someFunction (data); useData (data); return 0; } someFunction () and useData () ...

C - Problems with pointers to pointers, can't seem to see it.

Ok guys, just a quick question hopefully someone can find my mistake quickly, but I just can't see it at the moment Here is my struct: typedef struct { Car *buffer[CAR_PARK_SIZE]; char *arrival_time[CAR_PARK_SIZE]; int keep_running; int size; } CarPark; typedef struct { Car *buffer...

a few beginner C questions

I'm sort of learning C, I'm not a beginner to programming though, I "know" Java and python, and by the way I'm on a mac (leopard). Firstly, 1: could someone explain when to use a pointer and when not to? 2: char *fun = malloc(sizeof(char) * 4); or char fun[4]; or char *fun = "fun"; And then all but the last would set indexes 0...

B+ tree implementation, * * vs *

I am writing a B+ tree for a variety of reasons and I am come here to ask a question about implementation of its nodes. My nodes currently look like: struct BPlusNode { public: //holds the list of keys keyType **keys; //stores the number of slots used size_t size; //holds the array of pointers to lower nodes NULL if ...

Question about pointer allignement

I'm working on a memory pool implementation and I'm a little confused about pointers alignment... Suppose that I have a memory pool that hands out fixed size memory blocks, at the point of memory pool creation I malloc((size)*(num of blocks)). If whats being allocated are objects and the size comes from the sizeof operator alignment sh...

Best Practices: Should I create a typedef for byte in C or C++?

The title pretty much says it all, do you prefer to see something like t_byte* (where t_byte would be a typedef for unsigned char) or unsigned char* in code? I'm leaning towards t_byte in my own libraries, but have never worked on a large project where this approach was taken, and am wondering about pitfalls. Regards, Dan O. ...

Safely moving a C++ object

I’ve heard some words of warning against shipping an object to another memory location via memcpy, but I don’t know the specific reasons. Unless its contained members do tricky things that depend on memory location, this should be perfectly safe … or not? EDIT: The contemplated use case is a data structure like a vector, which stores o...

Polymorphism & Pointers to arrays

Hi all, I have a class A: class A { public: virtual double getValue() = 0; } And a class B: class B : public A { public: virtual double getValue() { return 0.0; } } And then in main() I do: A * var; var = new B[100]; std::cout << var[0].getValue(); //This works fine std::cout << var[1].getValue(); //This, ...

Is there a gcc macro for determining that frame pointers are not eliminated?

When -fomit-frame-pointer is used (automatic for various -O settings), performing a backtrace is problematic. I am wondering if there is a way of determining at compile time that the code is compiled with this switch? In that case, I could put in an #ifndef to guard against backtracing when ill-advised. Is any macro set when this -fom...