473,491 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Arry elements with not consecutive indexes

11 New Member
Hello,

I woul like to know If there is any way to allocate two positions of memory for a pointer to double but in such way that this positions are not consecutive.

What I really whant is to have one vector of two components where I can get the first component with the index 1, it means: VECTOR[1] but to get the second component I don't want to use the index two: VECTOR[2].

I would like to be able to get it using other index, for instance 10: VECTOR[10]

I dont want to allocate memory for the positions in the middle because of reasons of memory efficiency.


Thank you.
Feb 14 '07 #1
10 1648
Ganon11
3,652 Recognized Expert Specialist
I don't think there is a way to do this without using two different variables (instead of arrays/vectors). Arrays are actually pointers that point to the first element of many - each time you use the [x] operators, you are accessing the memory x * (size of variable) ahead of that pointer. Thus, an array cannot be split up over many locations.

A vector, however, should be storing these elements in separate locations, holding the pointers to each location in its own array (or some similar implementation). However, you still cannot create a Vector with 2 elements and access the element at vector[10]. You may, however, be able to create a vector with room for 10 elements, but only add 2 or 3 at specified indexes.
Feb 14 '07 #2
AdrianH
1,251 Recognized Expert Top Contributor
Hmm, you could define your own vector like class that overloads the operator[]. You could also use map. I'm not sure how memory efficent it is, but the locations of the elements may or may not be side by side (most likely not) as this is a hash and according to Wikipedia is usually implemented as a balanced tree, which means that access to any elment is O(lg n), where n is the number of elements in the map. (lg = log base 2)

Just make the first template parameter an int (which is your key) and away you go. What looks like an array is really a map.

Also, this has more memory overhead than an array when you have many elements, but for a sparce array, it is pretty good.

Here is an example of how to use it: http://www.sgi.com/tech/stl/Map.html

Hope this helps.


Adrian
Feb 14 '07 #3
gallerto
11 New Member
I don't think there is a way to do this without using two different variables ( You may, however, be able to create a vector with room for 10 elements, but only add 2 or 3 at specified indexes.
That is what I do now, But it is not efficient in terms of memory.

Thank you anyway
Feb 25 '07 #4
gallerto
11 New Member
Hmm, you could define your own vector like class that overloads the operator[]. You could also use map. I'm not sure how memory efficent it is, but the locations of the elements may or may not be side by side (most likely not) as this is a hash and according to Wikipedia is usually implemented as a balanced tree, which means that access to any elment is O(lg n), where n is the number of elements in the map. (lg = log base 2)

Just make the first template parameter an int (which is your key) and away you go. What looks like an array is really a map.

Also, this has more memory overhead than an array when you have many elements, but for a sparce array, it is pretty good.

Here is an example of how to use it: http://www.sgi.com/tech/stl/Map.html

Hope this helps.


Adrian
Thank you but think that I can't do it in C. I don't use C++. I forgot to tell that.
Feb 25 '07 #5
AdrianH
1,251 Recognized Expert Top Contributor
Thank you but think that I can't do it in C. I don't use C++. I forgot to tell that.
C... C++, it is all the same :). The main differences are things like constructors and destructors, strong type casting, automatic passing of the object pointer and auto-destruction of automatic variables (variables allocated on the stack). Overloading is helpful but not 100% necessary and operator overloading is just syntactic sugar. Virtual functions are just function pointers. There is of course the libraries too.

I've designed and developed an entire subsystem in C for a production real-time system as I would have done in C++. Constructors and destructors were just functions. I've also made allocators and deallocators (new and delete) functions which just called malloc() and free().

If you want to make your own map in C, you can do it. Just implement a balanced tree algorithm.

Or you could google for a C library implementation. But be warned, if you do this for a school project and they expect you to do it yourself, I'm pretty sure that this is probably too complex a method for it and your prof/teacher will squash you like a little bug for plagiarism. :)


Adrian
Feb 26 '07 #6
gallerto
11 New Member
C... C++, it is all the same :). The main differences are things like constructors and destructors, strong type casting, automatic passing of the object pointer and auto-destruction of automatic variables (variables allocated on the stack). Overloading is helpful but not 100% necessary and operator overloading is just syntactic sugar. Virtual functions are just function pointers. There is of course the libraries too.

I've designed and developed an entire subsystem in C for a production real-time system as I would have done in C++. Constructors and destructors were just functions. I've also made allocators and deallocators (new and delete) functions which just called malloc() and free().

If you want to make your own map in C, you can do it. Just implement a balanced tree algorithm.

Or you could google for a C library implementation. But be warned, if you do this for a school project and they expect you to do it yourself, I'm pretty sure that this is probably too complex a method for it and your prof/teacher will squash you like a little bug for plagiarism. :)


