Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Where is type "object_t" defined, in C?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading