So I got past my last problem using new[],
but now I seem to have a new one...
I created a char***, var.
The goal was to build each dimension of the array to be different sizes at different times,
based on separate user input...
Here is the code:


using namespace std;

int main(){

	char*** var;
	int ar1,*ar2,x,y,len;
	char inp[80];
	cout<< "var[x]:        x = ";
	cin>> ar1;
	var=new char**[ar1];
	ar2 = new int[ar1];
		cout<< "var["<< x<< "][y]:     y = ";
		cin>> ar2[x];
		var[x]=new char*[ar2[x]];
			cout<< "var["<< x<< "]["<< y<< "]*:  val = ";
			cin>> inp;
			var[x][y]=new char[len + 1];
	cout<< "\n\nNew Array:\n";
			cout<< " "<< x<< " , "<< y<< " = "<< var[x][y]<< endl;
	return 0;

this is what happens:

var[x]:        x = [b]2[/b]
var[0][y]:     y = [b]2[/b]
var[0][0]*:  val = [b]hello[/b]
var[0][1]*:  val = [b]somevalue[/b]
var[1][y]:     y = [b]3[/b]
var[1][0]*:  val = [b]anothervalue[/b]
var[1][1]*:  val = [b]moredata[/b]
var[1][2]*:  val = [b]ofdifferentsizes[/b]

New Array:
 0 , 0 = ofdifferentsizes
 0 , 1 = ofdifferentsizes
 1 , 0 = ofdifferentsizes
 1 , 1 = ofdifferentsizes
 1 , 2 = ofdifferentsizes


the first part seems to be working,
but when I set a value to var[x][y], it seems to change all of the values...
under New Array it should expectedly show all of the values of different lengths that I put into the array, and each spot in the array should be just large enough to hold that data.
What am I doing wrong here?
I can't seem to get the values to be set correctly...
any help is appreciated

I think you should re-read your previous thread about creating 2D arrays witht he new operator. Look at the following two lines, If I remember correctly, I stated that to create a two dimensional array of chars such that you get a 1D array of strings, you should have the two lines that follow after the inccorect ones insead.

var=new char**[ar1];
var[x]=new char*[ar2[x]];

should be

var=new char*[ar1];
var[x]=new char[ar2[x]];

Also I would like to make a very important point. You need to free the memory you used, that means you need to learn how to use how to use delete. Not since this is a two dimensional array you will need to loop through and delete all of the elements at array[x] before delete the array with the use of delete [] array; .

Please be very careful with allocating and deallocating memory, it leads to memory leaks that are really not wanted, and just cause more problems with code and your computer. Plus try to avoid making these mis-takes in future as some people may end up following you.


I get the error "Cannot convert char* to char**",
it's a three dimensional array,
where x is the number of groups of strings,
y is the number of strings in group x,
and z is the bytes of string y in group x.
I'm trying to allocate just enough memory that I need,
instead of having a maximum number of groups, max strings per group and max bytes per string (well, there is a max of 80 bytes per string because of the char inp[80] but that's beside the point), and then having a bunch of allocated memory that won't get used... I tried to pay careful attention to the array pointers I used which caused the errors in my last problem, but as far as I can tell the declaration and allocation of the variables is working... it's the final var[x][y]=new char[len + 1]; var[x][y]=inp; which isn't working...
If I cout<< var[x][y] immediately after this step, it displays the contents of the variable properly, but if I try to display any other elements of the array then they display the same output as the one I have just set.

Also, I know I have to delete allocated memory... I haven't in the example, I've written like ten test programs to make sure small concepts I'm putting together actually work before I base a larger program off of it--I figured I would get to the delete [] command when I got the variables working properly

Well the = operator is actually pointing the var[x][y] to the variable inp

I think that you should replace the = operator with

for(int a=0;a<=len;a++)

I guess that would make it work as we are asigning the created array with a characters.

okay thanks, I got it working--although, why am I able to go, var[x][y][i+6]='p'; after the loop completes? shouldn't this generate some kind of error? Segmentation fault? Since I would be going outside the bounds I defined with the new command?

is this an acceptable structure for deleting my allocated variables?

			delete [] var[x][y];
		delete ar2;
		delete [] var[x];
	delete var;

okay thanks, I got it working--although, why am I able to go, var[x][y][i+6]='p'; after the loop completes? shouldn't this generate some kind of error? Segmentation fault? Since I would be going outside the bounds I defined with the new command?

Because you are creating and initializing the variables in run time. The compiler simply accepts the code. But at run time the program gets to know that the assignment is out of bounds and wont assign the value.

And secondly. I think that the above code is the correct way of freeing all those created variables.

But i guess even

delete [] var;

Works. But i donot know whether it frees all the created variables.

Ah, based on what Chris said, I thought I had to delete it one dimension at a time... I suppose I could just look and find out for myself somehow.

And, this out-of-bounds thing--will the system know that I'm assigning variables out of bounds? I'm not exactly sure how arrays are saved in the memory anyways... I figured, if I wrote too much into one element, I could overwrite the next element or corrupt the pointer to the next element, or something. Anyways, I added some more code to overwrite the \0 after the string, and then write "abcd" after it, past the supposed bounds of the variable, and it had some strange results like, it would add an equals sign... Actually I found that, before I was putting the \0 into the array, I would get random characters depending on the length of the string..
etc., which is fixed if I just append a null character at the end of my out of bounds string.

Is this one of those things where, the outcome of the program depends on what happens to be in the memory at runtime?
Actually, I ran it once and got a fault where it worked before, so I suppose it does

Well the '\0' character tells that the string is at it end right so no wonder it would put different characters. Secondly i myself have never tried out dynamic assignments, this is mainly because i used vectors for my data management. And i exactly dont know on how the compiler assigns memory.