• Welcome to Valhalla Legends Archive.
 

Declaring Variables

Started by Crypticflare, November 05, 2003, 09:44 PM

Previous topic - Next topic

Crypticflare

I've been reading in my book and I got up to the declaring variables and naming them, and I'm a bit sketchy yet on how to declare say a string of text. I was wondering if someone could be kind enough to show me an example of the correct format.

I'm using Microsoft Visual C++ compiler if that makes a difference (not sure)

Thanks!

CupHead

#1

char MyString[] = "Blah blah blah";

char MyString[256];

char *MyString;
MyString = new char[256];



No guarantees that those are all right, just going off of memory.

Edit:
Oh, easiest way is probably this:

#include <string>
using std::string;

string MyString;


Edit2: Fixed code.

Eibro

Quote from: CupHead on November 05, 2003, 09:59 PM

char [] MyString = "Blah blah blah";

char MyString[256];

char *MyString;
MyString = new char[256];



No guarantees that those are all right, just going off of memory.

Edit:
Oh, easiest way is probably this:

#include <string.h>

string MyString;

Pretty close:
Should be: char MyString[] = "Blah blah blah"; or char* MyString = "Blah blah blah";
Also, you need to #include <string> for std::string, not string.h:
#include <string>

std::string myString;
Eibro of Yeti Lovers.

iago

#3
Easier would be to declare namespace:

#include <iostream>
#include <string>
using namespace std;

void main()
{
string TestString = "Moo";
cout << TestString << endl;
TestString = TestString + "Wee!";
cout << TestString << endl;
cout << "Enter your string:" << endl;
cin >> TestString;
cout << "You entered: " << TestString << "!!" << endl;
}


hopefully that works, but it should be a simple enough program to play with/learn from :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Kp

Unfortunately, someone associated with C++/STL decided to create a C++ class named string which handles a great deal of string related functionality.  It should be noted that this is not a built-in type and as such cannot be directly used with system functions (e.g. send, recv, CreateFile).  You can use the c_str() method of the string class to get at its enclosed data for sending, but you'll have to do some extra work if you want to read a string from the system and then put it in the string class.

Prior to the C++ string class, string typically referred to an array of characters, and CupHead/iago already covered how to do that.  Beware that when using a character array, it is your problem to ensure sufficient space is available.  The following will work, but produce bad results.
char x[4]; strcpy (x, "a very long string that doesn't fit");The compiler will (probably) let you compile this, but running it will result in a buffer overrun of x, which will likely cause your program to crash.  Careless string copying is the source of most buffer overflow exploits in programs; afaik, it's difficult/impossible to do this in "managed" languages like Java and C# because the compiler does lots of extra checking on array use.  In C and normal C++, you're responsible for checking that condition manually.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Skywing

#5
Note that void main() is illegal, iago.

iago

Quote from: Skywing on November 06, 2003, 10:48 AM
Note that void main() is illegal, iago.

It'll compile in his compiler!  I wanted to give him something easy.

Kp - it was Eibro that posted about char* I think, I didn't.

And note that he hasn't gotten past variable declarations.  Introducing him to string is the easiest way of doing it, he can worry about how it's implemented later.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on November 06, 2003, 11:53 AM
Quote from: Skywing on November 06, 2003, 10:48 AM
Note that void main() is illegal, iago.

It'll compile in his compiler!  I wanted to give him something easy.

Kp - it was Eibro that posted about char* I think, I didn't.

And note that he hasn't gotten past variable declarations.  Introducing him to string is the easiest way of doing it, he can worry about how it's implemented later.
So you think it's a good idea to teach people to do stuff the wrong way, just because a particular compiler supports it...?

iago

#8
okokok, I recant.  I edited my post and promise to never EVER for ANY REASON use or teach void main() to anybody ever again.  Happy? :P


lol, can't modify after 10 minutes.  So, here's fix:
Quote from: iago on November 06, 2003, 02:33 AM
Easier would be to declare namespace:

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
string TestString = "Moo";
cout << TestString << endl;
TestString = TestString + "Wee!";
cout << TestString << endl;
cout << "Enter your string:" << endl;
cin >> TestString;
cout << "You entered: " << TestString << "!!" << endl;
}


hopefully that works, but it should be a simple enough program to play with/learn from :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Eibro

#9
Quote from: Kp on November 06, 2003, 09:42 AM
Unfortunately, someone associated with C++/STL decided to create a C++ class named string which handles a great deal of string related functionality.  It should be noted that this is not a built-in type and as such cannot be directly used with system functions (e.g. send, recv, CreateFile).  You can use the c_str() method of the string class to get at its enclosed data for sending, but you'll have to do some extra work if you want to read a string from the system and then put it in the string class.

