If you wanted to put int,double into your own container class would you do it?

  • Thread starter Thread starter SpiffyEh
  • Start date Start date
  • Tags Tags
    Class Container
AI Thread Summary
To create a custom container class for integers, doubles, and strings, it is essential to consider the programming language being used. In C/C++, it is recommended to define types like INT32 to ensure consistent size across platforms, as the size of standard integers can vary between 32-bit and 64-bit systems. For string handling, the approach depends on whether strings are fixed-length or dynamically allocated on the heap. If using heap allocation, a struct may not suffice for storing string data, and a large flat memory area can be utilized instead. In Java, generics should be employed, utilizing collections such as List<A>, ArrayList<T>, or Vector<T> to manage different data types effectively within the container class.
SpiffyEh
Messages
191
Reaction score
0
If you wanted to put ints, doubles, strings into your own container class how would you do it?
 
Technology news on Phys.org
It might help if you said which language you are talking about.
 
SpiffyEh said:
If you wanted to put ints, doubles, strings into your own container class how would you do it?

Aside from CompuChip's suggestion, you also need to define the size and the orientation of your "words".

My suggestion to you is if you are doing your development in something C/C++, you should typedef your doubles, ints, and strings so that they are always in the same format regardless of the platform.

Typically what happens is that an int is platform dependent. On a 32-bit platform is 32 bits and on a 64-bit platform it is 64-bits. The best way to get around this is to define something like INT32 which is guaranteed to 32 bits regardless of what platform you execute the code on.

Also with strings, are they fixed length or are they allocated on the heap? If they are allocated on the heap, then your string data will not be able to stored in your structure as a TYPE or struct definition. One way around this is to use a large flat area of memory that is the maximum size of a string. Languages like BASIC can do this, and it's very easy to do this with a struct definition in C/C++.
 
Sorry, I forgot to mention I am trying to do it in Java
 
Then you can use generics. Look into List<A>, ArrayList<T>, Vector<T>, etc.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
35
Views
4K
Replies
23
Views
2K
Replies
36
Views
3K
Replies
17
Views
2K
Replies
6
Views
2K
Replies
36
Views
3K
Back
Top