Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynmaic allocation

Reply
Thread Tools

dynmaic allocation

 
 
Jung, William
Guest
Posts: n/a
 
      11-19-2008
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)?
 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      11-19-2008
On Nov 19, 12:29 pm, "Jung, William" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
maverik
Guest
Posts: n/a
 
      11-19-2008
On Nov 19, 1:45*pm, maverik <(E-Mail Removed)> 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];

/* ... */
}
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-20-2008
On Nov 19, 10:29 am, "Jung, William" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
dynmaic recursive nesting in xsl? Elhanan XML 0 05-17-2006 05:14 PM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Auto allocation of Indexes Hubble VHDL 8 09-08-2005 05:02 PM



Advertisments