run-time sized array

A

Allen

Hi all,

I have an array that I don't know the size requirement until run-time.
The way I handle this is:

int* piMyData;

piMyData = (int*)LocalAlloc(LPTR, sizeof(int)*iDataCount); //MS-specific


I also have a 2D char array that I don't know the size requirement until
run-time. Is there a similar method for handling this?
--

Best wishes,
Allen

No SPAM in my email !!
 
A

Alf P. Steinbach

Hi all,

I have an array that I don't know the size requirement until run-time.
The way I handle this is:

int* piMyData;

piMyData = (int*)LocalAlloc(LPTR, sizeof(int)*iDataCount); //MS-specific


std::vector<int> v( dataCount );

v[someIndex] = someValue;

I also have a 2D char array that I don't know the size requirement until
run-time. Is there a similar method for handling this?

Use a vector of vectors.
 
G

Greg P.

| I have an array that I don't know the size requirement until run-time.
| The way I handle this is:
|
| int* piMyData;
|
| piMyData = (int*)LocalAlloc(LPTR, sizeof(int)*iDataCount);
//MS-specific

Remember that you are posting to a "C++" newsgroup, not "C":
piMyData = static_cast<int*>(LocalAlloc(LPTR, sizeof(int)*iDataCount));

| I also have a 2D char array that I don't know the size requirement
until
| run-time. Is there a similar method for handling this?

Do you mean similar in terms of using native win32 (non standard) methods?
That is off topic here. There is a simple way that many novices use to
figure out the total size of any array (though it is frowned upon):

size_t size = sizeof(array[0][0]) * sizeof(array);
 
A

Allen

Greg P. said:
| I have an array that I don't know the size requirement until run-time.
| The way I handle this is:
|
| int* piMyData;
|
| piMyData = (int*)LocalAlloc(LPTR, sizeof(int)*iDataCount);
//MS-specific

Remember that you are posting to a "C++" newsgroup, not "C":
piMyData = static_cast<int*>(LocalAlloc(LPTR, sizeof(int)*iDataCount));

| I also have a 2D char array that I don't know the size requirement
until
| run-time. Is there a similar method for handling this?

Do you mean similar in terms of using native win32 (non standard) methods?
That is off topic here. There is a simple way that many novices use to
figure out the total size of any array (though it is frowned upon):

size_t size = sizeof(array[0][0]) * sizeof(array);

Hi Greg,

No, I'm not trying to determine the size. Also, I'm using a very old
compiler that doesn't have vectors and besides, I don't have any experience
using the STL.

I'm looking for a method similar to the first one I gave to create
storage for a 2D array that I don't know the size of until run-time (at
which time, I do).
In the first example, I get iDataCount and create an array at piMyData.
Then, I can:

piMyData = iSomeInt;

I want to get iXCount and iYCount and create a 2D array that I can
access:

pszMyData[x][y] = "a";
--

Best wishes,
Allen

No SPAM in my email !!
 
J

John Harrison

Hi Greg,
No, I'm not trying to determine the size. Also, I'm using a very old
compiler that doesn't have vectors and besides, I don't have any experience
using the STL.

I'm looking for a method similar to the first one I gave to create
storage for a 2D array that I don't know the size of until run-time (at
which time, I do).
In the first example, I get iDataCount and create an array at piMyData.
Then, I can:

piMyData = iSomeInt;

I want to get iXCount and iYCount and create a 2D array that I can
access:

pszMyData[x][y] = "a";


This is in the FAQ.

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html

question 16.15. You should be able to translate the use of new to
LocalAlloc, or better still drop LocalAlloc and use new instead. Every C++
compiler ever invented must have new, surely.

john
 
K

Kevin Goodsell

Allen said:
No, I'm not trying to determine the size. Also, I'm using a very old
compiler that doesn't have vectors

Sounds like a good time to get a new compiler.
and besides, I don't have any experience
using the STL.

Sounds like a good time to learn.

Ask in a C++ group, get a C++ answer. Use vector. It's infinitely
superior to anything you are likely to come up with.

-Kevin
 
J

J. Campbell

Allen said:
Hi all,

I have an array that I don't know the size requirement until run-time.
The way I handle this is:

int* piMyData;

piMyData = (int*)LocalAlloc(LPTR, sizeof(int)*iDataCount); //MS-specific


Why not use:

{
int* piMyData;
int size_piMyData = my_array_has_this_many_elements;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top