-2

I do not know whether the topic highlights my question correctly. However, this is my question. I have a fixed sized array. I feed data into the array using the console. When the array is full, i need to create a new array of same size and begin to fill that array. I do not want to expand the existing array or to declare an array of a larger size. In addition I want to delete those arrays if they get empty. This is related to modeling of hardware memory using C++. That's why I want to use arrays with a fixed size to represent memory blocks.

I have to use manual memory management here. I am trying to model the memory management system in hardware systems. what I want to do is something like this.

DataType array[1024];
int i; 
while(True) 
temp = read_console_input(); 
array[i] = temp; 
memory_manager(); 
endwhile

function memory_manager()
if array.is_full()
DataType array1[1024] = new Datatype[];
set_active_array(array1);
endif
endfunction

Thanks

  • 2
    If you need to ask how to dynamically allocate memory you definitely shouldn't be trying to do manual memory management. Take a look at [`std::vector`](http://en.cppreference.com/w/cpp/container/vector), it does everything you want, and it won't leak memory. But maybe you want to start off reading a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Praetorian Dec 17 '13 at 04:34
  • 1
    It sounds a lot like you want something on the order of `typedef char block[fixed_size]; std::vector memory;` – Jerry Coffin Dec 17 '13 at 04:56
  • @Praetorian I have to use manual memory management here. I am trying to model the memory management system in hardware systems. what I want to do is something like this. `DataType array[1024]; int i; while(True) temp = read_console_input(); array[i] = temp; memory_manager(); endwhile function memory_manager() { if array.is_full() DataType array1[1024] = new Datatype[]; set_active_array(array1); endif endfunction` – Sameera Hemachandra Dec 17 '13 at 05:22
  • @SameeraHemachandra: Always edit your question and place the code in there. – Thomas Matthews Dec 17 '13 at 05:28
  • What do you want to do with all of the full arrays? And are you familiar with the concept of a class? – Beta Dec 17 '13 at 05:32

1 Answers1

0

Just use std::vector, both for each block and as a container for the blocks.

You're talking about "when the array is full", which implies that you're keeping track of a dynamic size. std::vector does that for you. It also manages memory for you.

A std::vector doesn't become full -- it will just try to reallocate its buffer if e.g. a push_back would exceed the current capacity -- but you can easily add such a limit on top. I would wrap the vector in a class that provided only the relevant public operations

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • What I want to do is model memory hardware. When a ram block is full, we have to use next memory block. That is what I am trying to model here. Is there any way to declare arrays on the go? – Sameera Hemachandra Dec 17 '13 at 05:43
  • Whether you use raw `new[]` or let a `std::vector` manage the `new[]` and `delete[]` calls for you, there's still a *lot* of machinery involved under the hood. Maybe a faithful solution of the assignment is to use *one big array* that you subdivide into blocks. But this is difficult to say without the assignment text. – Cheers and hth. - Alf Dec 17 '13 at 05:46