move exercises and lectures into subfolders
This commit is contained in:
17
Exercises/C16/alloc_example_1.c
Normal file
17
Exercises/C16/alloc_example_1.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Declare an integer array and an integer pointer
|
||||
int *pData;
|
||||
|
||||
// Using malloc
|
||||
pData = malloc ( 10000 * sizeof (int));
|
||||
|
||||
// Using calloc
|
||||
pData = calloc ( 10000 , sizeof (int));
|
||||
|
||||
return 0; // Exit
|
||||
}
|
14
Exercises/C16/alloc_example_2.c
Normal file
14
Exercises/C16/alloc_example_2.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Declare an integer array and an integer pointer
|
||||
int *pData;
|
||||
|
||||
pData = calloc ( 10000 , sizeof (float)); // No warning
|
||||
pData = (float *)calloc ( 10000 , sizeof (float)); // Warning
|
||||
|
||||
return 0; // Exit
|
||||
}
|
22
Exercises/C16/alloc_example_3.c
Normal file
22
Exercises/C16/alloc_example_3.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Declare an integer array and an integer pointer
|
||||
int *pData;
|
||||
|
||||
// Using calloc (same approach malloc)
|
||||
pData = calloc ( 10000 , sizeof (int));
|
||||
|
||||
if ( pData == NULL)
|
||||
{
|
||||
printf ("\nMemory could not be allocated - terminating");
|
||||
return -1; // Use minus one as we did not exit successfully
|
||||
}
|
||||
|
||||
// We have our memory, make use of it here!
|
||||
|
||||
return 0; // Exit successfully
|
||||
}
|
25
Exercises/C16/alloc_example_4.c
Normal file
25
Exercises/C16/alloc_example_4.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Declare an integer array and an integer pointer
|
||||
int *pData;
|
||||
|
||||
// Using calloc (same approach malloc)
|
||||
pData = calloc ( 10000 , sizeof (int));
|
||||
|
||||
if ( pData == NULL)
|
||||
{
|
||||
printf ("\nMemory could not be allocated - terminating");
|
||||
return -1; // Use minus one as we did not exit sucesfully
|
||||
}
|
||||
|
||||
// We have our memory, make use of it here!
|
||||
|
||||
// Free up the allocated memoey
|
||||
free (pData);
|
||||
|
||||
return 0; // Exit sucesfully
|
||||
}
|
18
Exercises/C16/ex2.c
Normal file
18
Exercises/C16/ex2.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare an integer array and an integer pointer
|
||||
int *pData;
|
||||
int datlen;
|
||||
|
||||
printf("Length of data to be allocated: ");
|
||||
scanf("%d", &datlen);
|
||||
|
||||
// Using malloc
|
||||
pData = malloc ( datlen * sizeof (int));
|
||||
|
||||
free(pData);
|
||||
return 0; // Exit
|
||||
}
|
23
Exercises/C16/ex3.c
Normal file
23
Exercises/C16/ex3.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare an integer array and an integer pointer
|
||||
int *pData;
|
||||
int datlen;
|
||||
|
||||
printf("Length of data to be allocated: ");
|
||||
scanf("%d", &datlen);
|
||||
|
||||
// Using malloc
|
||||
pData = malloc ( datlen * sizeof (int));
|
||||
|
||||
for (int i = 0; i < datlen; i++) {
|
||||
pData[i] = i;
|
||||
printf("%d %d\n", i, i);
|
||||
}
|
||||
|
||||
free(pData);
|
||||
return 0; // Exit
|
||||
}
|
Reference in New Issue
Block a user