I’m a .NET programmer by day, and I’m learning C using Effective C by Robert Seacord.
I’m on Chapter 5 – Control Flow, where it describes "goto" and I’m trying to compile "Listing 5-11: Using a goto chain to release resources". But type "object_t" is not defined.
I assumed I could just find the definition in one of the standard headers (stdint.h etc.) but I can’t find it and a Stack Overflow and Google search aren’t helping.
I’ve searched this and previous chapters to no avail. Is this a user-defined type that the author doesn’t explain, at this point? Or is my Googling that bad?
I’ve tried lots of header files I can think of:
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stddef.h>
Here’s the function definition:
int do_something(void) {
FILE *file1, *file2;
object_t *obj;
int ret_val = 0; // Initially assume a successful return value
file1 = fopen("a_file", "w");
if (file1 == NULL) {
ret_val = -1;
goto FAIL_FILE1;
}
file2 = fopen("another_file", "w");
if (file2 == NULL) {
ret_val = -1;
goto FAIL_FILE2;
}
obj = malloc(sizeof(object_t));
if (obj == NULL) {
ret_val = -1;
goto FAIL_OBJ;
}
// Operate on allocated resources
// Clean up everything
free(obj);
FAIL_OBJ: // Otherwise, close only the resources we opened
fclose(file2);
FAIL_FILE2:
fclose(file1);
FAIL_FILE1:
return ret_val;
}
Here’s the error: error: unknown type name ‘object_t’
>Solution :
There’s no standard type called object_t. It’s a user defined type.
And given how this type is being used (i.e. just to demonstrate allocating memory), you can define it to be anything you want.