Archived

This topic is now archived and is closed to further replies.

Whisk

Size diff between char and string...

Recommended Posts

Whisk    122
Hi, Using the code snippet at the bottom of this post, can somebody tell me the following... 1) Why is there a size difference between c5 and st? 2) When dispaying the contents of c5 why do I get garbage values from further down the memory? 3) Why can I access undeclared indexes of c5. Just in case... I'm using a PIII Pentium 450 with 256MBRAM with DEV-C++ (Bloodshed). the output I get on my pc is: char array = 5 string = 4 HelloE x HelloE Hello Press any key to continue . . .
    
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
  char c5[5] = { 'H', 'e', 'l', 'l', 'o' };
  string st = "Hello";

  cout << "char array = " << sizeof(c5)
       << "\nstring = " << sizeof(st)
       << endl << endl
       << c5[0] << c5[1] << c5[2] << c5[3]
       << c5[4] << c5[5] << c5[6] << c5[7]
       << endl << c5
       << endl << st
       << endl;

    system("PAUSE");
    return 0;
}
    
Thanks! Edited by - Whisk on January 1, 2002 9:43:20 PM

Share this post


Link to post
Share on other sites
Null and Void    1088
1) Since c5 is an auto allocated array of 5 characters. It should have been 6 characters to include a NULL terminater if you plan to use it as a string. That's why when you cout c5 you get some random data afterwards. However, st will be (is being) seen as a class with a pointer to some memory (which holds 6 characters).
2) What did you expect to get?
3) The memory is still there, it just isn't part of c5. The compiler isn't doing index checking for you. Not a big deal as long as you pay attention.
For future reference: The compiler behind Dev C++ is called GCC.

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on January 1, 2002 9:53:14 PM

Share this post


Link to post
Share on other sites
Whisk    122
quote:
Original post by Null and Void
1) Since c5 is an auto allocated array of 5 characters. It should have been 6 characters to include a NULL terminater if you plan to use it as a string. That's why when you cout c5 you get some random data afterwards. However, st will be (is being) seen as a class with a pointer to some memory (which holds 6 characters).
2) What did you expect to get?
3) The memory is still there, it just isn't part of c5. The compiler isn't doing index checking for you. Not a big deal as long as you pay attention.
For future reference: The compiler behind Dev C++ is called GCC.



Thanks Null and Void,

1) I forgot to use the NULL terminater, but why is st which holds "Hello" only 4 bytes in size... shouldn't it be 5 bytes?
2) I expected to get "hello" displayed not the extra characters, but I realise now that was my bad, forgot the NULL terminater.
3) So is the non-existant index checking is specific to GCC compiler.. because back when I was using java I'm pretty sure you get a runtime error.

The strange looking code started from comparing the different byte sizes of data types as an exercise... then while doing this excerise I was interested if a char array and string with the same contents would be the same in size, I actually thought that string would be larger in size because its not a built-in data type...

To wrap up, I am all clear on everything except why st holding a 5 character string is only 4 bytes long considering a character takes up a byte..

Thanks again for all your help Null and Void..


Edited by - Whisk on January 1, 2002 11:54:15 PM

Share this post


Link to post
Share on other sites
krez    443
quote:
Original post by Whisk
1) I forgot to use the NULL terminater, but why is st which holds "Hello" only 4 bytes in size... shouldn''t it be 5 bytes?

4 bytes is the size of the pointer to st.
quote:
3) So is the non-existant index checking is specific to GCC compiler.. because back when I was using java I''m pretty sure you get a runtime error.

nope, c & c++ don''t check array bounds, so you have to be careful.

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
a person    118
1. that is due to the sizeof() operator handling things differently then you expect (ie taking the size of the pointer to the string). you should try not to use sizeof() for things like this. since the compiler treats char[5] as a 5 byte struct and the string (which gets type casted to a char* when using sizeof) as 4 bytes no matter how long the string is (4 bytes being the size of a pointer).

3. NEVER access out you allocated memory. you WILL get an access error sooner or later depending on how the memory is allocated. for instance you could concievable modify memory that belongs to you, but is being used for a different "varible". thus screwing up thing beyond anything you have encountered before with bugs that crop up and are difficult ot track.

Share this post


Link to post
Share on other sites
Whisk    122
Thank you Null and Void, Krez and a person for all your help.

I sorta thought 4 bytes was the size of the pointer, Null and Void was saying that st was a class with a pointer to some memory but for some reason I thought a pointers size was dependant on the data type, but thats just silly considering that a pointer holds the ''address'' to some other place in memory, not sure where I got that idea a pointers size varies.

I was hoping Null and Void would clear that up for me, thanks Krez and a person for filling me in on that point.

Share this post


Link to post
Share on other sites