Original Post
So was sprintf and all there related functions, is there any C alternatives I can use?
#include <string>#include <iostream>using namespace std;int main() { string x = "y helo thar"; // implicitly invoked constructor: std::string(const char*) string y; y = x; // copies the string as per str*cpy-type functions y[5] = 'l'; // omg I make dirty wr0d :( cout << x << endl << y << endl;}Quote:
Original post by ph33r
So was sprintf and all there related functions, is there any C alternatives I can use?
Quote:
Original post by ph33r
So was sprintf and all there related functions, is there any C alternatives I can use?
strncpy_sQuote:
Original post by Omaha
There is absolutely no reason not to use char arrays or pointers for string usage even if your code is native C++.
a = malloc( strlen( b ) + strlen( c ) + 1 );strcpy( a , b );strcat( a , c );Quote:
Original post by Omaha
Added ESPECIALLY if you're going to expose the whole std namespace just to use the string class!
Quote:
Original post by Anonymous Poster Quote:
Original post by MaulingMonkey Quote:
Original post by Omaha
There is absolutely no reason not to use char arrays or pointers for string usage even if your code is native C++.
Wrong
1) If you use any function which has to find the end of the string (std::strlen, std::strcat, etc) there's a good to perfect chance std::string will outpreform since it stores the string's length. This changes appending a series of data to a string from an O(N*N) to an O(N) complexity operation - see "Joel on Software - Back to Basics" for a full explaination and history about the dreaded painters algorithm.
2) std::string manages it's memory. Randomly malloced char pointer? Welcome to memory leak city...
3) a = b + c; - works with std::string, causes a to point to a random memory location with char pointers. The C version?
a = malloc( strlen( b ) + strlen( c ) );
strcpy( a , b );
strcat( a , c );
That's no less than 3 lines of code, AND it uses the painters algorithm (which is bad, so there's a good chance "a = b + c" will outpreform this version), AND we're omitting the free( a ) statement which we must call or suffer a memory leak.
You do realize that Omaha's post doesn't disagree with anything you've said.
Quote:
He was saying that if you're just holding text or keeping a small buffer, std::string is overkill;
Quote:
Note that I'm not disagreeing with your overall argument. Whether by accident or intentional, the buffer overrun in (3) shows your point.
Quote:Just FYI, Shlemiel the painter's algorithm is not the same thing as the Painter's algorithm.
Original post by MaulingMonkey
see "Joel on Software - Back to Basics" for a full explaination and history about the dreaded painters algorithm.
Quote:
Original post by Oluseyi Quote:Just FYI, Shlemiel the painter's algorithm is not the same thing as the Painter's algorithm.
Original post by MaulingMonkey
see "Joel on Software - Back to Basics" for a full explaination and history about the dreaded painters algorithm.
Quote:
Original post by Anonymous Poster
What part of "If you want to do lots of concatenation and inserting and stuff like that, then you might want to look into std::string or some other implementation of a string wrapper" don't you understand? It wasn't a blanket statement.
Quote:
You also seem really caught up on this speed thing.
Quote:
Note that one can also optimize for space.
Quote:
If you say the space gains are negligible, I'll just tell you that your speed gains are negligible (considering the average size of strings). For some string operations, it's entirely possible that the reduced overhead of arrays is faster (smaller constant).
Quote:
(I remember reading a comment in the STL headers that came with GCC saying that their list didn't give a length in constant time because they didn't want the overhead)
Quote:
Quote:
These are all good reason not to use manual pointers, which Omaha claimed do not exist. I have listed them as proof that there are reasons not to use them, dispelling this horrible claim (wheither actually intended or simply the result of not clearly expressing something else that was meant).
Actually, he said that there's no reason not to use manual pointers in a given situation. If you hadn't snipped all the context, you would've seen that.
Quote:
"Just use a stack allocator"? Many programmers here don't even know what an allocator is or how to properly use them.
Quote:
I'm always torn when I use C++. Part of me loves all the machinery that you have to play with. Part of me loathes how hard it is to do such a simple thing "properly".
Quote:
Quote:
Note that I'm not disagreeing with your overall argument. Whether by accident or intentional, the buffer overrun in (3) shows your point.
And I didn't even list that reason specifically. See how easy it is to misuse char pointers kids? That should've been "strlen( b ) + strlen( c ) + 1".
Quote:
Quote:
My point is that there's perfectly good reasons to avoid char pointers. Sure, if you've got a broken compiler for some obscure hardware architecture that dosn't optimize away the contents of an "if (0) { ... }" block, you may have better reasons to use char pointer/arrays than reasons not to. Regardless, even on such a craptastic platform, there WILL be reasons not to use char pointer/arrays. They just won't outweigh the reasons to use them.
It's not "some obscure hardware architecture" just because it's not something made by Intel.
Quote:
On of the design goals of C was to work on "some obscure hardware architecture". It's sad that you're splitting straws about the phrasing "there's no reason". It usually (in my experience, I suppose yours may vary) means "given the pros and cons, there's no reason".
Quote:
Original post by Omaha
Added ESPECIALLY if you're going to expose the whole std namespace just to use the string class!
#include <iostream>#include <string>void Foo(){ std::string str("A String"); std::cout << str << std::endl;}This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more