-1

I am trying to pass in a dynamic array to a function, add two values then print them in main. My current compile only prints "here". Where did I go wrong?

#include <iostream>
using namespace std;

void enqueue(int v1, int v2,char *q,int &size); //push value to back of 
queue
void dequeue(); //delete value @ q[0]

int main(){

int size = 0;
char* q = new char[size];
enqueue(1,2,q,size);
cout << q[2] << endl;
for (int i=0; i<size; i++){
  cout <<q[i];
  }
}

void enqueue(int v1,int v2, char *q,int &size){
  cout << "here" << endl;
  size++;
  q[size]=v1;
  size++;
  q[size]=v2;
}
enter code here
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Not enough memory is allocated. Also 1 and 2 as chars aren't printable. – Bob__ Oct 09 '18 at 06:02
  • Because you set size of q to 0? – o_weisman Oct 09 '18 at 06:02
  • `char* q = new char[size];` doesn't make `q` magically grow as `size` is increased. Arrays don't work this way. You shouldn't be using C-style arrays in any case. – n. m. could be an AI Oct 09 '18 at 06:10
  • 1
    The standard library already has a dynamic array; it's called a `std::vector` and you may considering using it rather than reinventing that wheel. In fact, more apt to your apparent need, the container `std::deque`, or the container adapter `std::queue`, is likely even *better* suited for whatever you're doing, and reduces all of this down to near-nothing. – WhozCraig Oct 09 '18 at 06:14

3 Answers3

1

First, you allocate an array of size 0, you need to specify a size you can work with (at least 3, since you are accessing q[2]. Second, I assume you want to print a number, not a char, so in place of

cout << q[i];

write

cout << (int)q[i];

which converts it to an integer and will print a number.

Jakub Jůza
  • 1,113
  • 2
  • 8
  • 13
0

This will print garbage value as you haven't allocated memory for the array(initial size was 0). If you are looking for more a dynamic array similar to vector(in STL), you need to remember the capacity of the array(how many elements can it store) and what is the current size(number of elements in the array). So every time prior to the insertion you need to check if there is any space left(capacity - current size) and if not reallocate. Obviously memory reallocation i very expensive affair and so there should be a balance in terms of initial allocation and then subsequent reallocation.

To fix your code, please allocate memory for the number of elements prior to enqueuing.

Jolly Jose
  • 84
  • 5
  • I increase the size variable which should be the size of the array right? how does that not allocate more memory? – Tristan Lassiter Oct 09 '18 at 06:14
  • @TristanLassiter There is no connection between the variable `size` and the size of the array. That `size` was used to provide the initial size of the array is irrelevant as soon as the array is allocated. Once allocated, the size of an array cannot be changed. There are many tricks around this. Usually you create a new, larger, array, copy the old array into the new, `delete[]` the old and replace the old with the new. – user4581301 Oct 09 '18 at 06:34
  • For a queue I recommend something a little different. Wrap the allocation in a block structure that contains the allocation and a pointer to the next block. [Take advantage of RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) in the block. When you need more space, allocate a new block and point the previous at it. When you have removed all items in one of the blocks, point the head of the queue at the next block and delete the empty block. This way you can continually push onto one end and remove from the other end without copying. – user4581301 Oct 09 '18 at 06:38
0

You could also change your function enqueue()to receive two chars instead of ints and increase your array size to two and increment after adding the value (arrays usually start with 0).

Here the code:

#include <iostream>
using namespace std;

void enqueue(char v1, char v2,char *q,int &size); //push value to back of

void dequeue(); //delete value @ q[0]

int main(){

    int size = 2;
    char* q = new char[size];
    enqueue('1','2',q,size);
    cout << q[2] << endl;
    for (int i=0; i<size; i++){
        cout <<q[i];
    }
}

void enqueue(char v1,char v2, char *q,int &size){
    cout << "here" << endl;

    q[size]=v1;
    size++;
    q[size]=v2;
    size++;
}

Just to clarify, this is NOT a dynamic array! It has a fixed size of two. In your case you could look into something like std::queue or similar.

krjw
  • 4,070
  • 1
  • 24
  • 49