Created
July 29, 2018 14:17
-
-
Save kassane/662769f1ab0b9685bf44982cc31f935b to your computer and use it in GitHub Desktop.
Simple float Vector in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
MEMORY MANAGER IN C LANGUAGE | |
@author: Kassane | |
Date: 07/29/2018 | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Vector | |
{ | |
int size; | |
float *value; | |
}; | |
typedef struct Vector Vector; | |
Vector alloc(Vector v, int s) | |
{ | |
v.value = (float*)malloc(sizeof(float[s])); | |
v.size = s; | |
return v; | |
} | |
Vector delete(Vector v) | |
{ | |
free(v.value); | |
v.value = NULL; | |
return v; | |
} | |
int main() | |
{ | |
Vector v = alloc(v,5); //new[] | |
for(int i=0; i < v.size; i++) | |
{ | |
v.value[i] = i+5.6*4.7; | |
} | |
printf("Vector in C lang with size: %d\n", v.size); | |
for(int i=0; i < v.size; i++) | |
printf("values[%d]: %.3f\n", i, v.value[i]); | |
v = delete(v); //delete[] | |
return 0; | |
} | |
/* OUTPUT | |
============== Valgrind Analysis ================ | |
Compiling source code to object code: | |
==27707== Memcheck, a memory error detector | |
==27707== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. | |
==27707== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info | |
==27707== Command: ./vector | |
==27707== | |
Vector in C lang with size: 5 | |
values[0]: 26.320 | |
values[1]: 27.320 | |
values[2]: 28.320 | |
values[3]: 29.320 | |
values[4]: 30.320 | |
==27707== | |
==27707== HEAP SUMMARY: | |
==27707== in use at exit: 0 bytes in 0 blocks | |
==27707== total heap usage: 2 allocs, 2 frees, 1,044 bytes allocated | |
==27707== | |
==27707== All heap blocks were freed -- no leaks are possible | |
==27707== | |
==27707== For counts of detected and suppressed errors, rerun with: -v | |
==27707== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) | |
Done! | |
===================================================== | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment