Register | Sign In


Understanding through Discussion


EvC Forum active members: 65 (9164 total)
4 online now:
Newest Member: ChatGPT
Post Volume: Total: 916,907 Year: 4,164/9,624 Month: 1,035/974 Week: 362/286 Day: 5/13 Hour: 0/2


Thread  Details

Email This Thread
Newer Topic | Older Topic
  
Author Topic:   Hey c++ people, I need help
coffee_addict
Member (Idle past 507 days)
Posts: 3645
From: Indianapolis, IN
Joined: 03-29-2004


Message 1 of 9 (200798)
04-20-2005 10:24 PM


How do I create a very large array (parameter probably around 2 million)? Would making a vector help?

Replies to this message:
 Message 2 by arachnophilia, posted 04-20-2005 10:43 PM coffee_addict has not replied
 Message 3 by Asgara, posted 04-20-2005 11:01 PM coffee_addict has not replied
 Message 4 by jar, posted 04-20-2005 11:21 PM coffee_addict has not replied
 Message 5 by PaulK, posted 04-21-2005 3:00 AM coffee_addict has not replied
 Message 6 by Dr Jack, posted 04-21-2005 4:30 AM coffee_addict has not replied
 Message 7 by Percy, posted 04-21-2005 2:53 PM coffee_addict has replied

  
arachnophilia
Member (Idle past 1373 days)
Posts: 9069
From: god's waiting room
Joined: 05-21-2004


Message 2 of 9 (200810)
04-20-2005 10:43 PM
Reply to: Message 1 by coffee_addict
04-20-2005 10:24 PM


very large array
a very large grant from nasa.

This message is a reply to:
 Message 1 by coffee_addict, posted 04-20-2005 10:24 PM coffee_addict has not replied

  
Asgara
Member (Idle past 2332 days)
Posts: 1783
From: Wisconsin, USA
Joined: 05-10-2003


Message 3 of 9 (200816)
04-20-2005 11:01 PM
Reply to: Message 1 by coffee_addict
04-20-2005 10:24 PM


NOT a C++ person ( more of a C- - )
Codeguru
Vector would be used for dynamic array. If you don't know the exact parameter I would say that yes a vector is called for.
I am NOT a C++ person, but I have read that HUGE arrays have problems unless you use some kind of dynamic allocation.
Page not found - CUJ Loans

Asgara
"Embrace the pain, spank your inner moppet, whatever....but get over it"
select * from USERS where CLUE > 0
http://asgarasworld.bravepages.com
http://perditionsgate.bravepages.com

This message is a reply to:
 Message 1 by coffee_addict, posted 04-20-2005 10:24 PM coffee_addict has not replied

  
jar
Member (Idle past 424 days)
Posts: 34026
From: Texas!!
Joined: 04-20-2004


Message 4 of 9 (200823)
04-20-2005 11:21 PM
Reply to: Message 1 by coffee_addict
04-20-2005 10:24 PM


What are you going to do with this array? Is it transient or permanent, one unit or fields?

Aslan is not a Tame Lion

This message is a reply to:
 Message 1 by coffee_addict, posted 04-20-2005 10:24 PM coffee_addict has not replied

  
PaulK
Member
Posts: 17828
Joined: 01-10-2003
Member Rating: 2.3


Message 5 of 9 (200868)
04-21-2005 3:00 AM
Reply to: Message 1 by coffee_addict
04-20-2005 10:24 PM


I'm not really a C++ person (yet) but it looks to me as if the potential problems are going to be with the implementation and not with the language itself.
My advice is:
1) Work out how much memory you are going to need (check the sixe of the individual elements - an integer is going to be 4 bytes on almost any system so 2,000,000 integers comes to 8MB). Don't forget to allow for all the rest of your variables.
2) Check the compiler documentation and release notes for limits on memory usage. That's the only place you are likely to find it.

This message is a reply to:
 Message 1 by coffee_addict, posted 04-20-2005 10:24 PM coffee_addict has not replied

  
Dr Jack
Member
Posts: 3514
From: Immigrant in the land of Deutsch
Joined: 07-14-2003
Member Rating: 8.3


Message 6 of 9 (200875)
04-21-2005 4:30 AM
Reply to: Message 1 by coffee_addict
04-20-2005 10:24 PM


You could create it the same way you create any other array... although personally I'd be more inclined to use std:eque - std::vector won't help as the standard effectively guarantees it to be a continuous block of memory, so it's no better than a C array in terms of memory fragmentation.
Relevant questions:
What's it to be an array of?
Is it to be a one-dimensional array?
Why do you want such a frickin' big array?
What access characteristics do you need from it?
edit: Bloody overactive smiley-maker
This message has been edited by Mr Jack, 04-21-2005 10:27 AM

This message is a reply to:
 Message 1 by coffee_addict, posted 04-20-2005 10:24 PM coffee_addict has not replied

  
Percy
Member
Posts: 22505
From: New Hampshire
Joined: 12-23-2000
Member Rating: 4.9


Message 7 of 9 (200949)
04-21-2005 2:53 PM
Reply to: Message 1 by coffee_addict
04-20-2005 10:24 PM


Mr. Jack is precisely correct. std:eque is the best approach for large arrays because it doesn't allocate contiguous memory. Two million is not a particularly large vector. I've used deque's of size one million with no problem.
One word of caution. If your array type is a builtin like int or char then std:eque or std:eque is fine, but if it's an aggregate type you might find better performance if you store pointers to your object instead of the object itself, avoiding many, many calls to copy constructors, e.g., std:eque.
The questions that others raised are good, too. Depending upon your application, a large array may not be the best approach. For example, if you have to search your array you may find std::map<> better suited since it uses a hash for quick lookups.
--Percy

This message is a reply to:
 Message 1 by coffee_addict, posted 04-20-2005 10:24 PM coffee_addict has replied

Replies to this message:
 Message 8 by coffee_addict, posted 04-21-2005 3:03 PM Percy has not replied
 Message 9 by Dr Jack, posted 04-22-2005 4:24 AM Percy has not replied

  
coffee_addict
Member (Idle past 507 days)
Posts: 3645
From: Indianapolis, IN
Joined: 03-29-2004


Message 8 of 9 (200952)
04-21-2005 3:03 PM
Reply to: Message 7 by Percy
04-21-2005 2:53 PM


On a second thought, screw array or vector. I just figured out how to do this without creating an array that big. By the way, my mistake. It's actually 3 million, not 2. All I have to do now is write the subroutine.
I'm doing an analysis with the Galileo data taken during its 3 years orbiting Jupiter (it measured stuff every 38 seconds for 3 years). It's a project I'm working with a professor. Hopefully, I can get some stuff published in the near future

This message is a reply to:
 Message 7 by Percy, posted 04-21-2005 2:53 PM Percy has not replied

  
Dr Jack
Member
Posts: 3514
From: Immigrant in the land of Deutsch
Joined: 07-14-2003
Member Rating: 8.3


Message 9 of 9 (201093)
04-22-2005 4:24 AM
Reply to: Message 7 by Percy
04-21-2005 2:53 PM


For example, if you have to search your array you may find std::map<> better suited since it uses a hash for quick lookups.
std::map doesn't use a hash for quick lookups it uses a tree structure (usually a red-black tree), if you want hash mapping you need the non-standard hash_map included with most popular STL libraries.

This message is a reply to:
 Message 7 by Percy, posted 04-21-2005 2:53 PM Percy has not replied

  
Newer Topic | Older Topic
Jump to:


Copyright 2001-2023 by EvC Forum, All Rights Reserved

™ Version 4.2
Innovative software from Qwixotic © 2024