My problem has to do with passing an array of integers to a function by passing the array's address. The integer array functions properly outside the function, but not inside. Most of what I've written up is just to give some extra details that may or may not be useful. To get the real gist of the problem you can skip to TL;DR at the bottom.
There are two structs:
lineType is a struct consisting of two integer values (valid and tag)
typedef struct
{
int valid;
int tag;
} lineType;
setType is a struct consisting of a pointer to a grouping of lines and a pointer to a grouping of integers.
typedef struct
{
lineType * lines;
int * iruQueue;
}
I have a pointer to a grouping of setTypes called cache (for anyone familiar with computer systems, this object is used to represent a cache system. A cache has a number of sets, each with a number of lines containing a tag, a bit indicating whether or not the line holds valid information, and a byte offset for the actual information the line holds. None of this is really required for my main question, but I thought this might clarify what I am attempting to do).
So we have setType * cache.
I have created a function that will allocate the proper space for the cache called initCache. Again, not sure if this information is necessary, but I want to throw out all the detail I can.
setType * initCache(int NoSets, int NoLines)
int i, j;
setType * cache;
//allocates the appropriate space for the sets
cache = malloc(sizeof(setType) * NoSets);
for(i = 0; i < NoSets; i++)
{
//allocates the space for the lines and the integer array
cache[i].lines = malloc(sizeof(lineType) * NoLines);
cache[i].iruQueue = malloc(sizeof(int) * NoLines);
}
//initializes all valid bits to 0 and the iruQueue entries to -1
for(i = 0; i < NoSets; i++)
for(j = 0; j < NoLines; j++)
{
cache[i].lines[j].valid = 0;
cache[i].iruQueue[j] = -1;
}
return cache;
}
So that is essentially the cache constructor. No problems there. There's a ton more code that I've written, but it all functions correctly after thorough testing.
A little more background: the iruQueue integer array is used to keep track of the least recently accessed line of a set. Ex: I have four lines, the array is initialized as an int * that looks like (-1, -1, -1, -1). Line 0 is accessed. (0, -1, -1, -1). Line 1 is accessed. (0, 1, -1, -1). Line 2 is accessed (0, 1, 2, -1). Line 0 is accessed again. (1, 2, 0, -1)
Here's my problem: at some point I want to pass the integer array to a function that will modify it within the cache. I have two functions for this purpose, enqueue and dequeue. Here's the enqueue function.
void enqueue(int ** iruQueue, int noLines, int lineNo)
{
int cached = 0;
int i;
for(i = 0; i < noLines; i++) {
if(*iruQueue[i] == -1 && cached == 0) {
*iruQueue[i] = lineNo;
cached = 1;
}
}
}
In the above function, I pass a pointer to the specific iruQueue array I want to modify as well as the number of lines in a set and the number of the line just used. It searches through the iruQueue, finds the first index with a value of -1 (indicating that the index is unused) and replaces it with the line number. I pass a specific iruQueue to this function like this.
cache(&(cache[setIndex].iruQueue), noLines, lineNo);
So here's where I really have a problem. I noticed that that it would work on the first call of enqueue, but wouldn't work properly on subsequent enqueues/dequeues.
Printing the specific queue like this prior to enqueues/dequeues (with NoSets being the number of setTypes in the cache array and NoLines being the number of lines in the set array)
int i, j;
for(i = 0; i < NoSets; i++) {
for(j = 0; i < NoLines; j++)
printf("%d, ", cache[i].iruQueue[j])
}
yields the following: "-1, -1, -1, -1, " when there are four lines and 1 set. After one enqueue with a lineNo of 0, it yields "0, -1, -1, -1, ". Proper functioning ends there.
I was curious, so within my enqueue function I added some code to print the iruQueue array before and after modification.
void enqueue(int ** iruQueue, int noLines, int lineNo)
{
int cached = 0;
int i;
//checks the iruQueue array prior to modification
printf("Before: \n");
for(i = 0; i < noLines; i++) {
printf("%d : %d\n", i, iruQueue[i]);
}
for(i = 0; i < noLines; i++) {
if(*iruQueue[i] == -1 && cached == 0) {
*iruQueue[i] = lineNo;
cached = 1;
}
}
//checks the iruQueue array after modification
printf("After: \n");
for(i = 0; i < noLines; i++) {
printf("%d: %d\n", i, iruQueue[i]);
}
}
Here's what it prints when we start with the unmodified iruQueue array (meaning all values should be -1), the number of lines being 4, and the line number being 0.
Before:
0 : -1
1 : 0
2 : -1
3 : 0
After:
0 : 0
1 : 0
2 : -1
3 : 0
It appears as though the iruQueue being modified has changed in that there are zeroes between each proper value. If I changed the print function to print through noLines * 2, the pattern would continue as such. That's my problem. When the iruQueue isn't passed to the enqueue/dequeue functions and I print its contents the same exact way, it will print correctly with no extra indices containing zeroes. Why would passing it as such change it like that, and is there anything I can do to fix it?
TL;DR
I pass an integer array (int * iruQueue) to a function by passing the address of the int array to the function ( &(cashe[setIndex].iruQueue) ). Outside the function it operates as it should, but when passed to the function it creates extra indices with 0 values inbetween each of the correct indices. How do I correct this problem?
If any more code/clarification is needed I will happily provide it. Also, if I'm doing this whole thing wrong (writing up waaaaayyyyy too much or something) just let me know- while I read a lot of things on this website to help my issues, I don't really ever post here, so I just wanted to be as thorough as possible.
Thanks!
Actually, your double pointer isn't needed here and complicates matters. You dereference it when accessing, but don't when printing (which is wrong; I wonder why it doesn't crash or produce more "random" output…). Simply reduce it to a single pointer:
void enqueue(int * iruQueue, int noLines, int lineNo)
{
int cached = 0;
int i;
for(i = 0; i < noLines; i++) {
if(iruQueue[i] == -1 && cached == 0) {
iruQueue[i] = lineNo;
cached = 1;
}
}
}
void foo()
{
enqueue(cache[setIndex].iruQueue, noLines, lineNo);
}
Related
EDIT:
I forgot to mention that I do not want to allocate another temporarily array.
I am trying to solve a problem in C, which is:
Suppose you were given an array a and it's size N. You know that all of the elements in the array are between 0 to n-1. The function is supposed to return 0 if there is a missing number in the range (0 to n-1). Otherwise, it returns 1. As you can understand, duplicates are possible. The thing is that its supposed to run on O(n) runtime.
I think I managed to do it but i'm not sure. From looking at older posts here, it seems almost impossible and the algorithm seems much more complicated then the algorithm I have. Therefore, something feels wrong to me.
I could not find an input that returns the wrong output yet thou.
In any case, I'd appreciate your feedback- or if you can think of an input that this might not work for. Here's the code:
int missingVal(int* a, int size)
{
int i, zero = 0;
for (i = 0; i < size; i++)
//We multiply the element of corresponding index by -1
a[abs(a[i])] *= -1;
for (i = 0; i < size; i++)
{
//If the element inside the corresponding index is positive it means it never got multiplied by -1
//hence doesn't exist in the array
if (a[i] > 0)
return 0;
//to handle the cases for zeros, we will count them
if (a[i] == 0)
zero++;
}
if (zero != 1)
return 0;
return 1;
}
Just copy the values to another array placing each value in its ordinal position. Then walk the copy to see if anything is missing.
your program works and it is in O(N), but it is quite complicated and worst it modify the initial array
can be just that :
int check(int* a, int size)
{
int * b = calloc(size, sizeof(int));
int i;
for (i = 0; i != size; ++i) {
b[a[i]] = 1;
}
for (i = 0; i != size; ++i) {
if (b[i] == 0) {
free(b);
return 0;
}
}
free(b);
return 1;
}
This problem is the same as finding out if your array has duplicates. Here's why
All the numbers in the array are between 0 and n-1
The array has a size of n
If there's a missing number in that range, that can only mean that another number took its place. Which means that the array must have a duplicate number
An algorithm in O(n) time & O(1) space
Iterate through your array
If the sign of the current number is positive, then make it negative
If you found a negative this means that you have a duplicate. Since all items are originally greater (or equal) than 0
Implementation
int missingVal(int arr[], int size)
{
// Increment all the numbers to avoid an array with only 0s
for (int i = 0; i < size; i++) arr[i]++;
for (int i = 0; i < size; i++)
{
if (arr[abs(arr[i])] >= 0)
arr[abs(arr[i])] = -arr[abs(arr[i])];
else
return 0;
}
return 1;
}
Edit
As Bruno mentioned if we have an array with all zeros, we could have run into a problem. This is why I included in this edit an incrementation of all the numbers.
While this add another "pass" into the algorithm, the solution is still in O(n) time & O(1) space
Edit #2
Another great suggestion from Bruno which optimizes this, is to look if there's more than one zero instead of incrementing the array.
If there's 2 or more, we can directly return 0 since we have found a duplicate (and by the same token that not all the numbers in the range are in the array)
To overcome the requirement that excludes any extra memory consumption, the posted algorithm changes the values inside the array by simply negating their value, but that would leave index 0 unchanged.
I propose a different mapping: from [0, size) to (-1 - size, -1], so that e.g. {0, 1, 2, 3, 4, ...} becomes {-1, -2, -3, -4, -5, ...}. Note that, for a two's complement representation of integers, INT_MIN = -INT_MAX - 1.
// The following assumes that every value inside the array is in [0, size-1)
int missingVal(int* a, int size) // OT: I find the name misleading
{
int i = 0;
for (; i < size; i++)
{
int *pos = a[i] < 0
? a + (-a[i] - 1) // A value can already have been changed...
: a + a[i];
if ( *pos < 0 ) // but if the pointed one is negative, there's a duplicate
break;
*pos = -1 - *pos;
}
return i == size; // Returns 1 if there are no duplicates
}
If needed, the original values could be restored, before returning, with a simple loop
if ( i != size ) {
for (int j = 0; j < size; ++j) {
if ( a[j] < 0 )
a[j] = -a[j] - 1;
}
} else { // I already know that ALL the values are changed
for (int j = 0; j < size; ++j)
a[j] = -a[j] - 1;
}
I have got an assignment and i'll be glad if you can help me with one question
in this assignment, i have a question that goes like this:
write a function that receives an array and it's length.
the purpose of the function is to check if the array has all numbers from 0 to length-1, if it does the function will return 1 or 0 otherwise.The function can go through the array only one.
you cant sort the array or use a counting array in the function
i wrote the function that calculate the sum and the product of the array's values and indexes
int All_Num_Check(int *arr, int n)
{
int i, index_sum = 0, arr_sum = 0, index_multi = 1, arr_multi = 1;
for (i = 0; i < n; i++)
{
if (i != 0)
index_multi *= i;
if (arr[i] != 0)
arr_multi *= arr[i];
index_sum += i;
arr_sum += arr[i];
}
if ((index_sum == arr_sum) && (index_multi == arr_multi))
return 1;
return 0;
}
i.e: length = 5, arr={0,3,4,2,1} - that's a proper array
length = 5 , arr={0,3,3,4,2} - that's not proper array
unfortunately, this function doesnt work properly in all different cases of number variations.
i.e: length = 5 , {1,2,2,2,3}
thank you your help.
Checking the sum and product is not enough, as your counter-example demonstrates.
A simple solution would be to just sort the array and then check that at every position i, a[i] == i.
Edit: The original question was edited such that sorting is also prohibited. Assuming all the numbers are positive, the following solution "marks" numbers in the required range by negating the corresponding index.
If any array cell already contains a marked number, it means we have a duplicate.
int All_Num_Check(int *arr, int n) {
int i, j;
for (i = 0; i < n; i++) {
j = abs(arr[i]);
if ((j >= n) || (arr[j] < 0)) return 0;
arr[j] = -arr[j];
}
return 1;
}
I thought for a while, and then i realized that it is a highly contrained problem.
Things that are not allowed:
Use of counting array.
Use of sorting.
Use of more than one pass to the original array.
Hence, i came up with this approach of using XOR operation to determine the results.
a ^ a = 0
a^b^c = a^c^b.
Try this:
int main(int argc, char const *argv[])
{
int arr[5], i, n , temp = 0;
for(i=0;i<n; i++){
if( i == 0){
temp = arr[i]^i;
}
else{
temp = temp^(i^arr[i]);
}
}
if(temp == 0){
return 1;
}
else{
return 0;
}
}
To satisfy the condition mentioned in the problem, every number has to occour excatly once.
Now, as the number lies in the range [0,.. n-1], the looping variable will also have the same possible range.
Variable temp , is originally set to 0.
Now, if all the numbers appear in this way, then each number will appear excatly twice.
And XORing the same number twice results in 0.
So, if in the end, when the whole array is traversed and a zero is obtained, this means that the array contains all the numbers excatly once.
Otherwise, multiple copies of a number is present, hence, this won't evaluate to 0.
I'm trying to find the sum of all positive numbers in an array. So far, I have come up with this;
int largest_sum_sequence(int list, int size)
{
int sum = 0, *index = 0;
for (index < size; index = size; index++)
{
if (list[index] > 0)
{
sum = sum + list[index];
}
}
printf("%d", sum);
return sum;
}
My program keeps crashing. I'm pretty sure it has something to do with index. Whenever I use list[index] it says that I need to use a pointer for index, but I don't know how to do that properly. Help is appreciated!
You do not want index to be a pointer, and your for loop is incorrect. Try:
int sum = 0, index = 0;
for (; index < size; index++)
The compiler tells you actually exactly what to do.
First of all you need an array or a pointer as a parameter, like
int largest_sum_sequence(int *list, int size)
{
....
}
or
int largest_sum_sequence(int list[], int size)
{
....
}
where the latter might be easier to read.
The second thing is, you don't want to iterate with a pointer through the list, but more with a simple integer.
so you declare
int sum = 0, index = 0;
The for() loop isn't quite correct either. Instead of initializing something, you test if index < size and discard the result. This is syntactically correct, but doesn't make sense.
The first part in the for-statement is executed before the loop starts, but doesn't influence the starting itself. It is meant for initializing the loop variable (index in this case). The second part is the termination condition. The loop terminates if the condition evaluates to false. The last part of the loop is for incrementing (or decrementing, more abstract, changing) the loop variable.
Having said this, it is halfway obvious it should look like:
for (index = 0; index < size; ++index) {
....
}
Assuming your list isn't very long (such that the sum could exceed 2^{31} - 1), the rest is correct. Although I'd add an \n to the printf() pattern.
Where is the list coming from? Can't answer that from your code, but you've room for mistakes here too :-)
Function argument doesn't except an array.
Quick function I wrote.
int getSums(int myArray[], int size)
{
int total = 0;
int index = size;
for ( int i = 0; i < index; i++)
{
if (myArray[i] > 0)
total += myArray[i];
}
return ( total );
};
I'm trying to create a programm to find all subsets whose elements sum to a specific target. My logic is : Let's say I have an array of 6 elements like this one
{3,2,4,1,1,9}.
To find a subset whose elements sum to the number 5, all I have to is get the first element and if its value is smaller than 5 then I move to the next elements of the array and I look for subsets whose elements sum to the difference : 5-value of the first element and so on.
Of course if the value of the element is greater than 5 then I move to the next element.
I've been trying to do this with a recursive function in which I will pass my array.
My problem is that I can't understand how to pass my array into my function and how am I supposed to move to the next element using my function again. To be my honest I did my research on this topic and I couldn't really find anything that helped me.
I use C and I declare my function like this void subset(int x[],int n); where n is the number of the elements of my array. I can't really understand the x[] part. My brother told me to do it like this but I can't really understand why.
So you're trying to pass your array into a function? An array has elements or members which will be referenced using the brackets "[ ]" for example in the array "int arry[6]={1,2,3,6,5};" the first element would be the number "1" which we can access using arry[0]
With all due respect it seems like you did not do your "honest research" in this area and I suggest you continue as arrays are an integral part of almost every language.
Below is some c code which should help you get started on your homework. int arry[] is a completely different type than lets say "int arry". The bracket part tells us that it is an array.
#include <stdio.h>
int main()
{
int arry[6]={1,2,3,6,5};
myFunction(arry,4);
return 0;
}
int myFunction(int myarr[], int x){
printf("the number %d element is %d",x+1,myarr[4]);
return 0;
}
Here's an elaborate solution. For the given data, it produces:
Data (6): 3 2 4 1 1 9
Found: 3 + 2 = 5
Found: 3 + 1 + 1 = 5
Found: 4 + 1 = 5
Found: 4 + 1 = 5
The key is to understand what data you need to solve the problem. You need to know:
The target sum
(The sum of the elements so far)
The number and list of unchecked elements
The number and list of elements already in the list
The 'sum so far' can be derived from the number and list of elements already in the list, but it is convenient to pass it too. That gives a 6-parameter recursive function. It is invoked with the desired target, 0 as the sum so far, the complete array of elements to search, and an empty list that is big enough to hold all the elements to be searched. This happens in main(). The function identifies whether it found any lists that added up to the target (returning 0 if not and 1 if it did), hence the test and report and the end of main().
In the function, check for various impossible conditions and bail out. If compiled with debug = 1 at the top, print data on entry to the function. Scan through the remaining unchecked elements, detecting when the element makes the sum equal to the target and reporting the list that does so (note it loses the original index information; you can add that, but it is an exercise for the reader). If the element is small enough to leave space for another, add the element to the found list and pass the rest of the array to a recursive call to the function. The other functions are trivial printing loops.
When target is set to 40 (instead of 5) it correctly reports no subsets are found adding up to 40.
There probably are easier ways to code it, but I'm pathologically opposed to unnecessary global variables (I regard debug as a way of conditionally compiling the code more than as a global variable). It would be possible to package up the argument list in a structure; I'm not sure it would help all that much, though. I could use bool instead of int (and false and true instead of 0 and 1) if I included <stdbool.h>. The code is unrepentantly C99 or C11; it won't compile as written under C89 (but it wouldn't be hard to make it work with C89).
Code:
#include <stdio.h>
static const int debug = 0;
static void found_one(int num, int *list)
{
printf("Found: ");
char const *pad = "";
int sum = 0;
for (int i = 0; i < num; i++)
{
printf("%s%d", pad, list[i]);
pad = " + ";
sum += list[i];
}
printf(" = %d\n", sum);
}
static void print_array(char const *tag, int num, int *arr)
{
printf("%s (%d):", tag, num);
for (int i = 0; i < num; i++)
printf(" %d", arr[i]);
putchar('\n');
}
static int find_subsets(int target, int sumsofar, int num_src, int *src,
int num_dst, int *dst)
{
if (sumsofar >= target)
return 0;
if (num_src <= 0)
return 0;
if (debug)
{
printf("-->> %s: sumsofar = %d, num_src = %d, num_dst = %d\n",
__func__, sumsofar, num_src, num_dst);
print_array("Source", num_src, src);
print_array("Dest'n", num_dst, dst);
}
int rc = 0;
int max = target - sumsofar;
for (int i = 0; i < num_src; i++)
{
if (src[i] == max)
{
dst[num_dst] = src[i];
found_one(num_dst + 1, dst);
rc = 1;
}
else if (src[i] < max)
{
dst[num_dst] = src[i];
if (find_subsets(target, sumsofar + src[i], num_src - (i + 1),
src + (i + 1), num_dst + 1, dst))
rc = 1;
}
}
if (debug)
printf("<<-- %s: %d\n", __func__, rc);
return rc;
}
int main(void)
{
int data[] = { 3, 2, 4, 1, 1, 9 };
enum { NUM_DATA = sizeof(data) / sizeof(data[0]) };
int subset[NUM_DATA];
int target = 5;
print_array("Data", NUM_DATA, data);
if (find_subsets(target, 0, NUM_DATA, data, 0, subset) == 0)
printf("Did not find any subsets totalling %d\n", target);
return 0;
}
I'm new to C and I'm having a small problem with my code:
int i, n;
int *arr;
while(n != 0) {
scanf("%d", &n);
if(n == 0)
exit(1);
else {
arr = (int*) malloc(sizeof(int) * n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
} //end if
} //end while
What I'm trying to do is to make an array of n size and I want to stop reading when I get a '0' for example if I enter:
3
2
2
5
2
6
7
0
I want an array of size 3 with values 2, 2, 5, an array of 2 with values 6 and 7 and exit because of the 0
* Sorry, I left out an important part I think... In my code a call a calc() where I send arr, right after scanf("%d",&arr[i]) and then i'll return the value and then if the next values e.g. 2 isn't 0 I'll read, create a new array, send arr, print result on console and again if the next value is 0 then it will exit. *
Could you guys tell me where I'm wrong?
You are almost there!
You are creating the new arrays in arr, but this is a single pointer so can only refer to one block of memory. When you call malloc the new memory is stored in arr but the old memory is lost. You are 'leaking memory' because the machine has the old memory reserved but you don't have a variable storing it's address so you have no way to find it again.
If you only need to store the last list you should free the old memory (in arr) before malloc'ing the new space. If you need to store all the arrays you will need an array of pointers in arr.
edit:
You need to call free to 'free' the previously allocated memory before you allocate the new memory. At the first set of data you don't have any existing 'malloc' but it's always safe to free a NULL pointer, so simply set the pointer to NULL at the start.
Hint: It's always a good idea to set all the variables to some safe initial value when you define them.
int *arr=NULL; // Mark this as pointing to no memory
....
free(arr); // first time it does nothing, afterwards it deletes the previous reserved memory
arr = (int*) malloc(sizeof(int) * n); // as before this reserves some memory
The problems which are visible in your code are:
1. Checking uninitialized integer n in while. To fix this either initialize n to non zero or use a do{ ... } while() instead of while().
2. You need to validate the value of n which is read through scanf. malloc takes size_t type as the parameter which is unsigned int. But n being an integer can accept negative values, thus if a negative value is entered it will be passed as unsigned int to malloc, this may lead to undesired results (Also for loop will be executed incorrect number of times). You may also consider changing the type of n from integer to unsigned int type or change the exit condition to if( n < 1 ).
3. There is memory leak in your program. Memory allocated through malloc is not freed through free.
4. Do not assume that malloc will always succeed. Please check for the success of malloc through a NULL check i.e.
if (NULL == arr)
{
//error handling
}
5. exit with non zero value generally indicates abnormal termination. You can use break or return. break might be a better idea as it generally gets difficult to test a function as the exit points in the function increase (though this may not be true in your case, but it is FYI)
6. Optionally, you can check the return value of scanf to make sure that a valid input was entered.
Help this helps!
You're not initializing n so you may or may not enter your while loop. Starting n at -1 would be a reasonable thing to do:
int i, n = -1;
And you should cast the return value of malloc, that can hide problems.
You're also leaking memory because you're not calling free on that you get back from malloc and you're losing track of what you read in every time you assign a new value to arr. Brian Roach and Martin Becket have mentioned these things though.
Presumably you want to be able to access these arrays later.
As it is, you're losing your pointer to the previous array when you malloc the next one (and of course, causing a memory leak if it were a larger application).
You need to allocate a chuck of int * (a set of int pointers) then store each int pointer there.
The trick is ... if you don't know how many arrays you're going to need, you need your code to be dynamic (for example; allocate some amount of space, then allocate more if you run out).
Another option is that you could limit the number of series the user can input and tell them they're done when they reach it.
Here's some help if you wanted to go the latter route:
int i;
int n = 1;
int **myArrayOfArrays = malloc(sizeof(int*) * 5); /* max of 5 arrays */
int *arr;
int arrayCount = 0;
while(n != 0) {
scanf("%d", &n);
if(n == 0)
break;
else {
if (arrayCount == 4) {
printf("Woah there partner! That's enough!\n");
break;
}
else
{
arr = malloc(sizeof(int) * n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
myArrayOfArrays[arrayCount] = arr;
arrayCount++;
}
} //end if
} //end while
HOWEVER ... now you don't know how long each array is. Which is a problem. You'd need to keep track of that, or use a dynamic structure such as a linked list. In the example below, we add the length as the first element of each array:
int main()
{
int i;
int n = 1;
int **myArrayOfArrays = malloc(sizeof(int*) * 5);
int *arr;
int arrayCount = 0;
while(n != 0) {
scanf("%d", &n);
if(n == 0)
break;
else {
if (arrayCount == 4) {
printf("Woah there partner! That's enough!\n");
break;
}
else
{
arr = malloc(sizeof(int) * (n + 1)); /* one more than we need */
arr[0] = n; /* store the array length in the first element */
for(i = 1; i <= n; i++)
scanf("%d", &arr[i]);
myArrayOfArrays[arrayCount] = arr;
arrayCount++;
}
} //end if
} //end while
int j;
for (i = 0; i < arrayCount; i++)
{
int length = myArrayOfArrays[i][0]; /* retrieve the length */
for (j = 1; j <= length; j++)
printf("%d ", myArrayOfArrays[i][j]);
printf("\n");
}
}
Dynamic allocation using arrays / raw memory means you need to keep track of stuff. The better approach really is using a linked list for your data. In this case, you could have a linked list of nodes, each of which contained a link list of integers.