fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. static unsigned o[31];
  7.  
  8. unsigned c(int a){
  9. if (a>='a' && a<='z')
  10. return a-'a';
  11. if (a>='A' && a<='Z')
  12. return a+'a'-'A';
  13. switch (a){
  14. case ' ':return 26;
  15. case '.':return 27;
  16. case ',':return 28;
  17. case '-':return 29;
  18. }
  19. return 30;
  20. }
  21.  
  22. static int _;
  23.  
  24.  
  25. void mur(int j){
  26. if (o[c(j)])
  27. printf("[%c]: %-7d%c",j,o[c(j)],
  28. (_=(_+1)%5)?' ':'\n');
  29. }
  30.  
  31. void hist(const char* a){
  32. _=0;
  33. for (int i=0;i<31;i++)
  34. o[i]=0;
  35. for (int i=strlen(a)-1;i>=0;i--)
  36. o[c(a[i])]++;
  37.  
  38. printf("text:\n%s\n====\n",a);
  39. for (int i=0;i<26;i++)
  40. mur('a'+i);
  41. mur(' ');
  42. mur('.');
  43. mur(' ');
  44. mur('-');
  45. printf("<another>: %d\n\n",o[30]);
  46. }
  47. int main(){
  48. hist("test");
  49. hist("The same issue will be exhibited by any classes/functions by-passing operator new to allocate memory, that is, by performing custom memory allocation followed by calls to the placement new operator. This is for instance typically the case of std::make_shared or std::allocate_shared for which is the solution is to use an aligned allocator as detailed in the solution for STL containers");
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 4540KB
stdin
Standard input is empty
stdout
text:
test
====
[e]: 1       [s]: 1       [t]: 2       <another>: 0

text:
The same issue will be exhibited by any classes/functions by-passing operator new to allocate memory, that is, by performing custom memory allocation followed by calls to the placement new operator. This is for instance typically the case of std::make_shared or std::allocate_shared for which is the solution is to use an aligned allocator as detailed in the solution for STL containers
====
[a]: 28      [b]: 6       [c]: 14      [d]: 9       [e]: 33     
[f]: 7       [g]: 3       [h]: 12      [i]: 22      [k]: 1      
[l]: 22      [m]: 9       [n]: 18      [o]: 30      [p]: 6      
[r]: 16      [s]: 27      [t]: 27      [u]: 6       [w]: 5      
[x]: 1       [y]: 9       [ ]: 59      [.]: 1       [ ]: 59     
[-]: 1       <another>: 7