0

I am working in VC++ 2008, and am trying to allocate a multi-dimensional array of chars to do some file work. I know that whenever an array is allocated all the members of the array should be initialized usually in a successive order. what I have currently is this.

char ** thing = new char *[lineY];
for (int ii = 0; ii < lineY; ii++){
    thing[ii] = new char[lineX];
}
... // working with array
// deleting each part of the array.
for (int ii = 0; ii < lineY; ii++){
    delete [] thing[ii];
}
delete [] thing;

the problem that I am running into is that if I add the array to the watch list, or put a break right after its been allocated the debugger states that the array is equal to a number like 51, or 32, and not a block of space with indexes, and values, but when I try to initialize the values of each index by making my allocation this:

char ** thing = new char *[lineY];
for (int ii = 0; ii < lineY; ii++){
    thing[ii] = new char[lineX];
        for (int jj = 0; jj < lineX; jj++){
        thing[ii][jj] = '';
    }
}

edit: the compiler throws "C2137 empty character constant" am I doing something wrong? edit: read msdn on the error number, and found answer

3
  • Just a little question, why on Earth would you ever do manual n-dimensional array allocation ? Commented Feb 8, 2012 at 11:04
  • @ScarletAmaranth when you don't need to generate a object just for one instance that it will be used in, and it is perfectly fine to allocate memory manually dynamically as long as you remember the key principles of dynamic OOP: destroy what you new, manage what you have, and allocate only as needed. besides it would be the same as instantiating an object that allocates n-dimensional arrays. I just avoid a whole mess of accessors. Commented Feb 8, 2012 at 11:17
  • You could always just allocate a single array, store all elements in row/column-major order, and set up pointers that will behave like a 2D array. Commented Feb 8, 2012 at 12:38

2 Answers 2

1

You cannot write thing[ii][jj] = '' because '' is an empty character constant which isn't allowed. Try replacing '' with something like ' ' (with the space between 's)

Sign up to request clarification or add additional context in comments.

Comments

0

Are lineX and lineY compile-time constants? In that case:

std::array<std::array<char, lineX>, lineY> thing;

Otherwise:

std::vector<std::vector<char> > thing(lineY, std::vector<char>(lineX));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.