473,785 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof help cont., ignore other post please

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(filestrea m, 0, SEEK-END);
count = ftell(filestrea m) + 1;
rewind(filestre am);
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,it emsize, 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(filestrea m) + 1;
as I did above. Why do I have to add 1 more byte?

Feb 12 '06 #1
2 1645
bob wrote:
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(filestrea m, 0, SEEK-END);
count = ftell(filestrea m) + 1;
rewind(filestre am);
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,it emsize, 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(filestrea m) + 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
Feb 12 '06 #2
On 11 Feb 2006 19:52:46 -0800, "bob" <op******@hotma il.com> wrote in
comp.lang.c++:
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(filestrea m, 0, SEEK-END);
count = ftell(filestrea m) + 1;
rewind(filestre am);
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,it emsize, 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(filestrea m) + 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';

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 12 '06 #3

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

Similar topics

5
2187
by: gelbeiche | last post by:
Is ( cont.begin() == cont.end() ) essentially equivalent to writing ( cont.empty() ) for a STL container ?
31
2390
by: Anjali M | last post by:
#include <stdio.h> int main() { int number = 10; printf("Number is: %d\n", number); printf("Sizeof of number is: %d\n", sizeof(number++)); printf("Number now is: %d\n", number);
12
2422
by: sonu | last post by:
#include<stdio.h> main() { int x=10,y; y=sizeof(++x); printf("x=%d\ny=%d\n",x,y); } Oput Put
4
17182
by: vijay | last post by:
hi, if you see assembly then sizeof operator has sizeof(type) or sizeof(variable) at compile time. How does C compiler gets value at compiler time.? How can we implement sizeof operator? the implementation as macro is as below #define sizeof_op1(val) (&val +1 ) - &val // for variable ex
72
3047
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
5
106357
Atli
by: Atli | last post by:
Hi everybody. After years of C# and PHP, I'm finally returning to Java. My goal is to create a Java program capable of sending images to a PHP Photo Album on my web server. Right now, however, I am stuck trying to send simple text variables through POST to my PHP script. The code does seem to connect to the script like it is supposed to, but it seems unable to send the POST variables. The PHP script is returning a 'Undefined index'...
5
2013
by: vicks | last post by:
Hi, I had just written the following code but not able to figure out why the sizeof() operator is printing the weired output when i give a function name as its operand? #include<stdio.h> #include <assert.h> int f();
14
2797
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
1
1607
by: DaTurk | last post by:
Sorry, some how I clicked post before I finished. Hi, I'm writing an application that has a native c++ layer, and a CLi/C++ layer above it. Above the CLi layer will be C# but that's not relavent to this post. My question has to do with communicating between the native layer to the CLi layer. Typically how I do this is this ...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.