Dynamic allocation/Deallocation of strings

S

sunilkjin

Hi ,
I am trying to alllocate a 2D array of strings and returning it
to a functiion, I get a memory access error. What is wrong with the
program?

int main3DArray( char *** x);
int main()
{
int i,j;
char **p;
main3DArray(&p);
for(i=0,j=0; i< 5;j++, i++)
// cout << p[j] << endl;
return 0;
}
int main3DArray( char *** x)
{
const int l=10;
const int m=10;
const int n=10;

int j,i;

x = (char ***)malloc ( n * sizeof (char ** ));

for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));



for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d array of
"TEST"
}
return 0;
}

Thanks
Sledge
 
Z

Zealot Zuo

Hi ,
I am trying to alllocate a 2D array of strings and returning it
to a functiion, I get a memory access error. What is wrong with the
program?

int main3DArray( char *** x);
int main()
{
int i,j;
char **p;
main3DArray(&p);
for(i=0,j=0; i< 5;j++, i++)
// cout << p[j] << endl;
return 0;
}
int main3DArray( char *** x)
{
const int l=10;
const int m=10;
const int n=10;

int j,i;

x = (char ***)malloc ( n * sizeof (char ** ));

for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));



for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d array of
"TEST"
}
return 0;
}

Thanks
Sledge


I have only one suggestion...
Why not use STL strings?
And still...
Why not use operator new and delete?
 
O

Ondra Holub

(e-mail address removed) napsal:
Hi ,
I am trying to alllocate a 2D array of strings and returning it
to a functiion, I get a memory access error. What is wrong with the
program?

int main3DArray( char *** x);
int main()
{
int i,j;
char **p;
main3DArray(&p);
for(i=0,j=0; i< 5;j++, i++)
// cout << p[j] << endl;
return 0;
}
int main3DArray( char *** x)
{
const int l=10;
const int m=10;
const int n=10;

int j,i;

x = (char ***)malloc ( n * sizeof (char ** ));

for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));



for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d array of
"TEST"
}
return 0;
}

Thanks
Sledge


I think the problem is, that you are making 3d array and working with
2d array. It mixed somehow.

It seems, you want 2d array of char*. O.K., you need char *** p in
main. (2 pointers for 2 dimensions and 1 pointer level to char* data
item).

Try it this way:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main3DArray( char **** x);
int main()
{
int i,j;
char ***p;
main3DArray(&p);

for (i=0,j=0; i< 5;j++, i++)
printf("%s\n", p[j]);
// cout << p[j] << endl;
return 0;
}

int main3DArray( char **** x)
{
const int l=10;
const int m=10;
const int n=10;

int j,i;

(*x) = (char ***)malloc ( n * sizeof (char ** ));

for (i =0; i< n; i++ )
(*x) = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{
(*x)[j] =(char*) malloc (l * sizeof(char));
strcpy((*x)[j],"TEST"); // I am trying to allocate a 2d
array of "TEST"
}
return 0;

}

However there is no need to make the main3DArray function to return
always 0 and return the real result in it's parameter. This is more
readable:

char*** Make3DArray(void)
{
const int l=10;
const int m=10;
const int n=10;

char*** x;

int j,i;

x = (char ***)malloc ( n * sizeof (char ** ));

for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{
x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d
array of "TEST"
}
return x;
}

Anyway, you should use some better (and much much more readable)
solutions in C++, for example std::vector, std::string etc.
 
J

John Carson

Hi ,
I am trying to alllocate a 2D array of strings and returning it
to a functiion, I get a memory access error. What is wrong with the
program?

int main3DArray( char *** x);
int main()
{
int i,j;
char **p;

You need a triple pointer, not a double.
main3DArray(&p);
for(i=0,j=0; i< 5;j++, i++)
// cout << p[j] << endl;


Memory access here I take it.
return 0;
}
int main3DArray( char *** x)
{

Here x is a local variable. Any changes to x only have local effect.
const int l=10;
const int m=10;
const int n=10;

int j,i;

x = (char ***)malloc ( n * sizeof (char ** ));

for (i =0; i< n; i++ )
x = (char **)malloc (m * sizeof (char * ));



for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[j] =(char*) malloc (l * sizeof(char));
strcpy(x[j],"TEST"); // I am trying to allocate a 2d array of
"TEST"
}
return 0;
}

Thanks
Sledge


To get it to work, you need:

char ***p;

You can then get it to work 2 ways:

1. Use a reference:

int main3DArray( char *** &x);

In this way changes to x no longer have only local effect. You call this
with

main3DArray(p);

2. Use a pointer correctly. You use:


int main3DArray( char **** x);

i.e., four asterisks. Inside the definition of main3DArray, you use *x
rather than x (or rather (*x) to get precedence right). Changes to x are
only local, but changes to what x points to are non-local. You call this
with

main3DArray(&p);

as at present.

Of course, all this is a very error prone style of programming. You should
not be using these sorts of pointer operations in normal code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top