Jump to content

C++ Strings

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
xXAlphaXx

xXAlphaXx

    Learning Programmer

  • Members
  • PipPipPip
  • 85 posts
I've been told that their are two ways too put a string in. Either through an array of chars or using #include<string> Wouldn't the string class be less memory consuming?

Pros and cons of each?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Actually, the string class will use more memory, but is also safer.

An array of chars is a fixed sized array, and requires null termination. Buffer overruns are VERY easy if you aren't careful when getting input.

The string class will use an array internally, but also stores other values. It makes buffer overruns very difficult, so you will spend much less time worrying about those issues when getting input.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
You can do as such without using include<string>
string[90]
That will hold 89 characters.
Or you could use string library wich is easier :)

#4
BlueMelon

BlueMelon

    Newbie

  • Members
  • PipPip
  • 28 posts
You could use

#include <string>
std::string mystring;

or use a char array, I kinda like the char array, but both are alright.

#5
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts

Moudi said:

You can do as such without using include<string>
string[90]
That will hold 89 characters.

Actually it holds 90 characters.