Prior to the C++ string class, string typically referred to an array of characters, and CupHead/iago already covered how to do that.  Beware that when using a character array, it is your problem to ensure sufficient space is available.  The following will work, but produce bad results.
char x[4]; strcpy (x, "a very long string that doesn't fit");The compiler will (probably) let you compile this, but running it will result in a buffer overrun of x, which will likely cause your program to crash.  Careless string copying is the source of most buffer overflow exploits in programs; afaik, it's difficult/impossible to do this in "managed" languages like Java and C# because the compiler does lots of extra checking on array use.  In C and normal C++, you're responsible for checking that condition manually.
With a little thrusting here and there, you can use std::string just as you would a char* :)
Example of retrieving window text (GetWindowText) straight into an std::string buffer:


// tstring is: typedef std::basic_string<TCHAR> tstring;

tstring IWindow::Text() const
{
  int textLength = GetWindowTextLength( m_hWnd );

  // Neat (icky) trick. Use std::string internal buffer as buffer
  // by reserving specified amount of space and casting const-ness away
  tstring stringBuf( textLength, TCHAR( '\0' ) );

  TCHAR* bufHandle = const_cast<TCHAR*>( stringBuf.c_str() );

  int charsCopied = GetWindowText( m_hWnd, bufHandle, textLength );

  // must resize to actual length, or std::string will flip out
  // when it goes to deallocate
  stringBuf.resize( charsCopied );

  return stringBuf;
}


Then to pass an std::string to functions that want a const char*, do something like:
template <typename T>
class stringEx : public std::basic_string<T>
{
public:
    operator const char*() const { return this->c_str(); }
};
It's flawless!
Eibro of Yeti Lovers.

iago

What if the functions need a non-constant string?  A couple time I've done, "send((char*)myString.c_str(), ..);", but I know that's a bad idea.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: Eibro on November 06, 2003, 02:49 PM
Quote from: Kp on November 06, 2003, 09:42 AM
Unfortunately, someone associated with C++/STL decided to create a C++ class named string which handles a great deal of string related functionality.  It should be noted that this is not a built-in type and as such cannot be directly used with system functions (e.g. send, recv, CreateFile).  You can use the c_str() method of the string class to get at its enclosed data for sending, but you'll have to do some extra work if you want to read a string from the system and then put it in the string class.

Prior to the C++ string class, string typically referred to an array of characters, and CupHead/iago already covered how to do that.  Beware that when using a character array, it is your problem to ensure sufficient space is available.  The following will work, but produce bad results.
char x[4]; strcpy (x, "a very long string that doesn't fit");The compiler will (probably) let you compile this, but running it will result in a buffer overrun of x, which will likely cause your program to crash.  Careless string copying is the source of most buffer overflow exploits in programs; afaik, it's difficult/impossible to do this in "managed" languages like Java and C# because the compiler does lots of extra checking on array use.  In C and normal C++, you're responsible for checking that condition manually.
With a little thrusting here and there, you can use std::string just as you would a char* :)
Example of retrieving window text (GetWindowText) straight into an std::string buffer:


// tstring is: typedef std::basic_string<TCHAR> tstring;

tstring IWindow::Text() const
{
  int textLength = GetWindowTextLength( m_hWnd );

  // Neat (icky) trick. Use std::string internal buffer as buffer
  // by reserving specified amount of space and casting const-ness away
  tstring stringBuf( textLength, TCHAR( '\0' ) );

  TCHAR* bufHandle = const_cast<TCHAR*>( stringBuf.c_str() );

  int charsCopied = GetWindowText( m_hWnd, bufHandle, textLength );

  // must resize to actual length, or std::string will flip out
  // when it goes to deallocate
  stringBuf.resize( charsCopied );

  return stringBuf;
}


Then to pass an std::string to functions that want a const char*, do something like:
template <typename T>
class stringEx : public std::basic_string<T>
{
public:
    operator const char*() const { return this->c_str(); }
};
It's flawless!

That's 'const' for a reason.  You're violating the C++ standard.

iago

I was saying, "what if" he needed a non-const char* for an api call?
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Etheran

Object-Oriented Programming Using C++ Second Edition -- Joyce Farrell
*ALL* of her examples are void main().. I think we need to denounce this book!!

MrRaza

I believe either way works fine, but skywing is correct. By the way, I think most people are getting ahead of themselves, remember, he's just learning to declare variables....