Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions DynamicArrays/IntMalloc1D.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*In this program we will be introducing dynamic memory allocation to create*/
/*a 1-dimensional array of integers*/

int main()
{
/*We need to create a pointer to an integer*/
int i,n,*array;
srand(time(NULL));

fprintf(stdout,"Size of array: ");
scanf("%d",&n);

array = (int *)malloc(n * (sizeof(int)));
/*We just allocated n * sizeof(int) amount of memory to our pointer *array*/

if (array == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
/*Always check if malloc was successful.*/
/*Now it works like a regural array.*/
for(i=0;i<n;i++)
{
array[i] = rand()%11;
fprintf(stdout,"Number %d is: %d\n",(i+1),array[i]);
}
/*After finishing with the program we have to free the allocated memory*/
free(array);

return 0;
}
54 changes: 54 additions & 0 deletions DynamicArrays/IntMalloc2D.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*This program is a continuation of intmalloc1D.c*/
/*Now we will be create a 2D array by creating an array of pointers*/
/*that point to arrays of integers.*/

int main()
{
/*The beginning is almost the same*/
int i,j,x,y,**array;
srand(time(NULL));

fprintf(stdout,"Size of array(x,y): ");
scanf("%d%d",&x,&y);

/*Now we will create an array size y of (int *)*/
array = (int **)malloc(y * sizeof(int *));
if (array == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
/*Each pointer of the array will be pointing to a 1D array*/
for(i=0;i<y;i++)
{
array[i] = (int *)malloc(x * sizeof(int));
/*We will be cheking each pointer*/
if (array[i] == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
}

/*Now it works like a regural 2D array.*/
for(i=0;i<y;i++)
{
for(j=0;j<x;j++)
{
array[i][j] = rand()%11;
fprintf(stdout,"Number %d of %d column is: %d\n",(j+1),(i+1),array[i][j]);
}
}
/*We will free each allocated array first and then the array of pointers*/
for(i=0;i<y;i++)
{
free(array[i]);
}
free(array);

return 0;
}
7 changes: 7 additions & 0 deletions DynamicArrays/READMEMALLOC.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This folder contains 6 different programs, that use and explain dynamic memory
allocation using functions like malloc() and realloc().

The correct way to go about studying dynamic memory allocation here is:

IntMalloc1D.c -> IntMalloc2D.c -> StructMalloc.c or StrMalloc.c ->
-> StrMallocArr.c -> Realloc.c
99 changes: 99 additions & 0 deletions DynamicArrays/Realloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0

/*There will be time when we the memory that we have allocated won't be enough.*/
/*That means that we need to allocate more. Thankfully for us, C has got us covered*/

int main()
{
int counter = 0, capacity = 2;
int **array,**forrealloc;
int i,j,n=3,flag=TRUE,res;
char c;

while (flag == TRUE)
{
/*Because realloc() is a slow function, we will use a capacity integer*/
/*and we will allocate and reallocate memory based on it.*/
if (counter == 0)
{
array = (int **)malloc(capacity * sizeof(int *));
if(array == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
for(i=0;i<capacity;i++)
{
array[i] = (int *)malloc(n * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
}
}
else if (counter >= capacity)
{
capacity += 2;
forrealloc = (int **)realloc(array,(capacity * sizeof(int *)));
if(forrealloc == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-3);
}
array = forrealloc;
/*We only need to allocate memory to the newly created pointers.*/
for(i=counter;i<capacity;i++)
{
array[i] = (int *)malloc(n * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
}
}
fprintf(stdout,"1. Add %d numbers or 2. Leave: ",n);
scanf("%d",&res);
while((c = getchar()) != '\n');

switch (res)
{
case 1:
{
for(i=0;i<n;i++)
{
fprintf(stdout,"Give %d number: ",(i+1));
scanf("%d",&array[counter][i]);
while((c = getchar()) != '\n');
}
counter++;
break;
}
case 2:
{
flag = FALSE;
break;
}
}
}
for(i=0;i<counter;i++)
{
for(j=0;j<n;j++)
{
fprintf(stdout,"Number %d of column %d: %d\n",(j+1),(i+1),array[i][j]);
}
}
/*Freeing the memory is the exact same as always.*/
for(i=0;i<capacity;i++)
{
free(array[i]);
}
free(array);

return 0;
}
33 changes: 33 additions & 0 deletions DynamicArrays/StrMalloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*In this program we will create a dynamic string. Strings work different in C*/
/*They are an array of characters. So malloc works here too.*/

int main()
{
int i,size;
char *str,c;

fprintf(stdout,"Give size of word: ");
scanf("%d",&size);
while((c = getchar()) != '\n');

/*When we are allocating memory for a string, we always need to add*/
/*one more space for the '\0'*/
str = (char *)malloc((size * sizeof(char)) + 1);
if(str == NULL)
{
fprintf(stderr,"Couldn't allocate memory.\n");
exit(-1);
}

fprintf(stdout,"Word: ");
fgets(str,size+1,stdin);
str[strcspn(str,"\n")] = '\0';

fprintf(stdout,"The word is: %s\n",str);
free(str);
return 0;
}
50 changes: 50 additions & 0 deletions DynamicArrays/StrMallocArr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*In this program we will create a dynamic array of strings. Because as we*/
/*said before strings are arrays of characters, so we need to malloc twice.*/

int main()
{
int amount,size,i;
char **str,c;

fprintf(stdout,"Choose number of words and size of each word: ");
scanf("%d%d",&amount,&size);
while((c = getchar()) != '\n');

str = (char **)malloc(amount * (sizeof(char *)));
if(str == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
for(i=0;i<amount;i++)
{
str[i] = (char *)malloc((size * sizeof(char)) + 1);
if(str[i] == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}
}

/*The important thing here is that you only need one for loop.*/
for(i=0;i<amount;i++)
{
fprintf(stdout,"Word %d: ",(i+1));
fgets(str[i],size+1,stdin);
str[i][strcspn(str[i],"\n")] = '\0';
}

/*We have to free everything as we did in previous programs*/
for(i=0;i<amount;i++)
{
fprintf(stdout,"The word %d is: %s\n",(i+1),str[i]);
free(str[i]);
}

free(str);
return 0;
}
54 changes: 54 additions & 0 deletions DynamicArrays/StructMalloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*We will create a dynamic array of structs*/

typedef struct
{
char firstname[20];
char lastname[20];
int age;
}People;

int main()
{
int i,totalp;
char c;

/*(People *person) creates a pointer to the struct we have created*/
People *person;

fprintf(stdout,"Amount of people: ");
scanf("%d",&totalp);
while((c = getchar()) != '\n');
person = (People *)malloc(totalp * sizeof(People));
/*Everything after this works exactly like a regural dynamic array.*/
if (person == NULL)
{
fprintf(stderr,"Couldn't allocate memory\n");
exit(-1);
}

for(i=0;i<totalp;i++)
{
fprintf(stdout,"First Name: ");
fgets(person[i].firstname,20,stdin);
person[i].firstname[strcspn(person[i].firstname,"\n")] = '\0';
fprintf(stdout,"Last Name: ");
fgets(person[i].lastname,20,stdin);
person[i].lastname[strcspn(person[i].lastname,"\n")] = '\0';
fprintf(stdout,"Age: ");
scanf("%d",&person[i].age);
while((c = getchar()) != '\n');
}

for(i=0;i<totalp;i++)
{
fprintf(stdout,"Person %d | First Name: %s | Last Name: %s | Age: %d\n",(i+1),person[i].firstname,person[i].lastname,person[i].age);
}

free(person);

return 0;
}