Memory in C

C utilizes three types of memory: static memory, automatic memory, and dynamic memory. Static memory we know from static variables. This memory persists from the beginning to the end of the program. Automatic, or local, memory is allocated when the function it is in is called, and persists until the function ends. Dynamic memory is allocated from the heap, and persists until it is specifically released. Dynamic memory is accessed via pointers.

#include <stdio.h>
#include <stdlib.h>

//static memory
static int staticMem;

int main(void){
    
    //automatic memory
    int autoMem;
    
    
    //get dynamic memory from 
    //the heap
    int * buffer = (int*)malloc(10);
    
    //release the memory
    free(buffer);
    
    return 0;
}

A pointer holds the memory address of another variable, function, or object. Memory is allocated using one of the memory allocation functions, usually malloc(). The free() function is used to release dynamic memory. Note that the malloc() and free() functions are defined in the stdlib.h header file.

Pointers are one of the best and worst parts of C. Facility with pointers is considered a sort of sine qua non for professional programmers; although, with the growth of languages using managed code it is a less vital subject. The cryptic nature of pointer notation acts as a sort of shibboleth among programmers and in certain companies. In C, however, a deep understanding of pointers really is vital, as pointers and dynamic memory enable us to create data structures.

#include <stdio.h>

int main(void){
    
    int i,j;
    char * strings[] = {"Three pounds of flax", "Photons be free!", "Han shot first", "Access to tools"};
    
    for(i=0; i<4; i++){
        j = 0;
        while(*(*(strings+i)+j)){
            printf("[%d][%d][%c] ", i, j, *(*(strings+i)+j));
            j++;
        }
        printf("\n\n");
    }
    
    return 0;
    
}

It’s very easy to declare a pointer. Pointers are declared using the typical variable declaration format, with the addition of an asterisk placed between the data type and the variable’s identifier.

#include <stdio.h>

int main(void){
    
    double dare;
    double *pDare;
    
    
    dare = 169.254
            
    //use address of operator
    *pDare = dare;
    
    return 0;
    
}

The address of operator, &, returns its operand’s memory address.

Note that the asterisk that declares a variable as a pointer is the same symbol used for multiplication. It’s also used in dereferencing a pointer.

#include <stdio.h>

int main(void){
    
    int anum = 1024;
    //declare pointer
    int *apointer;
    
    //assign pointer a memory address
    apointer = &anum;
    
    printf("Address of anum = %p Value = %d\n", &anum, anum);
    printf("Address of apoiter = %p Value = %d\n", &apointer, *apointer);
    
    return 0;
    
}

Note that the %p specifier is used to display pointer values.

Want to learn more? Get my book http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

I also have a book on the Linux command line, only $.99 http://www.amazon.com/Big-Als-Linux-Fedora-CLI-ebook/dp/B00GI25V30/

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s