1

I have two cross cutting concerns in my design of my software. The memory allocator tracks memory usage in its struct(class) member variables. I also have a logger. Right now I am passing the logger and the allocator into the constructor of my objects. I can maintain a reference to them but then I would have to do that in every struct(class) I create. It would be nice if they were global but I am not sure how to do that in C. Can I construct a global memory allocator that will correctly keep track of bytes used without passing it in to my functions calls to reference?

EDIT: I am trying to use object_tracker as a global variable in my create and destroy functions. When I compile and test the files below this is the result I get: How do I use extern correctly to get the global reference to work?

gcc -I ../include object_tracker.c test_file.c test_main.c -o test
./test

/tmp/ccuT8Z1A.o: In function `Test_Create':
test_file.c:(.text+0xb): undefined reference to `object_tracker'
/tmp/ccuT8Z1A.o: In function `Test_Destroy':
test_file.c:(.text+0x4b): undefined reference to `object_tracker'
collect2: error: ld returned 1 exit status

test_file.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "object_tracker.h"
#include "test_file.h"

struct Test* Test_Create() {
        struct Test* test = (struct Test*)Object_Tracker_Obj_Alloc(object_tracker, 1, sizeof(struct Test));
        test->foobar = 10;
        return test;
}

void Test_Destroy(struct Test* test) {
        if(test != NULL) {
                Object_Tracker_Obj_Free(object_tracker, test);
        }
}

test_file.h

#ifndef _TEST_FILE_H_
#define _TEST_FILE_H_

#include "object_tracker.h"

extern struct Object_Tracker* object_tracker;

struct Test {
        int foobar;
};

struct Test* Test_Create();
void Test_Destroy(struct Test* test);

#endif

test_main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "object_tracker.h"
#include "test_file.h"

int main(int argc, char* argv[]) {

        struct Object_Tracker* object_tracker = Object_Tracker_Create();
        struct Test* test = Test_Create();
        Test_Destroy(test);
        printf("heello\n");

}
eat_a_lemon
  • 3,158
  • 11
  • 34
  • 50

3 Answers3

0

Yes you can do that in C, just define the global memory allocator at the top of the file, and everything below it will know its existence.

In order for other files to have an eye on it, use extern, as explained in How do I share a global variable between c files?

Keep in mind though that global variables should be avoided when possible.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

I removed the extern keyword and made a globals.h file with the declaration for object_tracker. I included it every "class" file and it works. I am still not sure what the extern keyword is used for.

eat_a_lemon
  • 3,158
  • 11
  • 34
  • 50
0

According to your example there is no variable ‘object_tracker’ defined globally. You have define such variable, but it is inside main function.

struct Object_Tracker* object_tracker = Object_Tracker_Create();//Inside main function ???

Since it is defined inside main function is it not visible to other functions. So move this definition to global scope. The updated test_main.c is as below.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "object_tracker.h"
#include "test_file.h"

struct Object_Tracker* object_tracker; //Global scope variable

int main(int argc, char* argv[]) {

        object_tracker = Object_Tracker_Create(); //Assign value for the variable
        struct Test* test = Test_Create();
        Test_Destroy(test);
        printf("heello\n");

}
Pra
  • 83
  • 1
  • 10
  • object_tracker is defined globally in test_file.h but the extern keyword caused a problem so I removed it and everything worked. – eat_a_lemon Jun 22 '17 at 12:33
  • The code line '[code] extern struct Object_Tracker* object_tracker;' does not defined variable. It says compiler that there is variable 'object_tracker' defined in somewhere so go ahead with compilation. Since there is no such variable linker fail. But when you remove 'extern' that line change to a variable definition. So as you mentioned the issue dissappear. – Pra Jun 23 '17 at 02:53
  • Please refer [link] (http://www.cprogramming.com/declare_vs_define.html) has good example on declaration and definition. – Pra Jun 23 '17 at 03:31