Problem with sizeof of a struct void pointer 
Author Message
 Problem with sizeof of a struct void pointer

Hi,

I have a problem with getting the size of a struct. I have a struct in
main and a give it to a function by a void pointer. The problem is that
I don't get the size of this struct. I have included the source of the
main and the function.

Tnx for your time,

Hans

main.c
-----
#include "hashing.h"

void main(void)
{
  NAT_struct *element;

  char *filename;

  element = malloc(sizeof(NAT_struct));

  filename = malloc(sizeof("test.dat"));
  filename = "test.dat";
  if (create_empty_file(filename, element, 200)==-1)
    printf("FOUTJE");

Quote:
}

function
------
#include "hashing.h"

int create_empty_file (char *filename, const void *record, int size)
{
  FILE *file;
  header_struct header;

  // check of de file al bestaat.
  if ((file=fopen(filename, "rb"))!=NULL)
    return -1;

  // creer de file
  if ((file=fopen(filename, "wb"))==NULL)
    return -2;

  header.aantalRecords=size;
  header.bezetting=0;

  fwrite(&header, sizeof(header_struct), 1, file);
  fwrite(record, sizeof(*record)), size, file);
// when we replace *record with NAT_struct this works

  fclose(file);

  return size;

Quote:
}



Sat, 24 Nov 2001 03:00:00 GMT  
 Problem with sizeof of a struct void pointer


Quote:
> Hi,

> I have a problem with getting the size of a struct. I have a struct in
> main and a give it to a function by a void pointer. The problem is
that
> I don't get the size of this struct. I have included the source of the
> main and the function.
> main.c
> -----
> #include "hashing.h"

> void main(void)

  /* main returns int in ANSI C */

Quote:
> {
>   NAT_struct *element;

>   char *filename;

>   element = malloc(sizeof(NAT_struct));

>   filename = malloc(sizeof("test.dat"));
>   filename = "test.dat";

     /* Memory leak - access to allocated memory is lost */
     /* filename is now pointing to a string literal */

Quote:
>   if (create_empty_file(filename, element, 200)==-1)
>     printf("FOUTJE");
> }

> function
> ------
> #include "hashing.h"

> int create_empty_file (char *filename, const void *record, int size)
> {
>   FILE *file;
>   header_struct header;

>   // check of de file al bestaat.
>   if ((file=fopen(filename, "rb"))!=NULL)
>     return -1;

>   // creer de file
>   if ((file=fopen(filename, "wb"))==NULL)
>     return -2;

>   header.aantalRecords=size;
>   header.bezetting=0;

>   fwrite(&header, sizeof(header_struct), 1, file);
>   fwrite(record, sizeof(*record)), size, file);
> // when we replace *record with NAT_struct this works

   But of course. void * is a generic pointer to object type.
It is a pointer to *something* - and you can't extract information
about the size (or type) of this something from the pointer.
 Consider: char *cp;
           int  *ip;
           void *vp;
  sizeof (*cp) - size of an object of type char;
  sizeof (*ip) - size of an object of type int;
  sizeof (*vp) - has to be size of an object of type void?
                  And what this is presumed to be?

Quote:

>   fclose(file);

>   return size;
> }

  If you wish to keep your function generic, you have to pass the
size of your object into it as a parameter.
  Incidentally, writing contents of raw uninitialised memory into file
may be not such a bright idea.
--
        Regards,
                Alex Krol
Disclaimer: I'm not speaking for Scitex Corporation Ltd

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sat, 24 Nov 2001 03:00:00 GMT  
 Problem with sizeof of a struct void pointer
Hi,

As far as I know: What you are trying to do is not possible, as the size of
objects are calculated at COMPILE time, NOT during RUN time.

Cheers,
Johan


Quote:
>Hi,

>I have a problem with getting the size of a struct. I have a struct in
>main and a give it to a function by a void pointer. The problem is that
>I don't get the size of this struct. I have included the source of the
>main and the function.

>Tnx for your time,

>Hans

>main.c
>-----
>#include "hashing.h"

>void main(void)
>{
>  NAT_struct *element;

>  char *filename;

>  element = malloc(sizeof(NAT_struct));

>  filename = malloc(sizeof("test.dat"));
>  filename = "test.dat";
>  if (create_empty_file(filename, element, 200)==-1)
>    printf("FOUTJE");
>}

>function
>------
>#include "hashing.h"

>int create_empty_file (char *filename, const void *record, int size)
>{
>  FILE *file;
>  header_struct header;

>  // check of de file al bestaat.
>  if ((file=fopen(filename, "rb"))!=NULL)
>    return -1;

>  // creer de file
>  if ((file=fopen(filename, "wb"))==NULL)
>    return -2;

>  header.aantalRecords=size;
>  header.bezetting=0;

>  fwrite(&header, sizeof(header_struct), 1, file);
>  fwrite(record, sizeof(*record)), size, file);
>// when we replace *record with NAT_struct this works

>  fclose(file);

>  return size;
>}



Sat, 24 Nov 2001 03:00:00 GMT  
 Problem with sizeof of a struct void pointer
v982650 schrieb:

Quote:

> I have a problem with getting the size of a struct. I have a struct in
> main and a give it to a function by a void pointer. The problem is that
> I don't get the size of this struct. I have included the source of the
> main and the function.

Hi v982650,

You problem is that a "void*" does not carry any information about what it
actually points to. If that information is important to you, you must
provide it separately, in the way you need it. For instance if you only
need a size, pass a second parameter that indicates the size. Or you might
pass a special ID code to the function that identifies the type of the
pointer. That way you could calculate the size in the function and you
could even cast the "void*" pointer back to the correct original type.

