High strangeness with stringstream

It all started when I, being a poor noob, realized that g++ doesn't support itoa.

Long story short, I set out to write up a little library for myself, starting with an itoa replacement, using the stringstream method. Allow me to include my source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//hr_itoa.cc
//hr_itoa.cc
//itoa replacement

#include <cstdlib>
#include <sstream>
#include <string>
#include "hr_itoa.h"

std::string hr_itoa(int i){
	std::stringstream as;
	as << i;
	return as.str();
}
// hr_itoa.cc 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// hr_itoa.h

#ifndef hr_itoa_h
#define hr_itoa_h
#include <cstdlib>
#include <sstream>
#include <string>

std::string hr_itoa(int i){
	std::stringstream a;
	std::string stri;
}

#endif
// hr_itoa.h 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// tcli.cc - hr_itoa test client

#include <cstdlib>
#include <iostream>
#include <string>

#include "hr_itoa.h"

int main(){
	int numb = 123;
	std::string stri;
	stri = hr_itoa(numb);
	std::cout << stri << std::endl;
	return 0;
}

// hr_itoa test client 


She compiles and seems to link properly, and "runs," but my console output is an assumedly infinite string of whitespace (which piles on so quickly I can't even [Ctrl]-[C] fast enough to see if my "123" string gets output).

Now, for a while there, when I'd comment out the hr_itoa call in tcli.cc, I was getting a (supposedly commonplace) "free() invalid pointer" error and a listing of violated memory addresses and the like. I was, anyhow, but now, commented out or not, std::string stri initialized or not, it doesn't do that any more...

So.. I *have* googled around, and I *have* written a control program to test the core functionality of stringstream on my system:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// wtf.cc

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>


int main(){
	int i = 132;
	std::ostringstream o;
	o << i;
	std::string stri;
	stri = o.str();
	std::cout << stri << std::endl;

	return 0;
}
// wtf.cc 


which compiles and runs precisely as it should...

Needless to say, any help given would be quite welcome.
Also, besides the matter at hand, please feel free to tear apart what little code I've written for any other points of poor usage or c++ etiquette.

Thanking you in advance,

HR, ttoh.

P.S.: I'm writing by hand and compiling with g++. And, the output of "$ g++ -v" :

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
/ --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared 
/--enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext 
/--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu 
/--enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic 
/--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8) 


'm I missing anything?

EDIT: Prettied up the output of "$ g++ -v"
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// hr_itoa.h

#ifndef hr_itoa_h
#define hr_itoa_h
#include <cstdlib>
#include <sstream>
#include <string>

std::string hr_itoa(int i){
	std::stringstream a;
	std::string stri;
}

#endif
// hr_itoa.h  


That is not a mere function declaration, it's a function definition.
Try this instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// hr_itoa.h

#ifndef hr_itoa_h
#define hr_itoa_h
#include <cstdlib>
#include <sstream>
#include <string>

// you can leave the parameter name but it'll be ignored anyway
// std::string hr_itoa(int i);

std::string hr_itoa(int);

#endif
// hr_itoa.h  

This
1
2
3
4
std::string hr_itoa(int i){
	std::stringstream a;
	std::string stri;
}
shouldn't compile. If it compiles it returns something undefined.

Since it's in the header it's supposed to be a prototype and hence should look like this:
std::string hr_itoa(int i);
@Catfish2:

Ah! Somewhere along the lines I got it in my head that the function declaration needed to define the variables (as well as return type and function parameters) in order to ensure enough memory is allocated for the function when it itself is defined. Danke!

@coder777:

That makes sense enough, especially in light of Catfish2's analysis. I'm guessing that's why I got all of that whitespace (or, whatever it was.. didn't take the time to record it and test the actual char values..) piled onto my terminal. Danke auch!

~HR

P.S.: Turns out that I was compiling the whole cat completely wrong too, which probably had a lot to do with why, as coder777 pointed out, it compiled at all. I had skipped compiling everything individually as object files and then linking and compiling the executable. Oh well, works now! ^.^ Thanks to the both you again!
Last edited on
Topic archived. No new replies allowed.