Adrian

Mmmmm I have to say that all your ideas sound like chinish to me. I don't know what is a map. That is way I thought that it was C++.

It is not a school exercise. It is my first program in C but it is quite big (arround 6000 lines and still growing) (but I have a lot of experience in programming FORTRAN 95). I am writing a research program in an important laboratory to calculate aerodinamic forces.

I really need to increase the memory efficiency but it has to be with pointers because I wrote a lot of functions that expect to receive that pointers. In fact I am working with "double *****ArryOfFiveDimensions".

It would be very good if I didn't have to change all that funtions. Just allocate memory in a more efficient way.


Thankyou very much.
Feb 26 '07 #7
AdrianH
1,251 Recognized Expert Top Contributor
Mmmmm I have to say that all your ideas sound like chinish to me. I don't know what is a map. That is way I thought that it was C++.

It is not a school exercise. It is my first program in C but it is quite big (arround 6000 lines and still growing) (but I have a lot of experience in programming FORTRAN 95). I am writing a research program in an important laboratory to calculate aerodinamic forces.

I really need to increase the memory efficiency but it has to be with pointers because I wrote a lot of functions that expect to receive that pointers. In fact I am working with "double *****ArryOfFiveDimensions".

It would be very good if I didn't have to change all that funtions. Just allocate memory in a more efficient way.


Thankyou very much.
Are you saying that I sound like I’m speaking Chinese? :) I take that you mean that you don’t understand what I am saying, right?

It is interesting that you are playing with 5 dimensional arrays. What exactly do you need that for? That is pretty crazy stuff. Do you actually have a 5 dimensional array? What are the bounds on each dimension?

If you don’t want to change the functions, then you are stuck. You’re option is to either get more memory or get a new computer with more memory.

If you are not using any of the incompatible new C99 standard, you could may be able to just rename all your .c files to .cpp and use C++ (pre C99 C standard, C was a subset of C++).

If you are using a 5 dimensional array and this is a sparse array, you will have to change the interfaces that take a “double *****” with a “map<size_t, map<size_t, map<size_t, map<size_t, map<size_t, double> > > > >”. You could make this a lot cleaner using “typedef map<size_t, map<size_t, map<size_t, map<size_t, map<size_t, double> > > > > double_p5;” and use “double_p5” to replace “double *****”.

Please note that your programme will be slower in some sense as each map has a O(lg n) access time. This means for a map that has n elements, it will take lg n (rounded up to an integer) times longer to access than if it were a O(1) access time which is the access time of an array.

However, in your situation, a sparse 5D array may consume a lot of memory, requiring a lot of swapping to and from disk which would be far slower than using a map.

I am a contractor, and for a small fee, I could help you migrate your code. Send me a PM if this is something you would be interested in.


Adrian
Feb 26 '07 #8
gallerto
11 New Member
Are you saying that I sound like I’m speaking Chinese? :)

Yes. Sorry for the ortography.

It is interesting that you are playing with 5 dimensional arrays. What exactly do you need that for? That is pretty crazy stuff. Do you actually have a 5 dimensional array? What are the bounds on each dimension?

Yes I do. My arrys are like this ARRY[ti][xi][yi][zi][vi]. They store the evolution of the velocity components inside a fluid domain. The first four indexes are the time and space coordinates of the points and the last one correspond to each one of the components of the vector of the velocity. It means that what my arrys contain is the evolution of a 3D vectorial field.

When I started the program I decided to do it like this because it is very easy and similar to the phisic plane. But now it I know that maybe it is not a good idea. I have to waste a lot of memory for the pointers. I did it because I used FORTRAN before C and in FORTRAN you don't have that problem. Multidimensional arrys work in a different way in fortran. But when I started using multidimensional arrys in C I didn't still undertand very well what I was doing.

Now it is to late to change it. My program has arround 7000 lines and my grant will finish soon. I don't have time to change all the program. Anyway the program currently works with any problem of memory at all. The problems could arrive years later when the technology improves and the number of points of the vectorial field increases.

Now I just have an arry of 10x100x100x10x3 elementes more or less.

I will just leave a report with recommendations for future improvement of the program.

If you are not using any of the incompatible new C99 standard, you could may be able to just rename all your .c files to .cpp and use C++ (pre C99 C standard, C was a subset of C++).

In fact all my files are .cpp and I compile them as cpp with Devc++. I think that the problem is that I don't have time to change all the code neither to lear how to do all that things you sugest.

I allways learnt to program on my own. I have some little lacks.


