online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdio.h> #include <stdlib.h> #define DEFAULT_LIST_LENGTH 5 //an actual list element, contains type information typedef struct list_elem { enum {is_int = 1, is_float, is_double, is_char} type; union { int i_val; float f_val; double d_val; char* c_val; } value; } my_list_elem; /* list container, contains array of list elements as well as cursor and length of list */ typedef struct list { my_list_elem *element; unsigned int length; //number of elements, not bytes unsigned int cursor; } my_list; //allocate a new my_list and return pointer my_list * alloc_list() { my_list *in_list = malloc(sizeof(my_list)); in_list->element = malloc(sizeof(my_list_elem) * DEFAULT_LIST_LENGTH); in_list->length = DEFAULT_LIST_LENGTH; in_list->cursor = 0; return in_list; } //add new element to list void add_element(my_list *dest, void *in_value, const int type) { unsigned int tmp_cursor = 0; tmp_cursor = dest->cursor; //double list size if not big enough, to reduce number of realloc calls if(tmp_cursor == dest->length) { dest->element = realloc(dest->element, dest->length * sizeof(my_list_elem) * 2); dest->length *= 2; } (dest->element[tmp_cursor]).type = type; switch(type) { case is_int: (dest->element[tmp_cursor]).value.i_val = *(int *)in_value; break; case is_float: (dest->element[tmp_cursor]).value.f_val = *(float *)in_value; break; case is_double: (dest->element[tmp_cursor]).value.d_val = *(double *)in_value; break; case is_char: (dest->element[tmp_cursor]).value.c_val = (char *)in_value; break; } dest->cursor += 1; } //free list void free_list(my_list *in_list) { free(in_list->element); free(in_list); } //print list report (total list) void print_report(my_list* src) { printf("Current stats of list: \n"); printf("========================\n"); printf("Current cursor: %d\n",src->cursor); printf("Length (allocated): %d\n", src->length); printf("========================\n"); for(int i = 0; i < src->cursor ; i++) { switch(src->element[i].type) { case is_int: printf("Type: %d Value: %d\n", src->element[i].type, src->element[i].value.i_val); break; case is_float: printf("Type: %d Value: %f\n", src->element[i].type, src->element[i].value.f_val); break; case is_double: printf("Type: %d Value: %lf\n", src->element[i].type, src->element[i].value.d_val); break; case is_char: printf("Type: %d Value: %s\n", src->element[i].type, src->element[i].value.c_val); break; } } printf("\n\nEND.\n"); } int main() { my_list *new_list = alloc_list(); int my_val = 45; void *ptr_my_val = &my_val; add_element(new_list,ptr_my_val,1); char *ptr_my_string = "TEST"; add_element(new_list, ptr_my_string, 4); double my_double = 0.56843; double* ptr_my_double = &my_double; add_element(new_list, ptr_my_double, 3); print_report(new_list); free(new_list); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue