pointers

what is array decaying?

what is decaying of array? is there any relation to the array pointers? ...

size_t vs. intptr_t

The C standard guarantees that size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type. I've read on some sites that I found on the Googles that this is legal and/or should always work: void *v = malloc(10); size_t s = (size_t) v; So then in C99, the standard introduce...

string doesn't end at NULL but still behaves normally, why?

Hi In the following code, I copy a string in to a char* str, which is 10 characters long, using strncpy(). Now according to strncpy() manual, "Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null terminated. " which is exactly what happens here. The source string is 26 charcter...

[c++ / pointers]: having objects A and B (B has vector member, which stores pointer to A), knowing A is it possible to retrieve pointer to B?

Hello, While trying to learn c++, I tried to implement class representing very basic trie. I came up with the following: class Trie { public: char data; vector<Trie* > children; Trie(char data); Trie* addChild(Trie* ch); // adds child node (skipped others members/methods) }; Method addChild checks if child c...

C++ Check if pointer is passed, else create one?

Okay i've seen this done somewhere before where you have a function that takes a pointer parameter and returns a pointer. However you can choose not to pass a parameter and it will return a dynamically allocated pointer but if you do pass a pointer then it just fills it in instead of creating one on the heap. This is a single function n...

Memory (sbrk) 16-byte aligned shifting on pointer access

I wrote a reasonably basic memory allocator using sbrk. I ask for a chunk of memory, say 65k and carve it up as needed for variables requesting dynamic memory. I free the memory by adding it back to the 65k block. The 65k block is derived from a union sizeof(16-bytes). Then I align the block along an even 16-byte boundary. But I'm gettin...

Initialization between types "const int** const" and "int**" is not allowed, why?

Using V1.8 z/OS XL C compiler, with warnings jacked-up using INFO(ALL), I get the following warning on line 4 of the code below: WARNING CCN3196 Initialization between types "const int** const" and "int**" is not allowed. 1 int foo = 0; 2 int *ptr = &foo; 3 const int * const fixed_readonly_ptr = ptr; 4 const int...

32 bit Address Location confusion... (C Programming)

In C, we can place "&" before a variable to figure out the address of that variable. I have a 32 bit machine. Whenever I print the address in the console, the console displays a 7 digit base 10 number. I just want to know how's that number (10^7) related to the 32-bit machine. (2^32) Thanks ...

C and pointer notation

Opening up the Postgres codebase, I see that much of the C code is written by having pointers with the -> notation in such a way that: (foo)->next = 5; I know that pointer notation has levels of precedence, such that -> = (*foo). and is not the same as *foo. However, does it mean anything when the parentheses are outside the variable...

How to get a string from memory into DTrace script

I have a char* in my DTrace script that I know points to a zero-terminated string. How to access this string? Is there some function like copyin() that handles zero-terminated strings? ...

Python ctypes: copying Structure's contents

I want to mimic a piece of C code in Python with ctypes, the code is something like: typedef struct { int x; int y; } point; void copy_point(point *a, point *b) { *a = *b; } in ctypes it's not possible to do the following: from ctypes import * class Point(Structure): _fields_ = [("x", c_int),("y", c_int)] def copy_point(a,...

Can the Size of Pointers Vary Depending on what's Pointed To?

I was just reading the section of the C FAQ on pointers. It discusses not being able to use void * pointers to hold function pointers because pointers to data and pointers to functions may have differing sizes on some platforms and void * is only guaranteed be large enough to hold pointers to data. Can anyone give an example of a platf...

Memory Leaks in Cocoa

I'm working on an iphone application and having some trouble with memory leaks. I've read some docs about garbage collection that it make it sound simple but I must be missing something. I've got a viewController that needs access to an array which may need to repopulated from time to time. Here is a simplified version of what I have:...

Returning pointer to a local structure

Is it safe to return the pointer to a local struct in C? I mean is doing this struct myStruct* GetStruct() { struct myStruct *str = (struct myStruct*)malloc(sizeof(struct myStruct)); //initialize struct members here return str; } safe? Thanks. ...

Pointer initialization and string manipulation in C...

Hi, I have this function which is called about 1000 times from main(). When i initialize a pointer in this function using malloc(), seg fault occurs, possibly because i did not free() it before leaving the function. Now, I tried free()ing the pointer before returning to main, but its of no use, eventually a seg fault occurs. The above...

What's the difference between char and char* in C++ ?

Studing the code in Schaum's C++ book, i saw a lot of code using char*, int* etc. Doing the exercises i also saw that in the solutions there is char* and in my code i have used char (without the star). I want to know what is the difference between a char and a pointer char - integer and a pointer integer ? Where should i use them ? Wh...

Pointer to a pointer in objective-c?

I would like to declare a pointer to a pointer in objective-c. I have an instance variable (primaryConnection) that should be updated dynamically to point to a local variable when it changes. NSURLConnection *primaryConnection; -(void) doSomething { NSURLConnection *conn; primaryConnection = conn; conn = // set by something els...

Array of char* should end at '\0' or "\0" ?

Lets say we have an array of char pointers char* array[] = { "abc", "def" }; Now what should be put in the end ? char* array[] = { "abc", "def", '\0' }; or char* array[] = { "abc", "def", "\0" }; Though, both works. We only have to put the condition to check the end accordingly like array[ index ] != '\0'; or array[ index ]...

Why are weak pointers useful?

I've been reading up on garbage collection looking for features to include in my programming language and I came across "weak pointers". From here: Weak pointers are like pointers, except that references from weak pointers do not prevent garbage collection, and weak pointers must have their validity checked before they are...

Can I increment a char* passed to a function?

I'm working on a C++ application that will build a fixed-length record from a set of database fields. I'm writing a function that will accept the output record as a char*, the string to write, and the total length of the field. The purpose of the function is to copy the string to the current position of the char pointer, and then fill th...