1

Supposingly I've declared a character array and take a string from the user as follows:

char s[100000];
std::cin>>s;

Now say the user has entered the string "Program". My character array will be as as follows:

'P''r''o''g''r''a''m''\0'......(99992 remaining indices with no/junk values)

Is there a way to free the space occupied those 99992 indices? Similarly if I've an integer array of size say 100000 and I'm using only first 10 indices during run time, is there a way to resize my array during the run time of my program. I know we can use vectors for this purpose but is the thing possible somehow using arrays? For integer arrays, I know we may declare arrays dynamically and then declare size as per our requirement but say I have array of 10 integers as follows:

1 2 3 4 5 6 7 8 9 10

Now, I want to use only first 9 indices and wnat to kind of delete the 10th index. In other words, along with dynamic allocation, is dynamic deletion also possible with arrays?

EDIT: I know the thing is possible using STLs but I want to know if we can do the same thing in arrays?

CPPCoder
  • 155
  • 1
  • 1
  • 10

4 Answers4

6

No.

If you have arrays defined with a fixed size, you cannot release part of those arrays at run-time. Use a dynamically allocated array of some sort — probably a string or vector<int> for your two example arrays respectively, though a vector<char> might also work sufficiently well for you.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • In vectors too, I don't think we can delete and free space by deleting indices in between. Fo example I've 1 0 0 0......(1000 times) 1 and all 1000 zeros between the two 1s are useless, so I cannot free the space occupied by those 1000 zeros as resize function will shrink the size of vector from back thus deleting 1 at the last position. – CPPCoder Mar 20 '14 at 08:58
  • 1
    No - you can't delete the middle section of a vector trivially, but there are ways to move the unwanted middle to the end (and bring the wanted end forwards), and then you can shrink the result. OTOH, that wasn't what you seemed to be asking about. Your question only discussed deleting from the end. – Jonathan Leffler Mar 20 '14 at 09:02
1

When you write:

char s[100000];

You are telling the compiler to stack 100000 bytes in the program stack. However when you reserve memory dynamically:

char * = new char[100000];

You are asking the system to reserve 100000 bytes in the heap so you can handle that asked memory as you want, even tell the system to free it as a resource.

You can't free memory at the stack until your local context is finished. For example, exiting the function you where you declared char s[100000].

Check this question: What and where are the stack and heap?

std::string is implemented using dynamic memory allocation at the heap and that is why it allows you to reduce its size.

Community
  • 1
  • 1
0

that is not possible.

you may wrap your user input capturing in a subroutine that allocates stack space and allocates heap memory at the actual required length.

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0

You are confused over when to use static allocation and when to use dynamic allocation.

  • Static allocation is used when the maximum number of items is known in advance, at compile-time.
  • Dynamic allocation is used when the number of items is unknown until run-time.

There exists no other case than the two above. You cannot mix them and it wouldn't make sense to do so.

The only case where you should allocate a static array char s[100000]; is the case where you know, at some point, that there will be 100000 items that the program needs to handle.

You designed your program to handle the worst case of 100000 items. It must still be able to handle that many. If the program needs to have an array of variable, unknown size, you should have used dynamic allocation.

If we ignore that C++ exists, then what you would have done in C is this:

char* s = malloc(sizeof(*s) * 100000);
...
s = realloc(s, some_strlenght);

Please note that huge static arrays allocated on the stack is bad practice in many operative systems. So you might have to declare the 100000 array on the heap anyway, even though you won't resize it. Simply because there is likely not enough stack space in your process to declare large, bulky variables like that.

(Also, because of the way C++ is designed, std::string and std::vector etc are always implemented with dynamic memory internally, even if you only use them with one fixed size.)

Lundin
  • 195,001
  • 40
  • 254
  • 396