Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

HP C++ conversion to gnu g++: uses stringvar.shrink stringvar.dump

Posted on 2011-02-22
Last Modified: 2012-05-11
I'm converting C++ code written on an HP system to a Linux system using GNU G++
Sample code (not in entirety) follows:
        char LocationName[MAX_NAME_LENGTH];
        String Name;
        Name="                     ";

        while (test == FALSE) {
                cout << "Enter the Location name: ";
                cin >> Name;

                cout << "Do you wish to accept this location name (y,n) : ";
                if (accept == 'y') test=TRUE;

Open in new window

Where can I find information on shrink and dump (online)?
Are they HP specific?
How might I convert that part of the code to g++, if not part of g++?

(p.s. shrink and dump are very hard to successfully Google for.)

Appreciative of any helpful responses.

Question by:madmax61
  • 6
  • 2
  • 2
LVL 11

Assisted Solution

cup earned 100 total points
ID: 34958649
name is a String not a std::string.  That is either project specific or something from HP.  Check in your code whether you can find the definition of String.

Author Comment

ID: 34965399
I found String.h at /opt/aCC/include/SC/String.h

Inside String.h the references to shrink and dump were:

        char* dump(char* s) const {
                s[d->len]='\0'; return s;

Open in new window


void shrink(int newlen);

Open in new window

and I also noticed some more problematic code (of the code I'm porting to Linux), a reference to int_to_str() inside this HP header;

friend String int_to_str(int);

Open in new window

So, dump and shrink (and also int_to_str) are not standard C++, that is, they are specific to the HP C++ (aC++ to me more precise) compiler?

In laymans terms what is the dump code above doing?

What is shrink likely to be doing? Only reducing the length of a string? Trimming whitespace? Allowing someone to enter input of 20 characters say, but only assigning the first, say, 15 to the string?



Author Comment

ID: 34965441
Is dump some sort of string to char conversion?

p.s. 'm not very experienced with C++ so I'm not all that familiar with the difference between string and char.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Author Comment

ID: 34965659
As it turns out I can do a "man String" on HP and it tells me :

    Length adjustment
      void shrink(int n);
           If this String is longer that n, truncate it to that length.
           Otherwise leave the String unchanged.  Returns non-zero if the
           String is non-empty.  Runs in O(1).

   Conversion to char*
      The following functions provide an interface to C-style functions that

                                    - 6 -      Formatted:  February 24, 2011

 String(3C++)                                                   String(3C++)

      expect pointers to null-terminated character arrays.

      operator const char*()const;
           If length() is N, this operator works by stuffing a null
           character into cell N+1 and returning a pointer to the first
           cell.  Runs in O(length()) when space must be acquired to make
           room for the extra cell (worst case), and O(1) otherwise.

      char* dump(char* p)const;
           Copies the characters of this String into the character array
           pointed to by p, adding a null character at the end.  The array
           is assumed to be at least length()+1 characters in length, but
           this requirement is not checked for.  Runs in O(length()).


      String - variable-length character strings

      class istream;           // see iostream(3C++)
      class ostream;           // see iostream(3C++)

      //  Auxiliary types

      class Substring;
      class Tmpstring;

      class Stringsize{
          Stringsize(unsigned n);
          unsigned size()const;

      class String{

      //  Constructors, destructor

          String(const char* p);
          String(const char* p,unsigned n);
          String(char c);
          String(const Substring& s);
          String(const Tmpstring& s);
          String(Stringsize n);

      //  Conversion to char*

          operator const char*()const;
          char* dump(char* p)const;

Open in new window

Can I simply substitute any mention of "String" on HP with "string" in G++?
LVL 11

Expert Comment

ID: 34967615
You could try substituting String with std::string.  

Dump is just a string assignment.  Something like

String xxx;
xxx.dump ("fred");

can be replaced with

std::string xxx;
xxx = "fred";

Looks like shrink is just some form of truncation.  std::string takes care of that anyway so just comment out those statements.
LVL 33

Assisted Solution

sarabande earned 400 total points
ID: 34970109
dump is no string assignment. it writes the contents of String into char buffer passed as pointer to char (what is unsafe if no size is given).

if the String was widely used in the app i would implement new String class which has a std::string as member and provides existing interface.


Author Comment

ID: 34976599
Current status is I have 4 issues, which are all HP specific fromthe String.h header:

1. the String class (as opposed to std::string)
2. dump (as in stringvar.dump(charvar))
3. shrink (as in stringvar.shrink(num))
4. int_to_str (integer to string conversion)

Current solutions:

1. just use "string"
2. use c_str (http://www.cplusplus.com/reference/string/string/c_str/)
3. use resize (http://www.cplusplus.com/reference/string/string/resize/)
4. use this template to convert int to string(flexible solution which works on floats or doubles as well) (src : http://bytes.com/topic/c/answers/131431-how-do-simple-loop-store-strings )

template <class T>
inline std::string to_string (const T& t)
std::stringstream ss;
ss << t;
return ss.str();

Open in new window

(Also found this page useful:

I think I can manage to resolve most of my problems now with the above:

I have one remaining problem concerning resolving these "Strings".
Essentially the program takes textual input from the keyboard as a string and converts it to char, and uses these to char variables to pass into other routines, so I don't want to mess with how they are defined too much as my C++ understanding is fairly limited.

However, the char variables (which are full path filenames) are declared as, for example:
char filename[80];

but to use the c_str the filename (I believe) must be declared as:
const char * filename;

So if I go changing the many references to these filename character variables to const char * I'm not sure what effect it will have.


Author Comment

ID: 34976761
P.S. Thankyou for contributing so far Sara and cup.
LVL 33

Accepted Solution

sarabande earned 400 total points
ID: 34982094
c_str and dump is not the same.

you can add global function strdump like

  char * strdump(const std::string & str, char * ps, int siz)
      if (ps == NULL || siz <= 0)
           return NULL;
      if (siz > (int)str.length())
          siz = (int)str.length()+1;

      strncpy(ps, str.c_str(), siz-1);
      ps[siz] = '\0';
      return ps;

for converting int to str you could use stringstream

   std::ostringstream oss;
   oss << i;
   std::string str = oss.str();


Author Comment

ID: 35022376
Thankyou again to both of you, apologies for not accepting as yet, but I've been busy with other parts of the program as well. For example, not only did it use something called String rather than string it also used something called Time instead of time, which puts time related information into String I think.

I've not gone ahead and implemented the above suggested changes.  Your strdump will probably help me avoid other changes I was going to need to make to accommodate c_str.

I'll assign some points now.


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question