By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

run-time sized array

P: n/a
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 !!


Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, 22 Aug 2003 23:31:58 GMT, "Allen" <al************@att.SPAM.net> wrote:
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.

Jul 19 '05 #2

P: n/a
"Allen" <al************@att.SPAM.net> wrote in message
news:OJ**********************@bgtnsc05-news.ops.worldnet.att.net...
| 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);
Jul 19 '05 #3

P: n/a

"Greg P." <no@spam.sam> wrote in message
news:o1****************@newsread4.news.pas.earthli nk.net...
"Allen" <al************@att.SPAM.net> wrote in message
news:OJ**********************@bgtnsc05-news.ops.worldnet.att.net...
| 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[i] = 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 !!


Jul 19 '05 #4

P: n/a
In article <gt**********************@bgtnsc05-news.ops.worldnet.att.net>,
Allen <al************@att.SPAM.net> wrote:

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

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


If you don't want to use a vector of vectors, then you'll have to use
dynamic memory allocation with 'new'. This is covered in section 16.15 of
the FAQ at <http://www.parashift.com/c++-faq-lite/>.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #5

P: n/a
> 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[i] = 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-lit...tore-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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.512 / Virus Database: 309 - Release Date: 19/08/2003
Jul 19 '05 #6

P: n/a
"Allen" <al************@att.SPAM.net> wrote in message news:<OJ**********************@bgtnsc05-news.ops.worldnet.att.net>...
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;
Jul 19 '05 #7

P: n/a
Allen wrote:

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.