pointers

C: Function returning via void *

Coming from Java I'm confused by the use of Void allowing a return value in the following: void *emalloc(size_t s) { void *result = malloc(s); if (NULL == result) { fprintf(stderr, "MEMORY ALLOCATION FAILURE\n"); exit( EXIT_FAILURE ); } return result; } Is this returning a pointer to a chuck o...

How do I get an address in C?

There is probably a really simple answer to this but I just can't see it. #include <stdio.h> int main(void) { int x = 0; printf("\n%d\n%d\n",x,&x); } Okay, so the printf gives 0 (x) and 2293752 (&x). I am trying to figure out a way to look at any address (&) and see what is already stored there. This is just for experimentation. Any ...

How can I convert an offset in text segment of an Win32 executable into a pointer at runtime?

How can I convert an offset in text segment of an Win32 executable into a pointer at runtime? When using a disassembler, I can see the relative addresses. But how can I convert them to an absolute address at runtime? For example: .text:402BE620 Which address is that at runtime? How can I convert that number into a pointer? Some bac...

How to read/copy ctype pointers into python class?

This is a kind of follow-up from my last question if this can help you. I'm defining a few ctype structures class EthercatDatagram(Structure): _fields_ = [("header", EthercatDatagramHeader), ("packet_data_length", c_int), ("packet_data", POINTER(c_ubyte)), ("work_count", c_ushort)] class EthercatPacket(Structu...

Pointer question

Okay I go through 2 layers of functions fun1 calls func2 calls func3 . I pass a pointer all the way down using basically int *ptr, at the lowest "level" of the call stack I also have another function that dynamically allocates memory for an int array. At the top level (func1 level) I always get null back for the passed pointer. I have tr...

Get pointer to class of instance variable in Objective-C

I have an object that has several properties. If I have a pointer to one of those properties, is it possible to get a pointer to the class instance to which that ivar belongs? for example: foo.bar, where I know the address of bar, can the address of foo be found? This seems related to: run time references but I didn't see any referen...

C++ variable data being overwritten

Hi, Been a few years since I've written C/C++, and now I'm facing a problem I just cannot seem to solve on my own. Given the following struct: struct InputData { float diameter; float length; int vertIndex; struct InputData *parent; vector<InputData*> children; bool deadEnd; InputData(float dia, float lngt...

Is it a bad idea to use pointers as loop incrementers instead of the usual "int i"?

An example of this would be: char str[] = "Hello"; int strLength = strlen(str); for ( char * pc = str; pc < str + strLength; pc++) { *pc += 2; } Edit: Accounted for write-protected memory issue. ...

Faster (unsafe) BinaryReader in .NET

I came across a situation where I have a pretty big file that I need to read binary data from. Consequently, I realized that the default BinaryReader implementation in .NET is pretty slow. Upon looking at it with Reflector I came across this: public virtual int ReadInt32() { if (this.m_isMemoryStream) { MemoryStream str...

Problem with C callback function in C# - how to pass value to a pointer?

Hi, I've a C callback defined as follows: Int16 (CALLBACK *ProcessMessage)(Uint16 ServerId, const char PTR *RequestMsg, Uint32 RequestSize, char PTR **ResponseMsg, Uint32 PTR *ResponseSize, Int16 PTR *AppErrCode); An exemple of using this callback in C: Int16 CALLBACK ProcessMessage(Uint16 ServerId, const char PTR *Request...

are all data pointers of the same size in one platform?

Are char*, int*, long* or even long long* of same size (on a given platform)? Thanks ...

Pointer Arithmetic and Navigating malloc'ed arrays.

Okay, so I'm working on an OpenGL ES application for the iPhone, and I ran into an interesting issue. I have a function that computes the vertices, normals, and texture coordinates of a sphere dependent upon a detail level and a range of spherical coordinates. Originally, storing a vertex in an array looked something like this: //Afte...

IImage* from IBitmapImage*

I'm using the Imaging API to basically write a rescaling function using the Imaging built-in Resize function. I need to feed a custom UBitmap class that it's constructed with an Image* pointer with the resized function, so here's the Rescale function proto: int Rescale(const UBitmap* src, UBitmap* dst, UINT w, UINT h) My function all...

Is there different about the following memory allocation?

There are four ways to dynamic allocate memory, is there differences among the four ways? first like this: char *seq=(char *)malloc(100*sizeof(char)); void exam(char *seq){ // using 'seq' } second like this: char *seq; void exam(char *seq){ seq=(char *)malloc(100*sizeof(char)); // using 'seq' } third like this: char *s...

Accessing a pointer in an object's internal structure

I'm using the pyOpenSSL interface to the OpenSSL library but it is missing some functions I need and I can't or don't want to modify it to support these methods (for various reasons). So what I want to achieve, is to retrieve the OpenSSL object pointer. After that, I will be able to call the missing functions through ctypes. What is the...

Are C++ zero (null) pointers supposed to return false?

I'm not sure if my understanding of C++ is wrong.. I've read that 1) all non-zero values are equivalent to TRUE, and zero is equivalent to FALSE; 2) null pointers are stored as zero. Yet code like this: void ViewCell::swapTiles (ViewCell *vc) { ViewTile *tmp = vc->tile(); [stuff ...] if (tmp) addTile(tmp); } Gives me a se...

Is it safe to read past the end of an array?

Let's say I have a constructor like this: MyColor(uint8 vec[]) { r = vec[0]; g = vec[1]; b = vec[2]; a = vec[3]; } But I call it like this (3 elements instead of 4): uint8 tmp[] = {1,2,3}; MyColor c(tmp); But now vec[3] is undefined... is it safe to assign this value to a? If not, there's no nice workaround to check if vec[3] is se...

Reading a pointer from XML without being sure the relevant Obj-C instance exists

I have a "parent" Obj-C object containing (in a collection) a bunch of objects whose instance variables point to one another, possibly circularly (fear not, no retaining going on between these "siblings"). I write the parent object to XML, which of course involves (among other things) writing out its "children", in no particular order, a...

What's the proper use of printf to display pointers padded with 0s

In C, I'd like to use printf to display pointers, and so that they line up properly, I'd like to pad them with 0s. My guess was that the proper way to do this was: printf("%016p", ptr); This works, but this gcc complains with the following message: warning: '0' flag used with ‘%p’ gnu_printf format I've googled a bit for it, and the...

Difference between Foo *foo; and Foo foo; in C++

I know the basics of pointers. I would just like to know when you would use Foo *foo; instead of Foo foo; and what one allows you to do that the other doesn't. Thanks in advance ...