-1

I'm having trouble getting rid of the core segmentation fault on this code. It's creating a series of names in a 3-dimensional array with the dimensions row, col, and chars, where chars stores up to 5 letters of a name.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

const int MAXSIZE = 11;

char*** names;
names = new char** [MAXSIZE];
cout << &names << " ";
for (int i = 0; i < MAXSIZE; ++i) {
    names[i] = new char* [MAXSIZE];
    cout << &names[i] << " ";
    for (int j = 0; j < MAXSIZE; ++j) {
        names[i][j] = new char [5];
        cout << &names[i] << " " << i << j;
    }
    cout << endl;
}   

I've inserted some debugging in there too. I see that it is able to finish assigning addresses, so I'm not sure what's going wrong. No other code is being done, even as I have deletes at the end that are all good.

Ben Comer
  • 1
  • 1
  • Why don't you use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) and [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string)? – Some programmer dude Nov 15 '18 at 03:12
  • Some programmer dude I tried, I thought it would be more concrete with memory allocation if I used characters. I will change it back once I resolve the problem. – Ben Comer Nov 15 '18 at 03:13
  • And a note about "stores up to 5 letters". Don't forget that `char` strings in C++ are really called ***null-terminated** byte strings*. That *null-terminated* bit is crucial, and of course needs space as well. That means a string of five characters really need space for *six* to fit the terminator. A fact that you don't have to worry about (but still know) if you use `std::string` of course. – Some programmer dude Nov 15 '18 at 03:14
  • Some programmer dude Understood, thanks for the tip. This is an early build, but I'll definitely take your advice into account and switch to strings. – Ben Comer Nov 15 '18 at 03:16
  • In cout, why & in names? – Arun Nov 15 '18 at 03:18
  • As for your problem with your current code, please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. Also please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Nov 15 '18 at 03:21
  • Repeatedly printing `&names[i]` in the output seems kind of pointless. Do you mean `&names[i][j]`? – user4581301 Nov 15 '18 at 03:32
  • ^^ and don't you plan on putting something in those buffers before trying to print them ? – WhozCraig Nov 15 '18 at 04:13
  • @BenComer As an alternative -- [char*** names = create3DArray(MAXSIZE,MAXSIZE,6)](https://stackoverflow.com/questions/52068410/allocating-a-large-memory-block-in-c/52069368#52069368). – PaulMcKenzie Nov 15 '18 at 04:18
  • @Arun This was to check that the program is correctly assigning addresses to indices. – Ben Comer Nov 15 '18 at 06:06
  • @Someprogrammerdude Thank you, I'm new here, and this is really helpful. I'm going to archive this in an hour and try again. – Ben Comer Nov 15 '18 at 06:07

1 Answers1

0

Your code is ok, but remember that you can only store 4-symbol names in char[5] array. Some modifications of your example

const int MAXSIZE = 11;

char*** func()
{
    char*** names;
    names = new char**[MAXSIZE];
    for(int i = 0; i < MAXSIZE; ++i)
    {
        names[i] = new char*[MAXSIZE];
        for(int j = 0; j < MAXSIZE; ++j)
        {
            names[i][j] = new char[5];
            memset(names[i][j], 0, 5);
            memcpy(names[i][j], "abcd", 4); // !!! only 4 symbols for name !!!
        }
    }
    return names;
}

int main()
{
    char ***names = func();

    for(int i = 0;i < MAXSIZE;i++)
        for(int j = 0;j < MAXSIZE;j++)
            cout << names[i][j]<< endl;
// free memory
}
snake_style
  • 1,139
  • 7
  • 16