sizeof help cont., ignore other post please

B

bob

sorry to double, triple post but

testFile.txt has 28 character (what editor says anyhow): "nothing here,
see I'm empty"

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?
 
L

Larry I Smith

bob said:
sorry to double, triple post but

testFile.txt has 28 character (what editor says anyhow): "nothing here,
see I'm empty"

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);

printf("buffer size: %d\n", count);
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 used malloc() to allocate it, so YOU have to remember the
size you passed to malloc().
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?

For the nul-terminator on the end of the string.

This is C code, and C strings; a nul byte at the end
of the C string is required for printf("%s"), strlen(), etc.

You might use a C++ std::string instead of malloc(), then
you could 'ask' the string how long it is using its length()
member function. Read up on the subject.

Regards,
Larry
 
J

Jack Klein

sorry to double, triple post but

testFile.txt has 28 character (what editor says anyhow): "nothing here,
see I'm empty"

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?

That's pretty obvious, isn't it? The size of the buffer is "count"
bytes. You know that when you allocate it. If you're going to need
the information later, remember it. Keep "count" around. If you are
going to need it other places, in other functions, pass the size as a
second parameter to functions. Or instead of passing the bare
pointer, define a structure with two members, a pointer to allocated
memory and a size_t containing the size of the allocation.
Also one more question.


size_t itemsize = sizeof(char);

The line above is completely unnecessary for two reasons. First,
there is no need to store the result of the sizeof operator in an
object, you can just use the sizeof expression directly in the
function call.

Second, there is never a need for sizeof(char) in C or C++.
sizeof(char) is 1 by definition in C and C++. It always has been. And
it always will be.
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?

You are trying to mix direct and string oriented functions in an
improper manner. The printf() function with a "%s" conversion
specifier requires a C string, that is an array of characters that
includes a terminating '\0' character. The direct input function
fread() reads exactly the number of characters that you tell it, and
knows nothing at all about strings. Specifically it does not add an
extra terminating '\0' at the end to make the data into a string.

In fact, there is no reason that allocating one extra byte should make
any difference. Memory allocated with malloc() is not initialized to
any particular value, unless perhaps you are compiling in some sort of
debug mode and in that mode your compiler uses a version that zeros
out allocated memory.

Without specifically having that '\0' at the end, you are invoking
undefined behavior by passing an array of chars that is not a valid
string to printf().

So you need to not only allocate the extra byte for the '\0', you need
to put it there yourself to make sure it is there. After the fread(),
you should execute:

buffer [filestream] = '\0';
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top