473,546 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically allocate array variable of 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
28 7100
Trups wrote:
I want to dynamically allocate array variable of type LPWSTR.
Two things here:
1. LPWSTR is not a standard type but part of the win32 API.
2. LPWSTR is a pointer type, hence the 'P'. In fact it's a typedef for
wchar_t*.
Code looks like this...
main() {
Well, check the FAQ on this one.
LPWSTR *wstr;
int count = Foo (wstr);
You are passing the value of an uninitialised variable to a function. If you
want the function to init the variable, you need to pass its address.
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 ++;
}
This looks as if you wanted to allocate an array of wchar_t strings.
However, I have actually no clue what it is that you really want.
Where should I do delete?
'delete' is a C++ thing, in C you would use malloc() and free().

Sorry, but there is too little to start with. I would suggest you get a good
book or some other tutorial that introduces you step by step to C. If
you're not familiar with programming at all, I would further suggest using
a language with less pitfalls, like e.g. Python.

Uli

Oct 31 '08 #2

"Trups" <Sa***********@ gmail.comwrote in message news:
>
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?
Make sure you genuinely want a pointer to an LPWSTR. The LP is Microsoft's
way of indicating that the type is already a pointer.
However pointers to pointers are often useful. You use them for a list of
strings, for example.

In C arrays decay into pointers when you pass them to functions. Pointers
contain no size information. So you need to pass the buffer size separately.

There is no point passing an uninitialised pointer to a fucntion. C is call
by value, so you are just passing garbage.

I can't determine exactly what you want to do from your code. However it
looks like

int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you want
in your array.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 31 '08 #3
Malcolm McLean wrote:
[...]
int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you want
in your array.
Here's how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It's a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.
Nov 1 '08 #4
blargg said:
Malcolm McLean wrote:
[...]
>int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you
want in your array.

Here's how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It's a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.

You are, of course, correct; in general, for p of any object type, the
template p = malloc(n * sizeof *p) is superior. But Malcolm has been
posting here for several years at least. The probability that he does not
already know this is vanishingly small. I conclude that he disagrees with
the rationale. I have no idea why he would disagree, but Malcolm often has
rather off-the-wall ideas about programming, so it wouldn't surprise me in
the slightest if his views on malloc are strange too.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 1 '08 #5
blargg said:
Malcolm McLean wrote:
[...]
>int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you
want in your array.

Here's how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It's a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.

You are, of course, correct; in general, for p of any object type, the
template p = malloc(n * sizeof *p) is superior. But Malcolm has been
posting here for several years at least. The probability that he does not
already know this is vanishingly small. I conclude that he disagrees with
the rationale. I have no idea why he would disagree, but Malcolm often has
rather off-the-wall ideas about programming, so it wouldn't surprise me in
the slightest if his views on malloc are strange too.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 1 '08 #6
On Oct 31, 9:07 am, Trups <Samant.Tru...@ gmail.comwrote:
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
The general procedure for dynamically allocating an N-element array of
any type T is

T *array = malloc(sizeof *array * numberOfElement s);

That's if you know the number of elements to allocate ahead of time.
If you're allocating the array one element at a time, the procedure is
a little more complicated:

T *array = NULL, *tmp;
size_t arraySize = 0;
int done = 0;

/*
** Continue allocating elements until some condition is true
*/
while (!done)
{
/*
** if realloc cannot extend the array, it will return NULL
*/
tmp = realloc(array, sizeof *array * arraySize + 1);
if (!tmp)
{
/* realloc failed, handle error */
}
else
{
array = tmp;
arraySize++;
}

/* check if done */
}
Nov 2 '08 #7
"Richard Heathfield" <rj*@see.sig.in validwrote in message news
blargg said:
>Malcolm McLean wrote:
[...]
>>int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you
want in your array.

Here's how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It's a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.


