CodemancerDotCoDotUK

Dangling Pointers & Memory Leaks

Dangling pointer

Pointer points to a memory location that no longer exists.

Memory Leaks

Memory allocated by a program is not freed.

The program is particularly acute if memory allocated in heap can no longer be accessed.

void foo(){
    int * p = malloc(8);
    p = NULL;
    /* memory previously pointed by p can never
     * be freed. */
}
int * f2(int null){
    int mem2[num];
    return mem2;
} 
// DANGLING POINTER
// never return the address of a local variable.
Published on October 16, 2015

Dynamic Memory Allocation

malloc

void * malloc (size_t s) 
//size_t defined in stdlib.h
int * arr = malloc(5 * sizeof(int));
//allocate memory for an int array of size 5

calloc

calloc() works like malloc, but initializes the memory to zero if possible. The prototype is:

void *calloc(size_t num, size_t size);
// equivalent to malloc()
void *malloc(size_t num * size)

Num is the number of elements, and size the byte size of each element.

realloc

void* realloc (void * p, size_t s);

Extend or shrink the block pointed by p to s bytes

Safe instructions to use realloc
int *tmp;
if ((tmp = realloc(ip, sizeof(int) * (INITIAL_ARRAY_SIZE + 5))) == NULL) {
    fprintf(stderr, "ERROR: realloc failed");
}
ip = tmp;

If realloc fails to extend memory pointed by ptr, ptr will point to NULL, and its previous allocated memory will never be accessed (memory leak).

bzero

bzero(3) fills the first n bytes of the pointer to zero. Prototype:

void bzero(void *s, size_t n);

memset

To set the value to some other value (or just as a general alternative to bzero).

void *memset(void *s, int c, size_t n);

Specify c as the value to fill for n bytes of pointer s.

free

void free(void * ptr)

Free the heap block pointed by p

int *p = malloc(8);
// allocated 8 bytes to p
free(p);
free(p); //program crashes [runtime error]

After freeing a pointer, should point it to NULL.

Published on October 16, 2015

Protected: Download Mirror

This content is password protected. To view it please enter your password below:

Published on July 11, 2015

A Cultural Project

Here I made a cultural project about 1992 Los Angeles riots.

Capstone Project

Purpose

This project was designed to recall the history in the perspectives from different ethnic groups, with a mainstream media timeline for comparison. The map can you walk you through the event in a flat LA space layout.

To see LA Times timeline for comparison:  Click here

Map Comparison

Map of Los Angeles by Average Household Income

Map of Los Angeles by Average Household Income

Racial Dot Map In LA Highlights Segregation By Neighborhood

Racial Dot Map In LA Highlights Segregation By Neighborhood

The two maps above indicating racial and economic dispersion of LA can be compared with the map provided by this site. The violence of the riots in 1992 happened most in areas of the non-white, low-income households.

References

Images:

Videos:

Maps:

The maps uses Google Maps data with customized markers. Coordinates are parsed manually.

Texts:

Most texts are hand-written. There are textual explanations from the following sources:

Published on May 20, 2015

You Have An Accent!

Yes, I have an accent. Everybody has. If you are told you have an accent, that means they way what you speak is different to most people in this area do.

In a university, talking to people from everywhere, I find it easy to identify an accent and thus have an idea of where he or she comes from. Nevertheless, most people don’t, because they are not careful enough. If you can distinguish Indian English or Japanese English, it is not hard to tell the difference among America’s mid-west, California, and the Eastern coast. It is also not a problem to pinpoint which part of UK a person comes from, Scotland, Yorkshire, London, or London’s east-end. The fact is, listen. 

Accents shift frequently. I can speak many English accents in different contexts. The variation is not exclusive to English. People in China typically have two accents, the standard Mandarin and their own dialect.

Language and its accents are intersting objects as they carry information about the speakers’ whereabouts and enrich an distinct local culture. As people migrate, accents are also an intangible mark they take with them.  

Published on May 6, 2015

Welcome

Welcome! This place is for some valuable codes.

Published on May 4, 2015