Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Size of array

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys!I want to store in 2d array binary numbers with 10 bits.I want to store 10.000.000 numbers but when i try this,my programm prints me the message error and programm crashes. i did not use dynamic memory.I create this array int[10.000.000][10].
Can you help how can fix the problem??
 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's dynamic memory? What error did you suffer? Why did you say you wanted binary numbers? All numbers are stored in binary on a computer. Have you posted the exact code you used? What you had in your text won't compile.
 
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a number is a number.  There is no such thing as a binary number.  

You can have a binary representation of a number, or a decimal representation, or roman numberal representation...

So WHY do you need to store the binary representation of a number? Why not just store the number, and then convert it to decimal when you display it?
 
Master Rancher
Posts: 4507
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

numbers with 10 bits


A short has 16 bits which would use half the storage of an int.

store 10.000.000 numbers


Why the 2 dimensional array?
 
Sheriff
Posts: 16660
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the intent of the [10] dimension is to fit 10 bits, one element per bit. Of course, that's not necessary.
 
fred rosenberger
lowercase baba
Posts: 13014
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems like the classic case of settling on a design without really thinking through what is needed.  So, with any luck, Mark will come back and tell us what he is really trying to do.  
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are allocating space for 10 million arrays of size 10. That means this array can store 100 million integers. At 32 bytes each, you are allocating 3.2 GB to store these integers. I believe the maximum heap space a Java program can consume by default is around 2 GB, so you're probably running out of memory.

You're going to have to design this in such a way where you don't consume that much memory. Or use a ByteBuffer or something similar so that it doesn't get allocated to the heap.
 
Campbell Ritchie
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zachary Griggs wrote:. . . At 32 bytes each . . . .

That's 32 bits each, not bytes. But each array occupies something in the region of 20 bytes over and above its contents. That memory consumption is implementation dependent and probably slightly more on a 64‑bit machine than an old 32‑bit machine. So you can add 10,000,001 × about 20 bytes to your 10⁹ ints.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic