+1 (315) 557-6473 

Things every programmer should know about C pointers

Understanding C pointers is important if you hope to build a career in C programming. In this post, our C experts share a few things that you should know about C pointers to help you ace this area.

6 Things to Know About C Pointers!

The null pointer. You can use NULL or 0 to represent a null pointer, they are identical, and it indicates a pointer that is not pointing to anything.
Arrays and pointers. Arrays and pointers are mostly interchangeable, if you are not modifying the value.
Pointer arithmetic. You can add or subtract an integer from a pointer, or you can subtract 2 pointers from each other. If you need help understanding pointer arithmetic, we can provide C programming assignment help.
Pointer indexing. If you have a pointer ptr, you can use ptr[2] or 2[ptr], but please don’t.
Non zero based arrays. If you want an array where you access -8 to +8 for example. You create a pointer and reserve enough memory for it. Then you subtract the base (the value instead of 0), then you access it as an array. You can use this technique, so that you don’t need to keep using a[index – offset] each time. For more information, just ask us for C programming assignment help.
Write your own versions of malloc and free, and make them reserve extra memory at the start and end, and place a fixed value in the extra space and then during the free check to see if the values have been changed, which indicates the memory that was corrupted. Using macros you can even store the __FILE__ and __LINE__ where the memory was allocated in order to track the where the memory that was corrupted was allocated. You can use the following code for testing in your own applications, it can easily be extended so that it has a linked list, so during any malloc / free it would check all the blocks for corruption. If you want an extended debug that does all that and more, then we can offer it, just contact us for C programming assignment help.

//Debug memory structure, to check for corruption

typedef struct _DbgMem {

int line;

char *file;

int size;

int check;

} DbgMem;

//If Debug mode then use debug malloc and free

#ifdef DEBUG

#define dbgMalloc(size) DbgMalloc(__FILE__, __LINE__, size)

#define dbgFree(ptr) DbgFree((DbgMem*)ptr)

#else

#define dbgMalloc malloc

#define dbgFree free

#endif

//The check value

#define CHECK 0xDEADBEEF

//Allocate memory and set header and footer up

void * DbgMalloc(char *file, int line, int size) {

int end = size + sizeof(int) + sizeof(check);

DbgMem *mem = malloc(end);          //Allocate the extra memory

mem.line = line;

mem.file = file;

mem.size = size;                                  //The initial memory size

mem.check = CHECK;                       //To ensure under access will be caught

mem[end / sizeof(int)] = CHECK;     //To ensure end of array is not accessed

return mem + 1;                      //return the data space

}

//Free the memory and check for corruption first

void * DbgFree(DbgMem *ptr) {

int end = ptr[-1].size + sizeof(int) + sizeof(check);

int *mem = (int*)(ptr + end / sizeof(int));

if (!ptr) return;             //You can free NULL and that is always valid

//See if memory is corrupted

if (ptr[-1].check != CHECK || *mem != CHECK) {

printf(“Memory allocated at %s(%d) corrupted.\n”, ptr[-1].file, ptr[-1].line);

}

free(ptr-1);

}

We at Programming Assignment Helper know the ins and outs of C, so if you are looking for C programming assignment help, we can provide it.