printing bitset to_string().c_str() in template produces junk

Bug #1829415 reported by P Touchman
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc-7 (Ubuntu)
New
Undecided
Unassigned

Bug Description

I am trying to print a bitset with printf using a template, but I keep getting junk on the output.

I can print a bitset with printf("%s",std::bitset<32>(valuetoprint).to_string().to_cstr());

but I thought it would be nice to do that with a template so I could just go:

printf("%s",mybitset<32>(valuetoprint));

template <const int mysize,class mytype> const char * mybitset(mytype myobj){ return std::bitset<mysize>(myobj).to_string().c_str(); }

template <const int mysize> const char * mybitset2(int myobj){ return std::bitset<mysize>(myobj).to_string().c_str(); }

template <const int mysize> const char * mybitset3(int myobj){ return std::bitset<mysize>(myobj).template to_string<char,std::char_traits<char>,std::allocator<char>>().c_str(); }

template <const int mysize> const char * mybitset4(int myobj){ return std::bitset<mysize>(myobj).template to_string<char,std::char_traits<char>,std::allocator<wchar_t>>().c_str(); }

template <const int mysize> std::string mybitset5(int myobj){ return std::bitset<mysize>(myobj).template to_string<char,std::char_traits<char>,std::allocator<char>>(); }

printf("testing bitset32 %s " ,std::bitset<32>(0x255+0xf000).to_string().c_str());
printf("testing bitset16 %s " ,std::bitset<16>(0x255+0xf000).to_string().c_str());
printf("testing mybitset32 %s " ,mybitset<32,int>(0x255+0xf000));
printf("testing mybitset8 %s " ,mybitset<8,int>(0x255+0xf000));
printf("testing mybitset2 16 %s " ,mybitset2<16>(0x255+0xf000));
printf("testing mybitset2 15 %s " ,mybitset2<15>(0x255+0xf000));
printf("testing mybitset2 8 %s " ,mybitset2<8>(0x255+0xf000));
printf("testing mybitset2 16 %s " ,mybitset2<16>(0x255+0xf000));
printf("testing mybitset3 15 %s " ,mybitset3<15>(0x255+0xf000));
printf("testing mybitset3 8 %s " ,mybitset3<8>(0x255+0xf000));
printf("testing mybitset3 16 %s \n",mybitset3<16>(0x255+0xf000));
printf("testing mybitset4 16 %s \n",mybitset4<16>(0x255+0xf000));

std::cout << "mybitset5" << mybitset5<16>(0x255+0xf000) << "\n";

and the output I get is:

testing bitset32 00000000000000001111001001010101 testing bitset16 1111001001010101 testing mybitset32 `w�3V testing mybitset8 01010101 testing mybitset2 16 `w�3V testing mybitset2 15 111001001010101 testing mybitset2 8 01010101 testing mybitset2 16 `w�3V testing mybitset3 15 111001001010101 testing mybitset3 8 01010101 testing mybitset3 16 `w�3V
testing mybitset4 16 `w�3V
mybitset51111001001010101

So my template works fine up until I try to make it print a bitset of size 16. It works fine for a bitset of size 15 but not for 16.

I also tried this compiling with clang and it did the same thing, outputting garbage, working fine for a template with bitset size 15 and garbage for 16.

Revision history for this message
P Touchman (ptouchman) wrote :

I took the code out of the bigger program and made another small program to show the bug:

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

#include <stdio.h>
#include <cstdlib>
#include <bitset>
#include <strings.h>
#include <string.h>
#include <iostream>

template <const int mysize,class mytype> const char * mybitset(mytype myobj){ return std::bitset<mysize>(myobj).to_string().c_str(); }
template <const int mysize> const char * mybitset2(int myobj){ return std::bitset<mysize>(myobj).to_string().c_str(); }

#define bitsetvalue 0x255+0xf000

int main (int argc, char *argv[]) {
printf("COMPILER VERSION %s\n",__VERSION__);
printf("bitset 32 %s \n",std::bitset<32>(bitsetvalue).to_string().c_str());
printf("bitset 16 %s \n",std::bitset<16>(bitsetvalue).to_string().c_str());
printf("bitset 15 %s \n",std::bitset<15>(bitsetvalue).to_string().c_str());
printf("testing mybitset 32 %s \n",mybitset<32,int>(bitsetvalue));
printf("testing mybitset 16 %s \n",mybitset<16,int>(bitsetvalue));
printf("testing mybitset 15 %s \n",mybitset<15,int>(bitsetvalue));
printf("testing mybitset 15 %s \n",mybitset<15>(bitsetvalue));
printf("testing mybitset %s size %ld \n",mybitset<32>(bitsetvalue),strlen(mybitset<32>(bitsetvalue)));
printf("testing mybitset %s size %ld \n",mybitset<32,int>(bitsetvalue),strlen(mybitset<32,int>(bitsetvalue)));
printf("testing mybitset %s size %ld \n",mybitset<4,int>(bitsetvalue),strlen(mybitset<4,int>(bitsetvalue)));
printf("testing mybitset %s size %ld \n",mybitset<8,int>(bitsetvalue),strlen(mybitset<8,int>(bitsetvalue)));
printf("testing mybitset %s size %ld \n",mybitset<12,int>(bitsetvalue),strlen(mybitset<12,int>(bitsetvalue)));
printf("testing mybitset %s size %ld \n",mybitset<15,int>(bitsetvalue),strlen(mybitset<15,int>(bitsetvalue)));
printf("testing mybitset %s size %ld \n",mybitset<16,int>(bitsetvalue),strlen(mybitset<16,int>(bitsetvalue)));
printf("testing mybitset2 16 %s \n",mybitset2<16>(bitsetvalue));
printf("testing mybitset2 15 %s \n",mybitset2<15>(bitsetvalue));

}
=================

and the output gets:

$ g++ mybitsetsimple.cpp;./a.out
COMPILER VERSION 7.4.0
bitset 32 00000000000000001111001001010101
bitset 16 1111001001010101
bitset 15 111001001010101
testing mybitset 32
testing mybitset 16
testing mybitset 15 111001001010101
testing mybitset 15 111001001010101
testing mybitset size 0
testing mybitset size 0
testing mybitset 0101 size 4
testing mybitset 01010101 size 8
testing mybitset 001001010101 size 12
testing mybitset 111001001010101 size 15
testing mybitset size 0
testing mybitset2 16
testing mybitset2 15 111001001010101

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.