dynmaic allocation

Discussion in 'C++' started by Jung, William, Nov 19, 2008.

  1. I have a function that convert a string to binary, where
    - string is the string needs to convert to binary.
    - binary is the BYTE array to hold the converted data

    StringtoBinary( LPCSTR string, BYTE *binary)

    Since BYTE is a pointer to BYTE, do I need to use new operator to
    allocate space / storage (dynmaic allocation)?
     
    Jung, William, Nov 19, 2008
    #1
    1. Advertising

  2. Jung, William

    maverik Guest

    On Nov 19, 12:29 pm, "Jung, William" <> wrote:
    > I have a function that convert a string to binary, where
    > - string is the string needs to convert to binary.
    > - binary is the BYTE array to hold the converted data
    >
    > StringtoBinary( LPCSTR string, BYTE *binary)
    >
    > Since BYTE is a pointer to BYTE, do I need to use new operator to
    > allocate space / storage (dynmaic allocation)?


    It depends. You can do it in several ways:

    void
    foo() {
    BYTE binary[1234];
    std::string str = "comp.lang.c++";

    /* ... */
    StringtoBinary(str, binary);
    /* ... */
    }

    or if you don't know size of array

    void
    foo() {
    std::string str = "comp.lang.c++";
    BYTE binary = new BYTE[n];

    /* ... */
    StringtoBinary(str, binary);
    /* ... */

    delete[] binary;
    }

    or do it inside your function:

    StringtoBinary(std::string str, BYTE *binary) {
    binary = new BYTE[n];

    /* ... */
    }

    but I think it's not a good idea.
     
    maverik, Nov 19, 2008
    #2
    1. Advertising

  3. Jung, William

    maverik Guest

    On Nov 19, 1:45 pm, maverik <> wrote:

    >     BYTE binary = new BYTE[n];


    I mean BYTE *binary = new BYTE[n];

    > StringtoBinary(std::string str, BYTE *binary) {
    >     binary = new BYTE[n];
    >
    >     /* ... */
    >
    > }
    >
    > but I think it's not a good idea.


    Because in this case you cannot use binary outside of StringtoBinary
    (). If you really need this you should give a pointer-to-pointer as an
    argument:


    StringtoBinary(std::string str, BYTE **binary) {
    *binary = new BYTE[n];

    /* ... */
    }
     
    maverik, Nov 19, 2008
    #3
  4. Jung, William

    James Kanze Guest

    On Nov 19, 10:29 am, "Jung, William" <> wrote:
    > I have a function that convert a string to binary, where
    > - string is the string needs to convert to binary.
    > - binary is the BYTE array to hold the converted data


    > StringtoBinary( LPCSTR string, BYTE *binary)


    > Since BYTE is a pointer to BYTE, do I need to use new operator
    > to allocate space / storage (dynmaic allocation)?


    If you do, there's no way you can pass it back to the user.

    But this interface is completely broken anyway, and can't be
    made to work. The simplest would be to change it to something
    like:
    std::vector< BYTE > StringToBinary( std::string string ) ;
    ..

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 20, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hubble

    Auto allocation of Indexes

    Hubble, Sep 6, 2005, in forum: VHDL
    Replies:
    8
    Views:
    582
    Charles, SAG
    Sep 8, 2005
  2. Elhanan
    Replies:
    0
    Views:
    463
    Elhanan
    May 17, 2006
  3. Ken
    Replies:
    24
    Views:
    3,874
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    991
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,191
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page