473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dynmaic allocation

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
4 2174
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: kwijibo28 | last post by:
Hi all, I've got a simple question regarding stl containers. Consider this code: std::vector<float> foo; foo.resize(100); How do I know if memory allocation was successful? The resize...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
158
by: jacob navia | last post by:
1: It is not possible to check EVERY malloc result within complex software. 2: The reasonable solution (use a garbage collector) is not possible for whatever reasons. 3: A solution like the...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.