type casting to / from pointers to incomplete data types 
Author Message
 type casting to / from pointers to incomplete data types

Dear C experts,

regarding to the following set of compilation units I want
to ask some questions about pointers to incomplete data
types:

/* adt.h - Declarations for the abstract data type
   struct adt_s
*/

#ifndef ADT_H_INCLUDED
#  define ADT_H_INCLUDED

#  include <stddef.h>
      /* size_t */

   extern const size_t adtSize;
      /* gives size of struct adt_s, so that we can malloc
         some dynamic memory for adt use
      */

   extern void useAdt(struct adt_s *itemPtr);
   extern void initAdt(struct adt_s *itemPtr);

#endif /* ndef ADT_H_INCLUDED */

/* end of adt.h --------------------------------------
*/

/* adt.c
*/

#  include <assert.h> /* assert */
#  include "adt.h"
#  include <stddef.h> /* NULL */

/* complete definition of abstract data type adt
*/

struct adt_s
{
   int value;

Quote:
};

const size_t adtSize = sizeof (struct adt);

void useAdt(struct adt_s *itemPtr)
{
   assert(itemPtr != NULL);
   /*
      use *itemPtr:
   */
   itemPtr->value++;

Quote:
}

void initAdt(struct adt_s *itemPtr);
{
   assert(itemPtr != NULL);
   itemPtr->value = 0;

Quote:
}

/* end of adt.c --------------------------------------
*/

/* main.c
*/

#include "adt.h" /* adtSize, initAdt(), useAdt() */
#include <stdlib.h> /* malloc(), free(), NULL, EXIT_... */

int main(void)
{
   struct adt_s *adtPtr;

   adtPtr = (struct adt_s *)malloc(adtSize); /* === [1] === */

   if (adtPtr == NULL) /* === [1] === */
      return EXIT_FAILURE;

   initAdt(adtPtr);
   useAdt(adtPtr);
   free((void *)adtPtr); /* === [2] === */

   return EXIT_SUCCESS;

Quote:
}

/* end of main.c -------------------------------------
*/

Are pointer type casts to (see === [1] ===) and from (see
=== [2] ===) pointer types to incomplete struct types
possible?

Note, that in main.c the expression sizeof (struct
adt_s) is not needed; in order to allocate dynamic memory,
we take the value of adtSize.

If the standard says, "usage of pointers to an incomplete
data type is possible, where its size is not needed", how
can machine architectures be handled, where the binary
representation of pointers (and that of null ptr.s) depends
on the different alignment restrictions that the
architecture imposes on different data types?

Regards,
Friedhelm.

Follow-ups to comp.lang.c, please.



Sat, 23 Oct 1999 03:00:00 GMT  
 type casting to / from pointers to incomplete data types

Dear C experts,

regarding to the following set of compilation units I want
to ask some questions about pointers to incomplete data
types:

/* adt.h - Declarations for the abstract data type
   struct adt_s
*/

#ifndef ADT_H_INCLUDED
#  define ADT_H_INCLUDED

#  include <stddef.h>
      /* size_t */

   extern const size_t adtSize;
      /* gives size of struct adt_s, so that we can malloc
         some dynamic memory for adt use
      */

   extern void useAdt(struct adt_s *itemPtr);
   extern void initAdt(struct adt_s *itemPtr);

#endif /* ndef ADT_H_INCLUDED */

/* end of adt.h --------------------------------------
*/

/* adt.c
*/

#  include <assert.h> /* assert */
#  include "adt.h"
#  include <stddef.h> /* NULL */

/* complete definition of abstract data type adt
*/

struct adt_s
{
   int value;

Quote:
};

const size_t adtSize = sizeof (struct adt);

void useAdt(struct adt_s *itemPtr)
{
   assert(itemPtr != NULL);
   /*
      use *itemPtr:
   */
   itemPtr->value++;

Quote:
}

void initAdt(struct adt_s *itemPtr);
{
   assert(itemPtr != NULL);
   itemPtr->value = 0;

Quote:
}

/* end of adt.c --------------------------------------
*/

/* main.c
*/

#include "adt.h" /* adtSize, initAdt(), useAdt() */
#include <stdlib.h> /* malloc(), free(), NULL, EXIT_... */

int main(void)
{
   struct adt_s *adtPtr;

   adtPtr = (struct adt_s *)malloc(adtSize); /* === [1] === */

   if (adtPtr == NULL) /* === [1] === */
      return EXIT_FAILURE;

   initAdt(adtPtr);
   useAdt(adtPtr);
   free((void *)adtPtr); /* === [2] === */

   return EXIT_SUCCESS;

Quote:
}

/* end of main.c -------------------------------------
*/

Are pointer type casts to (see === [1] ===) and from (see
=== [2] ===) pointer types to incomplete struct types
possible?

Note, that in main.c the expression sizeof (struct
adt_s) is not needed; in order to allocate dynamic memory,
we take the value of adtSize.

If the standard says, "usage of pointers to an incomplete
data type is possible, where its size is not needed", how
can machine architectures be handled, where the binary
representation of pointers (and that of null ptr.s) depends
on the different alignment restrictions that the
architecture imposes on different data types?

Regards,
Friedhelm.

Follow-ups to comp.lang.c, please.



Sat, 23 Oct 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. type casting to / from pointers to incomplete data types

2. please explain this data type pointer cast...

3. Type Casting between Base and Derived Type (Serialization of objects with Type members)

4. using incomplete structure types as opaque types

5. incomplete data type

6. Incomplete structure declarations as opaque data types

7. Dereferencing pointer to incomplete type

8. Incomplete function pointer types

9. Problem with: dereferencing pointer to incomplete type

10. "dereferencing pointer to incomplete type"

11. struct datatype scope (dereferencing pointer to incomplete type)

12. gcc: dereferencing pointer to incomplete type

 

 
Powered by phpBB® Forum Software