char * t = "test";
delete t;
char * t = "test";
delete t;
t[0] = 'b'
(because that's better :), it may crash (or not depending on the compiler). You should assign it like this:const char *t = "test";
#include <stdio.h>char *t = strdup("test");
Quote:Original post by SiCraneOk, somewhere in my brain the same thing you just told me lurks, but is there a paper or something where i can read things like that up? I think the topic is something like "immediates".
Don't delete it, only delete things you new. In this case, the compiler usually allocates the string in a read only data segment of the executable image, which will cause a problem if you try calling delete on it.
Quote:Original post by dimeboltOk, i looked it up and stuff created with strdup has to be freed with free.
Yes, don't delete it. It's probably a pointer in constant space. In fact don't do this at all. If you would later on do something like(because that's better :), it may crash (or not depending on the compiler). You should assign it like this:t[0] = 'b'
const char *t = "test";
or#include <stdio.h>char *t = strdup("test");
The first will make sure you can't accidently modify the contents. The second approach will make a copy that is freely accessable (but fixed size!).
Tom
Quote:Original post by RDragon1Man i would gladly leave old scool c style strings behind me, but i am working on a font class for opengl and i need something like glPrint(int x, int y, char * str, ...).
Typically prefer std::string over C-style strings when using C++... You really are wasting much more time and grinding over unnecessary details and complexities. Strings should be easy to work with.
Quote:Original post by RDragon1Streams are cool but it takes so long to write stuff into them. Here is the idea i got:
Try std::stringstream.
std::stringstream ss;
ss << "X location: " << x << " Y location: " << y;
ss.str() returns a std::string
// test07string.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <windows.h> // Header File For Windows#include <iostream>#include <stdio.h>using namespace std;std::string myprint(const char *str, ...){ char text[256]; // Holds Our String va_list ap; // Pointer To List Of Arguments if (str == NULL) // If There's No Text return string(" "); // Do Nothing va_start(ap, str); // Parses The String For Variables vsprintf(text, str, ap); // And Converts Symbols To Actual Numbers va_end(ap); // Results Are Stored In Text return string(text);}int _tmain(int argc, _TCHAR* argv[]){ cout << myprint("hugo ruft egon %i, %.1f",5,4.5f).c_str() << endl; return 0;}// ---------------------output:hugo ruft egon 5, 4.5Press any key to continue
Quote:Original post by RDragon1Am I the only one who finds C-type strings easier and much less confusing? [rolleyes]
Typically prefer std::string over C-style strings when using C++... You really are wasting much more time and grinding over unnecessary details and complexities. Strings should be easy to work with.
Quote:Original post by Jonus
Streams are cool but it takes so long to write stuff into them.
#include <iostream>#include <string>#include <sstream>using namespace std;class text { stringstream ss; public: template <typename T> explicit text(const T& first) : ss() { ss << first; } text() : ss() {} template <typename T> text& operator() (const T& next) { ss << next; return *this; } operator std::string () { return ss.str(); } // <-- maybe not a good idea?};void funkyPrint(int x, int y, const std::string& input) { cout << "Pretending to print '" << input << "' at (" << x << ", " << y << ")" << endl;}int main() { // Now we get this nice, simple usage and we can just append what we like // (of whatever data type) without worrying about formatting codes: funkyPrint(0, 0, text("testing, ")(string("test") + "ing, ")(123)); // we could provide stream manipulators like std::hex, too - anything that // the underlying stream could make use of.}