Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > run-time sized array

Reply
Thread Tools

run-time sized array

 
 
Allen
Guest
Posts: n/a
 
      08-22-2003
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 !!




 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-22-2003
On Fri, 22 Aug 2003 23:31:58 GMT, "Allen" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Greg P.
Guest
Posts: n/a
 
      08-22-2003
"Allen" <(E-Mail Removed)> wrote in message
news:OJx1b.109648$(E-Mail Removed)...
| 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);


 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      08-23-2003

"Greg P." <(E-Mail Removed)> wrote in message
news1y1b.3347$(E-Mail Removed) nk.net...
> "Allen" <(E-Mail Removed)> wrote in message
> news:OJx1b.109648$(E-Mail Removed)...
> | 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 !!




 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-23-2003
> 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


 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      08-23-2003
In article <gty1b.109704$(E-Mail Removed)>,
Allen <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-23-2003
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.

 
Reply With Quote
 
J. Campbell
Guest
Posts: n/a
 
      08-23-2003
"Allen" <(E-Mail Removed)> wrote in message news:<OJx1b.109648$(E-Mail Removed)>...
> 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;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct with dynamic sized array member joe C++ 1 08-31-2011 09:02 AM
List as a dynamic array of increasing-sized arrays MartinBroadhurst C Programming 13 11-03-2010 12:05 PM
Sized array w/initializer Leo Havm°ller C Programming 4 04-22-2010 06:10 PM
Dynamic sized array? Daniel T. C++ 4 10-17-2006 12:19 PM
Oddly sized sized password textbox thechaosengine ASP .Net 1 09-15-2005 07:34 PM



Advertisments