[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

dynmaic allocation

Jung, William

11/19/2008 9:29:00 AM

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)?
6 Answers

Jeff L

4/1/2008 9:53:00 PM

0

On Apr 1, 2:28 pm, capricious <pokers...@beer.com> wrote:
> You stated on March 30th that you would not be returning to ARS.
>
> Footbullet?


No, Tom never stated that. Someone was spoofing email addresses and
posting to the list as Tom.

Tom posts using Google Groups, with Firefox, under Linux. The "Tom N"
who claimed he wasn't posting anymore posted using Bellsouth, with
Thunderbird 2.0.0.9, under Windows.

Different people.

AnonymousNasty

4/2/2008 11:19:00 AM

0

On Apr 1, 2:49 pm, Tom N <simpleman....@gmail.com> wrote:
> No thank you.
> If you think your troll shit is going to make me go away, think again.
>
> Tom Newton

The troll has manners he said no thank you. LMAO

maverik

11/19/2008 10:45:00 AM

0

On Nov 19, 12:29 pm, "Jung, William" <aopiyy...@yahoo.com> 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

11/19/2008 10:51:00 AM

0

On Nov 19, 1:45 pm, maverik <maverik.m...@gmail.com> 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];

/* ... */
}

Paavo Helde

11/19/2008 9:08:00 PM

0

"Jung, William" <aopiyy001@yahoo.com> kirjutas:

> 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,

No, BYTE is BYTE, "binary" is a pointer to a BYTE, and most probably to
the first one in the array of BYTE-s the function expects.

> do I need to use new operator to
> allocate space / storage (dynmaic allocation)?

No, you do not. You have to pass a pointer to a buffer large enough. How
the buffer is allocated should not be a concern.

StringtoBinary() has to document somehow large buffer it expects. Then
you prepare this beforehand:

std::vector<BYTE> buffer(needed_size_in_BYTEs);
StringtoBinary(mystring, &buffer[0]);

In case of conversions it might often happen that the function does not
fill the whole buffer and reports back instead how much it actually used.
Then you can resize the buffer to reflect this:

buffer.resize(actually_used_size_in_BYTEs);

If you have control over StringtoBinary() implementation, you can make it
more like C++. I mean, in C++ the caller should not deal with such
details as buffer allocation and resizing.

hth
Paavo


James Kanze

11/20/2008 8:34:00 AM

0

On Nov 19, 10:29 am, "Jung, William" <aopiyy...@yahoo.com> 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:james.kanze@gmail.com
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