Question

So i'm trying to write a function which would get input from keyboard and store it in the 2d dynamic array. n is the number of lines (tried with 1-4 lines), m is the number of characters per line (256 in my case). I've read plenty about dynamic arrays and the use of new and the code seems totaly fine to me, but i keep getting this error when i try to enter the text: Access violation reading location 0x00000000. Can't figure out why. Please help.

void KeyInput (char **string, unsigned int n, unsigned int m)
{
   cout<<endl<<"Input from keyboard"<<endl;
   string=new char* [n];
   for(unsigned int i = 0; i < n; i++ ) 
      string[i]=new char[m];
   for(unsigned int i = 0; i < n; i++ )     
      gets(string[i]);
}
Was it helpful?

Solution

can you give more information on where you are getting the access violation? I tried the following code (Visual Studio 2010, Window 7 Professional) and did not get an error. Note that I did change the characters per line to 15 instead of 255 as I wanted to test boundary conditions without a lot of typing.

Your function seems to work fine on my machine, however you do have a latent buffer-overflow using gets as it does not check for the length of the string. Remember that gets will append a null-terminator for you, so if in your case you enter exactly 255 characters you will overflow your buffer by one.

void KeyInput(char** string, unsigned int n, unsigned int m);

int _tmain(int argc, _TCHAR* argv[])
{
    char*      strArray;
    KeyInput(&strArray, 4, 15);
    return 0;
}

void KeyInput(char** string, unsigned int n, unsigned int m)
{
    string = new char*[n];
    for(unsigned int i = 0; i < n; i++)
    {
        string[i] = new char[m];
    }

    for(unsigned int i = 0; i < n; i++)
    {
        gets(string[i]);
    }
}

(also ignore the hideous _tmain and _TCHAR stuff, they are Windows idiosyncrasies :) ).

Finally, unless this is an assignment (or an exercise for self learning), do what 40two suggested and use STL to make your life easy.

OTHER TIPS

Use a vector of strings, take advantage of the force that STL has (use the force Luke see code below how):

void KeyInput (std::vector<std::string>& str_vec, int const n)
{
  std::cout << "\nInput from keyboard" << std::endl;
  for (auto i = 0; i < n; i++) {
    std::string tmp;
    std::getline(std::cin, tmp);
    str_vec.push_back(tmp);
  }
}

Update or Why your C++ teachers are wrong:

void KeyInput(char ***string, unsigned int n, unsigned int m)
{
  std::cout << "\nInput from keyboard" << std::endl;
  *string = new char*[n];
  for (unsigned int i = 0; i < n; i++)
    (*string)[i] = new char[m];
  for (unsigned int i = 0; i < n; i++)
    std::gets((*string)[i]);
}

int main()
{

  char **string = 0;
  KeyInput(&string, 4, 100);

  for (auto i = 0; i < 4; ++i) std::cout << string[i] << std::endl;

  return 0;
}
  1. You need triple pointers in order to pass the 2d array by reference and to be properly filled (OMG!!!).
  2. The user can enter only limited length strings (e.g., 99) don't forget strings have one character at the end (i.e., '/0' the null character).
  3. You have to take care of the memory allocated and deleted later in order to avoid memory leaks.
  4. If you want to shoot your self in the foot continue to program like this.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top