User Tools

Site Tools


opus:fall2012:asowers:cprogpart3

cprog Keyword 3

Identification of chosen keyword.

Malloc

Malloc uses blocks of memory and pointers to perform operations. In the following program we declare the struct RECORD, fill it with datatypes, and point to it. malloc lets us output the individual blocks of RECORD we defined to stdout.

#include<stdio.h>
 
        typedef struct rec
        {
                int i;
                float PI;
                char A;
        }RECORD;
 
        int main()
        {
                RECORD *point;
 
                point = (RECORD *) malloc (sizeof(RECORD));
 
                point->i = 10;
                point->PI = 3.14;
                point->A = 'a';
 
                printf("First value: %d\n", point->i);
                printf("Pi is: %f\n", point->PI);
                printf("Third value: %c\n", point->A);
 
                free(point);
 
                return 0;
        }

References

List any sites, books, or sources utilized when researching information on this topic. (Remove any filler text).

  • Reference 1
  • Reference 2
  • Reference 3

cprog Keyword 3 Phase 2

Identification of chosen keyword.

Malloc

Malloc uses blocks of memory and pointers to perform operations. In the following program we declare the struct RECORD, fill it with datatypes, and point to it. malloc lets us output the individual blocks of RECORD we defined to stdout.

#include<stdio.h>
 
        typedef struct rec
        {
                int i;
                float PI;
                char A;
        }RECORD;
 
        int main()
        {
                RECORD *point;
 
                point = (RECORD *) malloc (sizeof(RECORD));
 
                point->i = 10;
                point->PI = 3.14;
                point->A = 'a';
 
                printf("First value: %d\n", point->i);
                printf("Pi is: %f\n", point->PI);
                printf("Third value: %c\n", point->A);
 
                free(point);
 
                return 0;
        }

References

List any sites, books, or sources utilized when researching information on this topic. (Remove any filler text).

  • Reference 1
  • Reference 2
  • Reference 3

Demonstration

Demonstration of the indicated keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

#include<stdio.h>
 
        typedef struct rec
        {
                int i;
                float PI;
                char A;
        }RECORD;
 
        int main()
        {
                RECORD *point;
 
                point = (RECORD *) malloc (sizeof(RECORD));
 
                point->i = 10;
                point->PI = 3.14;
                point->A = 'a';
 
                printf("First value: %d\n", point->i);
                printf("Pi is: %f\n", point->PI);
                printf("Third value: %c\n", point->A);
 
                free(point);
 
                return 0;
        }

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

andrew ~ $ nano -c malloc.c
andrew ~ $ gcc malloc.c -o malloc
andrew ~ $ ./malloc
First value: 10
Pi is: 3.140000
Third value: a
andrew ~ $ 
opus/fall2012/asowers/cprogpart3.txt · Last modified: 2012/11/25 17:20 by asowers