Thank you very much for all your sugestions.
Feb 28 '07 #9
AdrianH
1,251 Recognized Expert Top Contributor
Are you saying that I sound like I’m speaking Chinese? :)

Yes. Sorry for the ortography.
No prob. Once I looked up what orthography was, I understand what you are now saying.
Yes I do. My arrys are like this ARRY[ti][xi][yi][zi][vi]. They store the evolution of the velocity components inside a fluid domain. The first four indexes are the time and space coordinates of the points and the last one correspond to each one of the components of the vector of the velocity. It means that what my arrys contain is the evolution of a 3D vectorial field.

When I started the program I decided to do it like this because it is very easy and similar to the phisic plane. But now it I know that maybe it is not a good idea. I have to waste a lot of memory for the pointers. I did it because I used FORTRAN before C and in FORTRAN you don't have that problem. Multidimensional arrys work in a different way in fortran. But when I started using multidimensional arrys in C I didn't still undertand very well what I was doing.

Now it is to late to change it. My program has arround 7000 lines and my grant will finish soon. I don't have time to change all the program. Anyway the program currently works with any problem of memory at all. The problems could arrive years later when the technology improves and the number of points of the vectorial field increases.

Now I just have an arry of 10x100x100x10x3 elementes more or less.

I will just leave a report with recommendations for future improvement of the program.
Are these arrays static, meaning, they do not change in dimension? If this is true, then you are right about the pointers wasting space. However, if this is dynamic, then the vectors will do you more good than harm.

If they are static though, then you could allocate your array on the stack and pass these things around as arrays, but you would have to increase your base stack size to be > 23Mib (10*100*100*10*3*8). The pointer space saved would be (10+10*100+10*100*100+10*100*100*10)*4 which is just over 4.2Mib. I'm not sure if it is worth it though.

If you are not using any of the incompatible new C99 standard, you could may be able to just rename all your .c files to .cpp and use C++ (pre C99 C standard, C was a subset of C++).

In fact all my files are .cpp and I compile them as cpp with Devc++. I think that the problem is that I don't have time to change all the code neither to lear how to do all that things you sugest.
What you described sounds like it is not all that sparse of an array. I don't know if what I suggested would have helped you actually.
I allways learnt to program on my own. I have some little lacks.


Thank you very much for all your sugestions.
No prob. We all have those (lacks and suggestions ;)).


Adrian
Feb 28 '07 #10
gallerto
11 New Member
Are these arrays static, meaning, they do not change in dimension? If this is true, then you are right about the pointers wasting space. However, if this is dynamic, then the vectors will do you more good than harm.

They are dinamic, of course.


What you described sounds like it is not all that sparse of an array. I don't know if what I suggested would have helped you actually.

Not the arry I descirbed but in the middle of the program i work with sparse arrys. For instace when i calculate the pressure I only need it in the first and last values of the indexes xi,yi and zi.

No prob. We all have those (lacks and suggestions ;)).

Thank you anyway for the interest that you showed
Mar 3 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2745
by: Nickolay Kolev | last post by:
Hi all, I have a list whose length is a multiple of 3. I want to get a list of tuples each of which has 3 consecutive elements from the original list, thus packing the list into smaller...
0
2248
by: Freddy Drogt | last post by:
Hello, I have a logfile and I want to make statistics of it. I have my logfile put i a array and ech ellement of the array is a virus detection. I want to create a log in the follow format:...
6
5723
by: Gilles Nambert | last post by:
Hi, I come form VB and i'm used to Redim statement to modifiy the size of an arry dunamically, keeping his content, and i dont't find how to do this simple operation in C# ? Thanks and best...
5
2366
by: Anjo Gasa | last post by:
Let's say I have: std::vector<Object> rgObjects; rgObjects itself is declared as a local variable and hence on the stack. But what about as I add elements to rgObjects: Object newObject( 3,...
8
5622
by: Mir Nazim | last post by:
Hello, I need to write scripts in which I need to generate all posible unique combinations of an integer list. Lists are a minimum 12 elements in size with very large number of possible...
12
20522
by: whytwelve13 | last post by:
Is there a way to find all elements that are "under" some point on the screen in JavaScript? That is, given (x, y) find all elements of size (w1, h1) whose absolute position compared to the...
56
5090
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
2
2366
by: Angus | last post by:
Hello I have a vector<int(aRemovecoll) which is a list of the indexes to be removed from another vector. The other vecotr contains an object - I will call it SomeObject. So the other vecotr is...
2
11219
by: Gentr1 | last post by:
Hi everybody! I am presently working on a Genetic Programming API in python. I have a bit of a problem at the moment... For some specific reasons, I am using nested lists data structure to...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7360
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.