Reg: Deletion of char ptr resulting in segmentation fault - Double to string convertion

emailstorbala emailstorbala@gmail.com
Sat Mar 29 03:27:00 GMT 2014


Hi Team,

For one of my project requirement, I tried to write a C++ function (given
below) to convert a double value to a string.

====================

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
	char * buffer = 0;
	double num = 23e45;

	buffer = new char[sizeof(double) + 1];
	if(buffer)
	{
		memset(buffer, '\0', sizeof(double) + 1);
		sprintf(buffer, "%lf", num);
		cout << "buffer ==>" << buffer << endl;
		delete [] buffer;
	}

	return 0;
}

=========================

I tried to compile this code in sun solaris compiler (CC - 32 bit) and got
the code compiled and ran without any error. I tried the same code in my
home 32-bit machine (Fedora) gcc (4.7.2). Compilation produces no errors and
warnings but when running it produces segmentation fault. The same result in
my 64 bit machine (Debian, gcc - 4.7.2).

One more information is that the same code is working fine without any
issues in Sun Solaris (SPARC processor) with gcc 3.4.2 compiler. The same
conversion code works perfectly for int, long and float. For double alone, I
am facing issues. 

Kindly provide me some information on where things goes wrong.

I am trying with the below compile options

g++ -Wall -g -O2 test.cpp -o test

I am getting the below error:

balamurugan@balamurugan:~/Programs/CPP$ ./test
buffer ==>23000000000000000602806197963719130677638070272.000000
*** glibc detected *** ./test: free(): invalid next size (fast):
0x0000000002312010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76d76)[0x7fa3db073d76]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x6c)[0x7fa3db078b1c]
./test[0x400842]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xfd)[0x7fa3db01bead]
./test[0x4008a1]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 10751367                          
/home/balamurugan/Programs/CPP/test
00600000-00601000 rw-p 00000000 08:03 10751367                          
/home/balamurugan/Programs/CPP/test
02312000-02333000 rw-p 00000000 00:00 0                                 
[heap]
7fa3d4000000-7fa3d4021000 rw-p 00000000 00:00 0 
7fa3d4021000-7fa3d8000000 ---p 00000000 00:00 0 
7fa3daffd000-7fa3db17f000 r-xp 00000000 08:03 13108368                  
/lib/x86_64-linux-gnu/libc-2.13.so
7fa3db17f000-7fa3db37e000 ---p 00182000 08:03 13108368                  
/lib/x86_64-linux-gnu/libc-2.13.so
7fa3db37e000-7fa3db382000 r--p 00181000 08:03 13108368                  
/lib/x86_64-linux-gnu/libc-2.13.so
7fa3db382000-7fa3db383000 rw-p 00185000 08:03 13108368                  
/lib/x86_64-linux-gnu/libc-2.13.so
7fa3db383000-7fa3db388000 rw-p 00000000 00:00 0 
7fa3db388000-7fa3db39d000 r-xp 00000000 08:03 13108101                  
/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3db39d000-7fa3db59d000 ---p 00015000 08:03 13108101                  
/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3db59d000-7fa3db59e000 rw-p 00015000 08:03 13108101                  
/lib/x86_64-linux-gnu/libgcc_s.so.1
7fa3db59e000-7fa3db61f000 r-xp 00000000 08:03 13108375                  
/lib/x86_64-linux-gnu/libm-2.13.so
7fa3db61f000-7fa3db81e000 ---p 00081000 08:03 13108375                  
/lib/x86_64-linux-gnu/libm-2.13.so
7fa3db81e000-7fa3db81f000 r--p 00080000 08:03 13108375                  
/lib/x86_64-linux-gnu/libm-2.13.so
7fa3db81f000-7fa3db820000 rw-p 00081000 08:03 13108375                  
/lib/x86_64-linux-gnu/libm-2.13.so
7fa3db820000-7fa3db908000 r-xp 00000000 08:03 19142369                  
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa3db908000-7fa3dbb08000 ---p 000e8000 08:03 19142369                  
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa3dbb08000-7fa3dbb10000 r--p 000e8000 08:03 19142369                  
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa3dbb10000-7fa3dbb12000 rw-p 000f0000 08:03 19142369                  
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7fa3dbb12000-7fa3dbb27000 rw-p 00000000 00:00 0 
7fa3dbb27000-7fa3dbb47000 r-xp 00000000 08:03 13108390                  
/lib/x86_64-linux-gnu/ld-2.13.so
7fa3dbd26000-7fa3dbd2b000 rw-p 00000000 00:00 0 
7fa3dbd43000-7fa3dbd46000 rw-p 00000000 00:00 0 
7fa3dbd46000-7fa3dbd47000 r--p 0001f000 08:03 13108390                  
/lib/x86_64-linux-gnu/ld-2.13.so
7fa3dbd47000-7fa3dbd48000 rw-p 00020000 08:03 13108390                  
/lib/x86_64-linux-gnu/ld-2.13.so
7fa3dbd48000-7fa3dbd49000 rw-p 00000000 00:00 0 
7fff228f2000-7fff22913000 rw-p 00000000 00:00 0                         
[stack]
7fff2292d000-7fff2292e000 r-xp 00000000 00:00 0                         
[vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                 
[vsyscall]
Aborted

Thanks and Regards,
Balamurugan R




--
View this message in context: http://gcc.1065356.n5.nabble.com/Reg-Deletion-of-char-ptr-resulting-in-segmentation-fault-Double-to-string-convertion-tp1022435.html
Sent from the gcc - bugs mailing list archive at Nabble.com.



More information about the Gcc-bugs mailing list