Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof help

Reply
Thread Tools

sizeof help

 
 
bob
Guest
Posts: n/a
 
      02-12-2006
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?

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      02-12-2006

"bob" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: 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


 
Reply With Quote
 
 
 
 
Audison.Athena
Guest
Posts: n/a
 
      02-12-2006
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].

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments