C ++. How can I arrange the storage and storage order of 100 char 2D arrays [500] [500]?

I have a homework on programming that requires storing up to 100 char 2D arrays up to 500 * 500 in size along with 4 numbers associated with each of the array. It should be something like this:

struct BLOCK { short x1; short y1; short x2; short y2; char points [ 500 ] [ 500 ]; }; 

The program should read this output:

 p identifier_1 x1 y1 x2 y2 ... identifier_p x1 y1 x2 y2 

Where

  • p - how many BLOCK will be declared (range 1 - 100)
  • identifier - BLOCK identifier (range 1 - 10 000)
  • x1 y1 x2 y2 - it doesn’t matter in this context. The point is that the 2D array they describe can be up to 500 * 500.

My attempt to make it work crashes when I try to enter p> 9:

 #include <iostream> #include <string> using namespace std; struct BLOCK { short x1; short y1; short x2; short y2; char points [ 500 ] [ 500 ]; }; int main () { short numberOfBlocks; cin >> numberOfBlocks; short indices [ numberOfBlocks ]; BLOCK BLOCKsTable [ numberOfBlocks ]; } 

I also noticed that this thing does not work:

 char array [ 100 ] [ 500 ] [ 500 ]; 

I CAN ONLY USE:

  • iostream
  • line

I CAN'T USE

  • malloc, calloc, alloc, etc.
  • std :: vector

My question is:

  • How to make this work work? How to be able to declare up to 100 BLOCK structures?
  • Are there other simple ways to achieve this: declare and save 100 2d 500 * 500 arrays?
+5
source share
2 answers

500 * 500 = 250000. In round numbers, each BLOCK instance will occupy about 250 kb.

Your code example uses the gcc extension to instantiate n from an array to n BLOCK instances on the stack. Thus, ten copies occupy about 2.5 megabytes. I would not expect this to be a problem, but maybe your Linux lab machines are configured with a small maximum size for each process stack. In any case, it is expected that 100 BLOCK instances will occupy up to 20.5 megabytes in the stack, which is much more likely to use the allocation of each process stack, so this will not work anyway.

The answer to the question "How to be able to declare up to 100 BLOCK structures?" it is simple to declare them in the global static namespace and not on the stack. Your assignment conditions are very restrained, they seem to be designed to make you understand the differences between the different types of instance objects β€” heap, stack, and static / global.

+4
source

You need to dynamically allocate an array of BLOCK blocks and dynamically create each block and feed it into an array with identifier / fields (purpose and nature are -int, string, other - identifiers are not specified in your question)

As pointed out in the comments, numblocks is unknown at compile time and therefore tied to a crash (the amount of memory on the stack is also limited, even if the compiler decides to give you an exception and perform the distribution behind the scenes after the value is known at run time)

 int numblocks; std::cin >> numblocks; BLOCKS *blarray = new BLOCKS[numblocks]; for(int i=0; i < numblocks; i++) { BLOCK *b = new BLOCK() std::string identifier; std::cin >> identifier; std::cin >> b->x1 >> b->y1 >> b->x2 >> b->y2; blarray[i] = b; } 

Of course, you need to control the deallocation of the memory later with deletion and deletion []

Note. How can you not use std :: vector if you can use the namespace std ... ??? (this refers to the original question, which was edited to reflect the vector, you cannot use and remove the link to the possibility of using "namespace std")

-1
source

Source: https://habr.com/ru/post/1238735/


All Articles