473,887 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically allocate array variable type LPWSTR

HI,

I want to dynamically allocate array variable of type LPWSTR.
Code looks like this...

main() {
LPWSTR *wstr;
int count = Foo (wstr);
for (int i = 0; i < count; i++)
//print each element;
}

int Foo(LPWSTR *wstr)
{
int count = 0;
while (!done){
//Here I need to allocate "wstr" one element by one element. How
to do that?
// I don't know the count
count ++;
}

Where should I do delete?

Thanks
Trupti
Oct 31 '08 #1
3 6910
Sa***********@g mail.com wrote:
HI,

I want to dynamically allocate array variable of type LPWSTR.
Code looks like this...

main() {
int main() {
LPWSTR *wstr;
I recommend initialising all pointers to 0.
int count = Foo (wstr);
for (int i = 0; i < count; i++)
//print each element;
}

int Foo(LPWSTR *wstr)
Whatever you allocate here will probably change 'wstr'. The problem is,
the caller of 'Foo' will not know of those changes. If you expect the
caller to access the array, you need to make sure the changes are also
transferred back to the caller. For that pass 'wstr' either by
reference or by pointer:

int Foo(LPWSTR * &wstr)
{
int count = 0;
while (!done){
//Here I need to allocate "wstr" one element by one element. How
to do that?
What does it mean "one element by one element"? Is it supposed to grow
somehow? Is it conditional? Based on what?
// I don't know the count
count ++;
Huh? Where is the return statement?
}

Where should I do delete?
Whoever *owns* the allocated array should dispose of it at the point
they don't need it any longer. I would say, the 'main' seems to get the
ownership once 'Foo' is done allocating, so 'delete[]' should be in the
'main' function (as written, at least).

--------------------------- with all those things in mind, you're
probably much better off using a vector of LPWSTR:

#include <vector>

void Foo(std::vector <LPWSTR>& wstr);

int main()
{
std::vector<LPW STRwstr;
Foo(wstr);
... // no need to delete anything, 'std::vector'
// takes care of its own storage. And it has
// '.size()' too, so you don't need to keep
// the count, it does it for you.
}

void Foo(std::vector <LPWSTR>& wstr)
{
while (!done)
{
...
wstr.push_back( /* some new LPWSTR value */ );
}
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '08 #2
On Oct 31, 8:36*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Samant.Tru...@g mail.com wrote:
HI,
* I want to dynamically allocate array variable of type LPWSTR.
Code looks like this...
main() {

int main() {
* *LPWSTR **wstr;

I recommend initialising all pointers to 0.
* *int count = Foo (wstr);
* *for (int i = 0; i < count; i++)
* * * //print each element;
}
int Foo(LPWSTR *wstr)

Whatever you allocate here will probably change 'wstr'. *The problem is,
the caller of 'Foo' will not know of those changes. *If you expect the
caller to access the array, you need to make sure the changes are also
transferred back to the caller. *For that pass 'wstr' either by
reference or by pointer:

* * int Foo(LPWSTR * &wstr)
{
* *int count = 0;
* *while (!done){
* * *//Here I need to allocate "wstr" one element by one element.*How
to do that?

What does it mean "one element by one element"? *Is it supposed to grow
somehow? *Is it conditional? *Based on what?
* * // I don't know the count
* *count ++;

Huh? *Where is the return statement?
* }
Where should I do delete?

Whoever *owns* the allocated array should dispose of it at the point
they don't need it any longer. *I would say, the 'main' seems to get the
ownership once 'Foo' is done allocating, so 'delete[]' should be in the
'main' function (as written, at least).

--------------------------- with all those things in mind, you're
probably much better off using a vector of LPWSTR:

#include <vector>

void Foo(std::vector <LPWSTR>& wstr);

int main()
{
* * *std::vector<LP WSTRwstr;
* * *Foo(wstr);
* * *... * * * * * // no need to delete anything, 'std::vector'
* * * * * * * * * *// takes care of its own storage. *And it has
* * * * * * * * * *// '.size()' too, so you don't need to keep
* * * * * * * * * *// the count, it does it for you.

}

void Foo(std::vector <LPWSTR>& wstr)
{
* * *while (!done)
* * *{
* * * * *...
* * * * *wstr.push_back ( /* some new LPWSTR value */ );
* * *}

}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Hmmm Ok I will use vectior that seems very simple. Thank you.

But just for my Knowledge
If I do not know the "count" in Foo to allocate the number of elements
in arrary at the beginning
I need to do realloc everytime, right?
Trupti
Oct 31 '08 #3
Sa***********@g mail.com wrote:
[..]
If I do not know the "count" in Foo to allocate the number of elements
in arrary at the beginning
I need to do realloc everytime, right?
No, not every time. Nobody tells you to allocate exactly the number of
elements you need. You just need to remember how many you allocated and
how many you are currently using. That's what 'std::vector' does behind
the scenes, essentially. It does reallocate when it grows, but not on
every 'push_back'. And you can prevent it from reallocating if you tell
it to 'reserve' as many as you possibly will ever use (although it is
not necessarily known or easy to calculate).

RTFM about 'std::vector', and better if you actually get a decent book
for it, like "The C++ Standard Library: a Tutorial and a Reference" by
Josuttis.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3116
by: Ovid | last post by:
Hi all, I'm having a problem trying to create a 2D array whose dimensions are determined at runtime. Below my signoff is a minimal test case that hopefully demonstrates what I'm trying to do. Unfortunately, this segfaults. The output is the following: $ gcc -Wall -c arrays.c $ gcc -o arrays arrays.o $ ./arrays
10
2611
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr = malloc(sizeof(*arr) * 4); /* I want to dynamically allocate
5
5602
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
5
9870
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy the memory across. Using Marshal.PtrStructure doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it). ...
2
3455
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You can not use the row 0, and the column 0.
11
3791
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
10
2848
by: The Cool Giraffe | last post by:
I got a hint recently (guess where, hehe) and was directly pointed to the vector class. Now, i have no issues using that way but i'm concerned about the performance issue. Which is fastest (significantly faster, preferable in any way, etc.) in the following list. a) std::vector? b) a struct with an array and an integer?
7
8738
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a two-dimensional array, like this: int x; but I want it dynamically-allocated, and I want expressions that refer
28
7182
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11173
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.