Quote:
> void main(void)

Beware: the only correct return type for function "main" is "int".

Quote:
> {
>   NAT_struct *element;

>   char *filename;

>   element = malloc(sizeof(NAT_struct));

>   filename = malloc(sizeof("test.dat"));
>   filename = "test.dat";

This is probably a conceptual error. It works, but it does not do what you
think it should do ;-) The assignment operator "=" does not copy character
arrays. If you want to copy some text from one array to another you should
use "strcpy()" for normal C strings.

What you do instead is overwrite (and therefore loose) the pointer
returned by "malloc()" with the address of a string constant.

Quote:
>   if (create_empty_file(filename, element, 200)==-1)

If you want to pass the size of the structure to the function simply add
it as an additional parameter to the function call:
   create_empty_file( filename, element, 200, sizeof(NAT_struct) )

Quote:
> int create_empty_file (char *filename, const void *record, int size)

The new function prototype would look like this:
   int create_empty_file ( char *filename, const void *record,
                           int amount, int recordSize )
(note that I replaced your "size" with "amount")

Quote:
> {
>   FILE *file;
>   header_struct header;

>   // check of de file al bestaat.
>   if ((file=fopen(filename, "rb"))!=NULL)
>     return -1;

If the file has been opened successfully, you should close it before
returning.

Quote:
>   fwrite(record, sizeof(*record)), size, file);

This line would become:
   fwrite( record, recordSize, amount, file );

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 24 Nov 2001 03:00:00 GMT  
 Problem with sizeof of a struct void pointer
Quote:

> Hi,

> I have a problem with getting the size of a struct. I have a struct in
> main and a give it to a function by a void pointer. The problem is that
> I don't get the size of this struct. I have included the source of the
> main and the function.

> Tnx for your time,

> Hans

> main.c
> -----
> #include "hashing.h"

> void main(void)
> {
>   NAT_struct *element;

>   char *filename;

>   element = malloc(sizeof(NAT_struct));

>   filename = malloc(sizeof("test.dat"));
>   filename = "test.dat";
>   if (create_empty_file(filename, element, 200)==-1)
>     printf("FOUTJE");
> }

> function
> ------
> #include "hashing.h"

> int create_empty_file (char *filename, const void *record, int size)
> {
>   FILE *file;
>   header_struct header;

>   // check of de file al bestaat.
>   if ((file=fopen(filename, "rb"))!=NULL)
>     return -1;

>   // creer de file
>   if ((file=fopen(filename, "wb"))==NULL)
>     return -2;

>   header.aantalRecords=size;
>   header.bezetting=0;

>   fwrite(&header, sizeof(header_struct), 1, file);
>   fwrite(record, sizeof(*record)), size, file);
> // when we replace *record with NAT_struct this works

>   fclose(file);

>   return size;
> }

You can't use sizeof with a void pointer and get a meaningful result.
Sizeof
needs the type information to calculate the size.  You could update your
function to pass in the size of the record.
ex.
int create_empty_file (char *filename, const void *record, int
record_size, int size)
{ ... }
then when you call the function add the sizeof
ex.
if (create_empty_file(filename, element, sizeof(NAT_struct), 200)==-1
and then you'll need to change the fwrite to
fwrite(record, record_size, size, file);


Sat, 24 Nov 2001 03:00:00 GMT  
 Problem with sizeof of a struct void pointer
J. Wegener NOSPAM schrieb:

Quote:

> As far as I know: What you are trying to do is not possible,

Thus far you are correct ...

Quote:
> as the size of
> objects are calculated at COMPILE time, NOT during RUN time.

but this is not the reason why ! The size of objects is indeed calculated
at compile time, simply because it is constant, but it can nevertheless be
used freely at runtime, if it is known. The real problem is that an object
of type "void" does exist and does not have a size.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sat, 24 Nov 2001 03:00:00 GMT  
 Problem with sizeof of a struct void pointer

Quote:

>Hi,

>I have a problem with getting the size of a struct. I have a struct in
>main and a give it to a function by a void pointer. The problem is that
>I don't get the size of this struct. I have included the source of the
>main and the function.

It is sometimes possible to get a feel for this sort of thing by looking
at the standard library. For example take a look at a function you call
in your code:

...

Quote:
>  fwrite(&header, sizeof(header_struct), 1, file);
>  fwrite(record, sizeof(*record)), size, file);

fwrite() requires that you pass it the size of the array (in fact the
number of elements and the same of each element). If standard library
functions can't work this sort of stuff out for themselves it is unlikely
that you code will be able to either. Look at other standard library
functions e.g. fgets(), memcpy(), qsort(). A natural solution for this
sort of problem is to pass the size as an extra argument to the function.

--
-----------------------------------------


-----------------------------------------



Sun, 25 Nov 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. sizeof (struct) ! = sizeof struct elements

2. relation between sizeof(void*), sizeof(int)

3. sizeof(void*) versus sizeof(foo*)

4. casting of struct element void pointer casting

5. structs and void pointers

6. void pointer to a struct?

7. problem with pointer to pointer to void

8. SizeOf Misreporting Struct w/ Bitfield, Union, and Nested Struct

9. pointers to functions in structs which take a pointer to that struct as an arg

10. cast pointer to struct to pointer to type of first member of struct

11. void pointer (void *) : how to determine type?

12. from void (void*) to void (_cdecl*) (void*)

 

 
Powered by phpBB® Forum Software