By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,878 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

dynmaic allocation

P: n/a
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)?
Nov 19 '08 #1
Share this Question
Share on Google+
4 Replies


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

P: n/a
On Nov 19, 1:45*pm, maverik <maverik.m...@gmail.comwrote:
* * 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];

/* ... */
}
Nov 19 '08 #3

P: n/a
"Jung, William" <ao*******@yahoo.comkirjutas:
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<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
Nov 19 '08 #4

P: n/a
On Nov 19, 10:29 am, "Jung, William" <aopiyy...@yahoo.comwrote:
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:ja*********@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
Nov 20 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.