473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

sizeof help

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

Feb 12 '06 #1
2 2038

"bob" <op******@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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
Feb 12 '06 #2
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].

Feb 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Metzen | last post by:
hello, ok, I want to find the length of an int array that is being passed to a function: main() { int array={1,2,3,4,5,6,7,8}; function(array); } function(int *array) {
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
12
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class...
3
by: kimi | last post by:
Hello, yes i try to learn c-programming.So i have a question on my code, but if i am wrong here please tell me a group where i belong to. I wrote a function which fgets a filename from the...
21
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
7
by: guoliang | last post by:
help: i try the code: " char str={'h','e','l','l','o','\n','\0'}; char *pstr=str; printf("sizeof=%d\n",sizeof(str)); printf("sizeof=%d\n",sizeof(pstr)); //i want to get sizeof(str) not...
15
by: somenath | last post by:
Hi All, I am not able to understand the behavior of the bellow mentioned program. #include<stdio.h> int main(void) { printf("\n Size = %d \n",sizeof 1<0); return 0;
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.