I am currently a Java programmer doing some work in C for a class, and I've been struggling a lot with the differences between the two. Currently, I am trying to add functionality with compiler hooks to count the time that is spent within each function of the program that is being executed. My struggle for this is that my solution is to use a stack, and I'm having trouble instantiating (if that's even the right word to use?) the stack since I can't add a main method or something of that sort to create it at the beginning. Here is my code now:
#include <stdio.h>
#include <time.h>
#include "mystack.h"
static stack_t* entry_times=create_stack(500);
static unsigned int count_entered=0;
static unsigned int count_completed=0;
__attribute__((no_instrument_function))
void __cyg_profile_func_enter(void *this_fn, void *call_site){
count_entered++;
time_t start_time;
time(&start_time);
stack_enqueue(entry_times, start_time);
printf("Total Functions Entered: %d\n", count_entered);
}
__attribute__((no_instrument_function))
void __cyg_profile_func_exit(void *this_fn, void *call_site){
time_t end_time;
time(&end_time);
time_t start_time = stack_dequeue(entry_times);
double difference = difftime(end_time, start_time);
count_completed++;
printf("Time in Function: %d\n", difference);
}
Right now, when I try to compile this code, I am getting a "initializer element is not constant" error point to the line where I create my entry_times stack. How can I solve this error, or refactor my code to prevent this issue?
I'm sorry if this is a duplicate topic, I have done a fair amount of searching, and I suspect I simply do not know what to actually search for to find the information that I am looking for.