Macro help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • d2klein@gmx.net

    #1

    Macro help

    Hi there,


    i am looking for a macro which replaces a string "string" with its
    character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
    should work for strings of different length.
    Is there any possibility to do this?
    Or is the only solution to write a function which is quite hard since i
    need to allocate memory for the char[] and using a functon makes some
    problems with memory leaks then.


    thanks in advance and greetings

    Axel Klein

  • Nils O. Selåsdal

    #2
    Re: Macro help

    d2klein@gmx.net wrote:
    Hi there,
    >
    >
    i am looking for a macro which replaces a string "string" with its
    character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
    You forgor the null terminator there :-)

    Anyway, what purpose would converting a string literal to a character
    array initialization form serve for you ?
    should work for strings of different length.
    Is there any possibility to do this?
    Using the preprocessor only, on a C string literal ? no.
    Or is the only solution to write a function which is quite hard since i
    need to allocate memory for the char[] and using a functon makes some
    problems with memory leaks then.

    Comment

    • d2klein@gmx.net

      #3
      Re: Macro help

      Nils O. Selåsdal schrieb:
      d2klein@gmx.net wrote:
      Hi there,


      i am looking for a macro which replaces a string "string" with its
      character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
      You forgor the null terminator there :-)
      >
      Anyway, what purpose would converting a string literal to a character
      array initialization form serve for you ?
      >
      The caracter array initialization form is used to allocate memory for
      the string (it is a wide char array on hp-ux).
      After the conversion i call a function to convert the char array to an
      SAP-CHAR array (hpux uses 4byte wchar while SAP-CHAR is just 2 byte;
      needed to communicate with a SAP system). After that the original char
      array should be freed since memory usage would explode if not (every
      literal would be in memory twice once as wchar und once as SAP-CHAR).
      should work for strings of different length.
      Is there any possibility to do this?
      Using the preprocessor only, on a C string literal ? no.
      bad news :(
      Any other idea to do this?
      Or is the only solution to write a function which is quite hard since i
      need to allocate memory for the char[] and using a functon makes some
      problems with memory leaks then.

      Comment

      • Richard

        #4
        Re: Macro help

        d2klein@gmx.net writes:
        Hi there,
        >
        >
        i am looking for a macro which replaces a string "string" with its
        character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
        should work for strings of different length.
        Is there any possibility to do this?
        Or is the only solution to write a function which is quite hard since i
        need to allocate memory for the char[] and using a functon makes some
        problems with memory leaks then.
        Not so much trouble with memory leaks as taking into account the need to
        "escape" certain characters which alters the amount of chars needed for the final
        destination string. But doable. Try it.

        When I say the need to escape, I assume you want it compilable in C.

        Comment

        • d2klein@gmx.net

          #5
          Re: Macro help


          Richard schrieb:
          d2klein@gmx.net writes:
          >
          Hi there,


          i am looking for a macro which replaces a string "string" with its
          character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
          should work for strings of different length.
          Is there any possibility to do this?
          Or is the only solution to write a function which is quite hard since i
          need to allocate memory for the char[] and using a functon makes some
          problems with memory leaks then.
          >
          Not so much trouble with memory leaks as taking into account the need to
          "escape" certain characters which alters the amount of chars needed for the final
          destination string. But doable. Try it.
          >
          I got around 5000 literals to convert. So memory is a quite big issue
          isn't it ?
          When I say the need to escape, I assume you want it compilable in C.
          yeah plain old c nothing more allowed for some reasons

          Comment

          • Eric Sosman

            #6
            Re: Macro help



            d2klein@gmx.net wrote On 08/07/06 08:43,:
            Nils O. Selåsdal schrieb:

            >>d2klein@gmx.n et wrote:
            >>
            >>>Hi there,
            >>>
            >>>
            >>>i am looking for a macro which replaces a string "string" with its
            >>>character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
            >>
            >>You forgor the null terminator there :-)
            >>
            >>Anyway, what purpose would converting a string literal to a character
            >>array initialization form serve for you ?
            >>
            The caracter array initialization form is used to allocate memory for
            the string (it is a wide char array on hp-ux).
            Why not write

            wchar_t string[] = L"string";

            If the "string" part is an ordinary string literal (not a wide
            string literal), generated by macro expansion or something, you
            can convert to wide by concatenating with an empty wide literal:

            wchar_t string[] = L"" "string";
            After the conversion i call a function to convert the char array to an
            SAP-CHAR array (hpux uses 4byte wchar while SAP-CHAR is just 2 byte;
            needed to communicate with a SAP system). After that the original char
            array should be freed since memory usage would explode if not (every
            literal would be in memory twice once as wchar und once as SAP-CHAR).
            Freeing the original character array will accomplish little.
            The only way you can free() it is if you allocated it dynamically
            with malloc() and friends. If you allocate it dynamically you
            cannot use an initializer, so you will need to copy the initial
            contents from somewhere else. You can then free the array, but
            the "somewhere else" cannot be free()d and will still occupy
            space.

            --
            Eric.Sosman@sun .com

            Comment

            • Richard

              #7
              Re: Macro help

              d2klein@gmx.net writes:
              Richard schrieb:
              >
              >d2klein@gmx.net writes:
              >>
              Hi there,
              >
              >
              i am looking for a macro which replaces a string "string" with its
              character initialisation form {'s', 't', 'r', 'i', 'n', 'g'}. this
              should work for strings of different length.
              Is there any possibility to do this?
              Or is the only solution to write a function which is quite hard since i
              need to allocate memory for the char[] and using a functon makes some
              problems with memory leaks then.
              >>
              >Not so much trouble with memory leaks as taking into account the need to
              >"escape" certain characters which alters the amount of chars needed for the final
              >destination string. But doable. Try it.
              >>
              I got around 5000 literals to convert. So memory is a quite big issue
              isn't it ?
              Depends. If you have a machine made in the last 15 years I would say no
              :)

              The max length of your single string will strlen(s)*5+2. I think thats
              right : work it out.

              5000 literals would be about a quarter of a megabyte I suppose. Not
              much. Why do you suppose that is an issue?
              >
              >When I say the need to escape, I assume you want it compilable in C.
              >
              yeah plain old c nothing more allowed for some reasons

              Comment

              • Eric Sosman

                #8
                Re: Macro help



                d2klein@gmx.net wrote On 08/07/06 09:21,:
                >
                I got around 5000 literals to convert. So memory is a quite big issue
                isn't it ?
                Is it?

                How long are these literals? 100 characters each (a couple
                lines of source)? That's 500000 characters. At four bytes per
                wchar_t (your system's value), that's two megabytes. Plus two
                more bytes when each is converted to an SAP-CHAR (as you described
                elsethread), which is another megabyte. There was a time when
                three megabytes was A Lot Of Memory, and there are circumstances
                when it still is -- but you say your program runs under HP-UX,
                which sort of suggests it's not running in a cell phone or a
                Palm Pilot or a toaster oven or something.

                Why bend your program all out of shape to save something
                less than three megabytes? (Or more likely, as I've explained
                elsethread, to fail to save it?)

                --
                Eric.Sosman@sun .com

                Comment

                Working...