0

Ive moved from autoit and am now learning C++ and some problems keep coming up.

The first one is storing character input. The problem is that I have no idea how many characters are in that line of the file (if you are reading a file) or how many letters a user is going to type (in a console application).

What is the best way to approach this problem?? I have heard about the string class, but I want to avoid it becuase I dont know how it works and that leads to vunerabilities etc.

Secondly...

In C you can load shellcode into memory, create a function pointer, and execute that code. Is there any mechanism for this in C++???

Thirdly...

How does the interpreter iterate through char arrays for string output??? (char array[3];) Does the compiler keep track of the size of the array, or does it just keep reading from memory until it hits that \0 thing???

Lastly...

If Char * are just pointers to data in memory, then why does:

char * title = "Program Title";

this work??? where is the string literal stored in memory?? how is it referenced???

Thankyou very much. I greatly appreciate your help.

-Hyperzap

user4157124
  • 2,809
  • 13
  • 27
  • 42
64bit_twitchyliquid
  • 894
  • 2
  • 10
  • 22
  • 3
    Welcome to Stack Overflow! May I suggest that you find, study, and learn from a good book? See [the C++ FAQ](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on this site for a list of excellent books from which to learn. – Robᵩ May 01 '11 at 10:46
  • 1
    hyperzap, I thought you were banned from computer for the next 8 weeks and then I see you here, trying to learn C++. Nice. – Jos van Egmond May 01 '11 at 15:06

2 Answers2

2
  1. Investing your time in learning std::string is well worth the effort, as it takes care of a lot of hassle for you. If you don't want to take advantage of the features in C++, then why use C++ and not just C?
  2. You can use the same code for this as you would in C.
  3. Yes, iostream-output of C-style strings outputs until terminating zero. Once again, if you use std::string you do not have to care about such details.
  4. Correct me if I'm wrong, but I think title would be a const char[] stored on the stack.

Example:

const char* hello = "Hello\0World";
cout << hello; // Prints only "Hello", i.e. up to terminating zero (\0)

The reason this works:

const char* hello = "Hello world";
cout << hello;

is because hello is really "Hello world\0"; - in other words, the compiler inserts a terminating zero.

Note that std::string doesn't do any magic. It too reads until the terminating zero:

string hello = "Hello\0World\n";
cout << hello; // Still only gives "Hello"
Lstor
  • 2,265
  • 17
  • 25
  • Thankyou. But how does the std::string actually store data when it does not know the size of the data before hand?? – 64bit_twitchyliquid May 01 '11 at 10:08
  • `std::string` keeps track of the size internally. When initialized with a string literal, e.g. "Hello world", it just reads how many characters there are until the terminating zero. – Lstor May 01 '11 at 10:10
  • I find the last example misleading, in that particular case since the `std::string` is constructed from a literal it will just read up until '\0', but a string can contain internal '\0' perfectly: `const char literal[] = "Hello\0World"; std::string s( literal, literal+sizeof(literal)-1 );` Will create a string that contains `"Hello\0World". And it does do some *magic*, whenever it needs to it grows in size, avoiding buffer overflows so it will in fact be safer in most cases. – David Rodríguez - dribeas May 01 '11 at 11:02
  • what is the "magic" that allows it to grow to fit new information? linked list? – 64bit_twitchyliquid May 02 '11 at 07:44
  • @user733100 That's implementation defined, but one implementation could be having an array of `char_type`, and allocating a larger array whenever the string is changed in such a way that its `size` would be larger than its `capacity`. It would then copy its contents to the new, larger array. It's not really _magic_, it's just more advanced than a basic array. – Lstor May 02 '11 at 12:48
0

char* title = "String Literal" works because the compiler preallocates a memory location to store your string literal, thus you then get back a pointer to this memory location. In c++, an array for which you know the size at compile time (as in your example: char array[3] is a type in itself, so the compiler does keep track of the size. However, if you do not know the size (ie. char array[]), it is just a pointer to char. However, you should be using std::vector in c++ (better safety and performance). I'm not too sure about your other two questions (didn't quite understand them)

wendazhou
  • 295
  • 2
  • 10
  • when I do cout << title;, where title is a char title[] = "WORD";, how does the compiler know the length of the string, and thus print it to the screen correctly??? how is the char represented in memory??? – 64bit_twitchyliquid May 01 '11 at 09:59
  • First question: basically I want to know the best way to get and store data from a file, considering I cannot get the length of the data. – 64bit_twitchyliquid May 01 '11 at 10:01
  • @user733100: the compiler knows the size of the literal, it needs to know it to reserve space for the literal in the object file. But that is not what goes on in `cout << "Hi"`. Rather the literal decays into a `const char*` to the first element, and that pointer is passed to the function. Literals have a trailing '\0' character, and the function that implements the `<<` operator keeps writting characters until it finds that char. – David Rodríguez - dribeas May 01 '11 at 11:04