473,500 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic allocation/Deallocation of strings

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[i][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[i] = (char **)malloc (m * sizeof (char * ));

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

Thanks
Sledge

Dec 15 '06 #1
3 3121

su*******@yahoo.com wrote:
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[i][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[i] = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[i][j] =(char*) malloc (l * sizeof(char));
strcpy(x[i][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?

Dec 15 '06 #2

su*******@yahoo.com 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[i][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[i] = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[i][j] =(char*) malloc (l * sizeof(char));
strcpy(x[i][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[i][j]);
// cout << p[i][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)[i] = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{
(*x)[i][j] =(char*) malloc (l * sizeof(char));
strcpy((*x)[i][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[i] = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{
x[i][j] =(char*) malloc (l * sizeof(char));
strcpy(x[i][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.

Dec 15 '06 #3
<su*******@yahoo.comwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com
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[i][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[i] = (char **)malloc (m * sizeof (char * ));

for (i =0; i< n; i++ )
for ( j=0; j<m; j++)
{ x[i][j] =(char*) malloc (l * sizeof(char));
strcpy(x[i][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.
--
John Carson

Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2562
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
2
1729
by: Manisha | last post by:
Hi, I am creating a C++ dll which is used to process data passed to it through one of its exported functions. It should be able to process 160 simultaneous requests. For this reason, I have...
5
3737
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
7
8203
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
12
2340
by: whitehatmiracle | last post by:
Hi all Im not quite sure how to use the new and delete for a whole array dynamically. Actually i want that, a user inputs a char in a single char array. Everytime he inputs a char he creates a...
18
2986
by: welch.ryan | last post by:
Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /*...
5
1989
by: mike.m.schmidt | last post by:
Hello, I've been trying to optimize a piece of code that executes in 6 milliseconds. From what I have read, dynamically managing memory with new/malloc and delete/free is less efficient than...
9
3289
by: danil52 | last post by:
I have dynamic memory allocation in my constructor and deallocation in my destructor. However if there is exception being thrown in constructor, the destructor does not get called and memory leaks...
21
2876
by: arnuld | last post by:
I have created a program to print the input words on stdout. Input is taken dynamically from stdin. In each word, each input character is allocated dynamically. I have ran this program with a file...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.