array


Hi,

1- Is there any data type through which i can store a large data(1000 Bytes).


2- how can i implement this arrays in smarter way:
1
2
3
4
5
char array0 [1000] // 1000 bytes.
char array1 [1000] // 1000 bytes.
char array2 [1000] // 1000 bytes.
char array3 [1000] // 1000 bytes.


thanks in advance
You can use the STL vector, which is equivalent, but stores the elements on the heap. You really should learn about the standard containers.

1
2
3
4
std::vector array0(1000);
std::vector array1(1000);
std::vector array2(1000);
std::vector array3(1000);


@kbw:
1- which advantages do i get if i implement them as vector?

2- which benefit do i get if the data are stored in the heap and not in the stack?

1. They're stored on the heap.

2. The heap is faster than the stack, as it's dynamic. Which also means you can remove old or add new elements to the vector -- arrays on the stack are slower and they are fixed-size -- you can't add a new element to an automatic array; whereas with a dynamic container, such as a vector, being dynamic, you can add and remove elements. For example if you create a vector of 10 elements but need 15 at some point; you can use std::vector::push_back(value) to create a new element.

That, I believe, answers both questions.
To add something: a std::vector is not really efficient if you are going large amounts of memory or if you know that the size of your array is going to change several time. For these a std::deque or a std::list are better
closed account (S6k9GNh0)
You can make your own datatype to hold this much although I'm very curious as to what you need to use that much memory at once.

Dynamic array version:

 
char * myArray = new char[1000]; //Allocate enough memory for 1000 char and give a pointer to the allocated memory. 

@computerquip:

you may need so much storage if you have a big data streaming something like video data streaming.

But my first question is still not answered :

How can i implement an array of this type?

1
2
3
	

char * myArray = new char[1000]; //Allocate enough memory for 1000 char and give a pointer to the allocated memory.  


thanks
closed account (S6k9GNh0)
wut? In detail explanation:

1
2
char 8 myCharArray; //This creates a pointer that handles or points to memory that we are going to allocate.
myCharArray = new char[1000]; //This is the same as saying malloc(sizeof(char) * 1000). It allocates enough space for 1000 chars which comes out to be since each character is one byte, 1000 bytes. 
1- which advantages do i get if i implement them as vector?

A vector mirrors the syntax and semantics of an array. But they differ in that it uses heap storage rather than the stack; there is a small stack allocation. A vector can provide range checking on the index. A vector can grow or shrink.

2- which benefit do i get if the data are stored in the heap and not in the stack?

The stack is a very limited resource. Just because the stack grows dynamically on the commonplace desktop OS', doesn't mean it's generally true. DOS apps have a 2K stack by default.

Heap usage can grow and shrink. Stack usage only grows (until the a pop occurs).

You have to copy stack objects that are returned, whereas heap objects are used in place.
closed account (S6k9GNh0)
A vector is similar to an array except it allows dynamic allocation to become easier. The stack allocation is similar to that of the array stack allocation. Did you guys ever know that the standard way of writing a pointer was once char myPointer[] This is also the reason why int main(int argc, char * argv[]) is allowed. argv[][] is not allowed for various reason I will not go into.

Stack should almost always never be used except for small and conventional reason. more latar
@computerquip: first, thanks, second, i mean how can i implement an array so that in each array's cell i can store 1000 bytes?
Aren't 2D arrays really just single-dimensional arrays with added syntactic sugar? I'm fairly sure
int myArray[10][10];
is the same as
int myArray[100];?
In which case
char screen_buffer[COLUMNS][ROWS]
is the same as
char screen_buffer[COLUMNS * ROWS]
which I think ~=
char* screen_buffer = malloc((sizeof(char) * COLUMNS * ROWS));.
Last edited on
The result may be similar but the concept is that myArray[10][10] is an array of arrays
- and you can't access it as 1D array eg: myArrat[15] = 0 -
Last edited on
Of course! It's a pointer to a pointer, what am I thinking?
char** pStr;
If you accessed it as a 1-Dimensional array then you wouldn't be dereferencing the second pointer. Ahhh, I get it now.
Last edited on
Topic archived. No new replies allowed.