Connecting Tech Pros Worldwide Help | Site Map

dynmaic allocation

Jung, William
Guest
 
Posts: n/a
#1: Nov 19 '08
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)?
maverik
Guest
 
Posts: n/a
#2: Nov 19 '08

re: dynmaic allocation


On Nov 19, 12:29 pm, "Jung, William" <aopiyy...@yahoo.comwrote:
Quote:
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
Guest
 
Posts: n/a
#3: Nov 19 '08

re: dynmaic allocation


On Nov 19, 1:45*pm, maverik <maverik.m...@gmail.comwrote:
Quote:
* * BYTE binary = new BYTE[n];
I mean BYTE *binary = new BYTE[n];
Quote:
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
Guest
 
Posts: n/a
#4: Nov 19 '08

re: dynmaic allocation


"Jung, William" <aopiyy001@yahoo.comkirjutas:
Quote:
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.
Quote:
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<BYTEbuffer(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
Guest
 
Posts: n/a
#5: Nov 20 '08

re: dynmaic allocation


On Nov 19, 10:29 am, "Jung, William" <aopiyy...@yahoo.comwrote:
Quote:
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
Quote:
StringtoBinary( LPCSTR string, BYTE *binary)
Quote:
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
Closed Thread