Reputation: 34
While trying to pass an argument to a function; the function receives the wrong value thus creating a segmentation fault while trying to use that value
argc and argv in the main function are corrupted before the program even starts
main.c file:
#include <stdio.h>
#include <stdlib.h>
#include "util/BasicTypes.h"
#include "util/IncludeOpengl.h"
#include "util/Vec2.h"
#include "util/Vec3.h"
#include "util/Vec4.h"
#include "util/Vector.h"
#if !(defined(__linux__) || defined(_WIN64))
STATIC_ASSERT(false, unsupported_system)
#endif
i32 main(i32 argc, ch8** argv) {
//u32 a[] = {1, 2, 3, 4};
//u32* b = a;
u32 i;
Vector v;
init_Vector(&v, sizeof(u32));
for (i = 0; i < 4; i++) {
printf("%i\n", i);
Vector_append(&v, &i);
}
for (i = 0; i < Vector_getSize(&v); i++) {
printf("%i\n", Vector_get(&v, i));
}
destruct_Vector(&v);
return 0;
}
vector.c file where i define the vector functions:
#ifndef VECTOR_C
#define VECTOR_C
#include "../util/Vector.h"
#include "../util/BasicTypes.h"
#include "../util/Assert.h"
void init_Vector(Vector* self, u32 elementSize) {
self->elementSize = elementSize;
self->elements = NULL;
self->elementCount = 0;
}
void destruct_Vector(Vector* self) {
if (self->elements) {
heapFree(self->elements);
}
}
void Vector_append(Vector* self, void* element) {
if (self->elements) {
self->elements = heapRealloc(self->elements, self->elementCount + 1);
memcpy(((byte*)self->elements) + (self->elementCount) * self->elementSize, element, self->elementSize);
self->elementCount += 1;
return;
}
else {
self->elements = heapAlloc((usize)self->elementSize);
memcpy(self->elements, element, self->elementSize);
self->elementCount += 1;
return;
}
}
void Vector_remove(Vector* self, u32 id) {
if (id < self->elementCount) {
void* temp = self->elements;
self->elements = heapAlloc((self->elementCount - 1) * self->elementSize);
memcpy(self->elements, temp, id * self->elementSize);
memcpy(((byte*)self->elements) + id * self->elementSize, ((byte*)temp) + (id + 1) * self->elementSize, (self->elementCount - id - 1) * self->elementSize);
self->elementCount -= 1;
}
else {
ASSERT(false, "(Vector_remove) id is not in the list");
}
}
void Vector_insert(Vector* self, void* element, u32 id) {
if (!self->elements && !id) {
self->elements = heapAlloc(self->elementSize);
self->elementCount = 1;
}
else {
if (id < self->elementCount + 1) {
void* temp = self->elements;
self->elements = heapAlloc((self->elementCount - 1) * self->elementSize);
memcpy(self->elements, temp, id * self->elementSize);
memcpy(((byte*)self->elements) + (id + 1) * self->elementSize, ((byte*)temp) + (id) * self->elementSize, (self->elementCount - id - 1) * self->elementSize);
memcpy(((byte*)self->elements), ((byte*)temp), self->elementSize);
self->elementCount += 1;
}
else {
ASSERT(false, "(Vector_insert) id is too far");
}
}
}
void* Vector_get(Vector* self, u32 id) {
return ((byte*)self->elements) + id * self->elementSize;
}
u32 Vector_getSize(Vector* self) {
return self->elementCount;
}
#endif // VECTOR_C
vector.h file the header file for vector.c:
#ifndef VECTOR_H
#define VECTOR_H
#include "../util/BasicTypes.h"
typedef struct Vector {
void* elements;
u32 elementSize;
u32 elementCount;
} Vector;
void init_Vector(Vector* self, u32 elementSize);
void destruct_Vector(Vector* self);
void Vector_append(Vector* self, void* element);
void Vector_remove(Vector* self, u32 id);
void Vector_insert(Vector* self, void* element, u32 id);
void* Vector_get(Vector* self, u32 id);
u32 Vector_getSize(Vector* self);
#endif // VECTOR_H
basictypes.h file where i define some aliases:
#ifndef BASIC_TYPES_H
#define BASIC_TYPES_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef i8 ch8;
typedef i16 ch16;
typedef i32 ch32;
typedef i64 ch64;
typedef u8 uch8;
typedef u16 uch16;
typedef u32 uch32;
typedef u64 uch64;
typedef float f32;
typedef double f64;
typedef long double f128;
typedef u8 byte;
typedef size_t usize;
#endif // BASIC_TYPES_H
Upvotes: 0
Views: 114
Reputation: 213306
typedef i8 ch8;
is wrong, since int8_t
might likely expand to signed char
, which in turn is not directly compatible with char
unless you explicitly covert between the two types.
The format of main is not for the programmer to decide, but the compiler. gcc under strict compliance settings (-pedantic-errors
) tells you this much:
error: second argument of 'main' should be 'char **' [-Wmain]
Besides, nobody wants to read code based on your own special type system. We have standards for a reason, so use standard C instead:
#include <stdint.h>
....
int main (int argc, char* argv[])
{
uint32_t i;
Upvotes: 1