Area Survey: Security in File Systems

C vs C++
Sarah Diesburg
Florida State University
1
Hello World

C++
cout << “Hello World!” << endl;

C
printf(“Hello World!\n”);
2
Dynamic Array/Memory
Allocation

C++
int *x_array = new int[10];
delete[] x_array;

C
int *x_array = malloc(sizeof(int) *10);
free(x_array);
3
Structs

After defining a struct, how do you declare a new
instance?
struct a_struct
{
int x;
};

C++
a_struct struct_instance;

C
struct a_struct struct_instance;
4
Typedefs

To get around typing so much, use a typedef
typedef struct a_struct
{
int x;
}a_struct_type;

Now declare it
a_struct_type struct_instance;
5
Declaring Variables
C++
for(int i=0; i<condition; i++){
}

C
int i;
for(i=0; i<condition; i++) {
}

6
Boolean


No boolean type in C
You can simulate through enum
typedef enum {FALSE, TRUE} bool;

Why is false before true?
typedef enum {FALSE=0, TRUE=1} bool;
7
Boolean Example
#include <stdio.h>
#include <stdlib.h>
typedef enum {FALSE, TRUE} bool;
main()
{
bool flag1=TRUE;
bool flag2=FALSE;
if(flag1 > flag2)
printf("The truth wins again!\n");
return 0;
}
8