Skip to content

Commit c519a7a

Browse files
committed
Fixed bug: Discarding memory when realloc
1 parent a89cdf9 commit c519a7a

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

main.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Command cmds[] = {
2626
{NULL, 0, 0, NULL, 0}
2727
};
2828

29-
#define ALLOC_MEM (size_t)512
29+
#define ALLOC_MEM (size_t)5
3030
#define REALLOC_MEM (size_t)1024
3131

3232
int main(int argc, char const *argv[]) {
@@ -35,12 +35,11 @@ int main(int argc, char const *argv[]) {
3535
size_t program_len;
3636

3737
size_t memory_size = ALLOC_MEM;
38-
unsigned char* memory = (char*)calloc(sizeof(unsigned char), memory_size); ERR_ALLOC(memory)
38+
unsigned char* memory = (char*) calloc(sizeof(unsigned char), memory_size); ERR_ALLOC(memory)
3939
size_t mem_ptr = 0;
4040
unsigned char current_instr;
4141

4242
int err = handleArgs(argc, argv, cmds, &filename);
43-
4443
if (err == 1) { ERR("Failed to Initialize!\n") }
4544
if (err == 2) { return 0; }
4645
else if (filename == NULL) {
@@ -54,11 +53,11 @@ int main(int argc, char const *argv[]) {
5453

5554

5655
for (int i = 0; i < program_len; i++) {
57-
// Dynamicly realloc if the Memory gets too small
56+
// Dynamicly alloc if the Memory gets too small
5857
if (mem_ptr >= memory_size) {
58+
unsigned char* new_memory = (unsigned char*) calloc(sizeof(unsigned char), (memory_size) + REALLOC_MEM); ERR_ALLOC(new_memory)
59+
memcpy(new_memory, memory, memory_size);
5960
memory_size += REALLOC_MEM;
60-
unsigned char* new_memory = (unsigned char*)calloc(sizeof(unsigned char), memory_size); ERR_ALLOC(new_memory)
61-
memcpy(new_memory, memory, mem_ptr);
6261
free(memory);
6362
memory = new_memory;
6463
}

0 commit comments

Comments
 (0)