You are, of course, correct; in general, for p of any object type, the
template p = malloc(n * sizeof *p) is superior. But Malcolm has been
posting here for several years at least. The probability that he does not
already know this is vanishingly small. I conclude that he disagrees with
the rationale. I have no idea why he would disagree, but Malcolm often has
rather off-the-wall ideas about programming, so it wouldn't surprise me in
the slightest if his views on malloc are strange too.

Firstly, for pedagogical reasons I think the pointer dereference that isn't
really is a source of confusion. It is easier to think of sizeof() being
applied to a type.

My other objection is that the pointer in which the return address of
malloc() is stored can have a rather long identifier. Here it is only
ptr[i], but it can easily by

employees->bonuslist[employees->Nlists];
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 2 '08 #8
In article <9f************ *************** ***@bt.com>,
"Malcolm McLean" <re*******@btin ternet.comwrote :
"Richard Heathfield" <rj*@see.sig.in validwrote in message news
blargg said:
Malcolm McLean wrote:
[...]
int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you
want in your array.

Here's how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It's a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.
You are, of course, correct; in general, for p of any object type, the
template p = malloc(n * sizeof *p) is superior. But Malcolm has been
posting here for several years at least. The probability that he does not
already know this is vanishingly small. I conclude that he disagrees with
the rationale. I have no idea why he would disagree, but Malcolm often has
rather off-the-wall ideas about programming, so it wouldn't surprise me in
the slightest if his views on malloc are strange too.

Firstly, for pedagogical reasons I think the pointer dereference that isn't
really is a source of confusion. It is easier to think of sizeof() being
applied to a type.
I somewhat agree. On the other hand, by avoiding using sizeof object
early on, the student isn't given an opportunity to become familiar.
My other objection is that the pointer in which the return address of
malloc() is stored can have a rather long identifier. Here it is only
ptr[i], but it can easily by

employees->bonuslist[employees->Nlists];
employees->bonuslist[employees->Nlists] =
malloc( sizeof *employees->bonuslist[0] );
Nov 2 '08 #9
"blargg" <bl********@gis hpuppy.comwrote in message news:
In article <9f************ *************** ***@bt.com>,
"Malcolm McLean" <re*******@btin ternet.comwrote :
>"Richard Heathfield" <rj*@see.sig.in validwrote in message news
blargg said:

Malcolm McLean wrote:
[...]
int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++ )
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them
you
want in your array.

Here's how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It's a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.

You are, of course, correct; in general, for p of any object type, the
template p = malloc(n * sizeof *p) is superior. But Malcolm has been
posting here for several years at least. The probability that he does
not
already know this is vanishingly small. I conclude that he disagrees
with
the rationale. I have no idea why he would disagree, but Malcolm often
has
rather off-the-wall ideas about programming, so it wouldn't surprise me
in
the slightest if his views on malloc are strange too.

Firstly, for pedagogical reasons I think the pointer dereference that
isn't
really is a source of confusion. It is easier to think of sizeof() being
applied to a type.

I somewhat agree. On the other hand, by avoiding using sizeof object
early on, the student isn't given an opportunity to become familiar.
>My other objection is that the pointer in which the return address of
malloc() is stored can have a rather long identifier. Here it is only
ptr[i], but it can easily by

employees->bonuslist[employees->Nlists];

employees->bonuslist[employees->Nlists] =
malloc( sizeof *employees->bonuslist[0] );
That's not a bad idea.
Now you're breaking the symmetry that the sizeof the object that is pointed
to by the return of malloc() is taken.
In fact you'd need more than one of them. So make that

malloc(employee s->Nbonuses * sizeof *employees->bonuslist[0]);

and of course the asterisk is being used in two different contexts.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 2 '08 #10

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

Similar topics

4
3098
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
2557
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
5585
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
9785
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...
2
3430
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
3757
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
2819
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
8712
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
3
6893
by: Samant.Trupti | 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++) //print each element;
0
7694
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. ...
0
7947
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...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
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...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
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...
0
3491
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
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...

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.