Jump to content
  • Advertisement
Sign in to follow this  
Jonus

Question about: char * t = "test"

This topic is 4766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When i do someonething like this in cpp ...
char * t = "test";
... do i have to delete the t later?
delete t;
I know that you have to delete everything you create with new, but the "test" string is created by the compiler so i don't know what to do with it.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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
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";

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

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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.
Ok, 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".

Quote:
Original post by dimebolt
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
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";

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
Ok, i looked it up and stuff created with strdup has to be freed with free.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
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.
Man 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, ...).

When i try to think of a way to do this in c++/strings/streams then i go astray or maybe i just got a good idea.

Share this post


Link to post
Share on other sites
Try std::stringstream.

std::stringstream ss;

ss << "X location: " << x << " Y location: " << y;

ss.str() returns a std::string

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
Try std::stringstream.

std::stringstream ss;

ss << "X location: " << x << " Y location: " << y;

ss.str() returns a std::string
Streams are cool but it takes so long to write stuff into them. Here is the idea i got:
// 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.5
Press any key to continue



Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
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.
Am I the only one who finds C-type strings easier and much less confusing? [rolleyes]
...and don't even get me started on streams.

I should have known that starting out with ASM was a bad plan.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jonus
Streams are cool but it takes so long to write stuff into them.


???

If you're talking about the time the computer takes, it is dominated by the time needed for the actual I/O anyway (all the internal system stuff that translates your data into pixel information for the screen, after looking at the font data, checking where the windows are, etc. etc.).

If you're talking about the time it takes you, haven't you noticed how much extra time it takes to type out all those weird formatting codes - and get them right?

Anyway, with a bit of setup work, you can do this stuff in-line with the streams:


#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.
}


Unfortunately, just creating a stringstream in-line, doing insertions and then calling .str() won't work, because the insertion operator returns a reference to the base ostream class (which doesn't provide .str()). I think I saw someone make it work once, but it wasn't at all pretty.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!