sizeof help

Discussion in 'C++' started by bob, Feb 12, 2006.

  1. bob

    bob Guest

    how can I get the size of this 'buffer':

    FILE *filestream;
    filestream = fopen("testFile.txt", "r+"};
    char *buffer;
    size_t count;
    fseek(filestream, 0, SEEK-END);
    count = ftell(filestream) + 1;
    rewind(filestream);
    buffer = (char*) malloc(count);
    printf("buffer size: %d\n", sizeof buffer);

    I keep getting 4, for the pointer. which actually makes sense .. I
    think. So how can I find out the size of the 'buffer' that buffer
    points to?

    Also one more question.

    size_t itemsize = sizeof(char);
    size_t returnvalue;
    returnvalue = fread(buffer,itemsize, count, filestream);
    printf("fread returns: %d\n", returnvalue);
    printf("fread read filestream into buffer, see: %s\n", buffer);

    The final printf function won't work rigth, unless I add one more byte
    to the buffer...i.e.

    count = ftell(filestream) + 1;

    as I did above. Why do I have to add 1 more byte?
     
    bob, Feb 12, 2006
    #1
    1. Advertising

  2. "bob" <> wrote in message
    news:...
    : how can I get the size of this 'buffer':
    :
    : FILE *filestream;
    : filestream = fopen("testFile.txt", "r+"};
    : char *buffer;
    : size_t count;
    : fseek(filestream, 0, SEEK-END);
    : count = ftell(filestream) + 1;
    : rewind(filestream);
    : buffer = (char*) malloc(count);
    : printf("buffer size: %d\n", sizeof buffer);
    :
    : I keep getting 4, for the pointer. which actually makes sense .. I
    : think. So how can I find out the size of the 'buffer' that buffer
    : points to?
    You can't -- in C, with malloc, the caller is responsible to
    remember the size of the memory block that it has allocated.

    In C++, you should use #include <vector> and write:
    std::vector<char> buffer;
    ...
    buffer.resize( count ); // instead of malloc
    printf("buffer size: %d\n", (long)buffer.size() );
    //or better:
    // std::cout<<"buffer size: "<<buffer.size()<<'\n';

    : Also one more question.
    :
    : size_t itemsize = sizeof(char);
    : size_t returnvalue;
    : returnvalue = fread(buffer,itemsize, count, filestream);
    : printf("fread returns: %d\n", returnvalue);
    : printf("fread read filestream into buffer, see: %s\n", buffer);
    :
    : The final printf function won't work rigth, unless I add one more byte
    : to the buffer...i.e.
    : count = ftell(filestream) + 1;
    :
    : as I did above. Why do I have to add 1 more byte?

    As you used it, printf expects a zero-terminated string.
    Again, a better way is:
    std::cout.write( &buffer[0], buffer.size() );


    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Feb 12, 2006
    #2
    1. Advertising

  3. when the type of operand expression of sizeof operator is an array
    type, the result is the total number bytes in the array(the size of an
    array of n elements is n times the size of an element). however here
    the type of the operand expression is pointer to char, not
    char[constant-expression].
     
    Audison.Athena, Feb 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,338
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    633
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,426
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    560
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    497
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page