Dynamic allocation/Deallocation of strings

Discussion in 'C++' started by sunilkjin@yahoo.com, Dec 15, 2006.

  1. Guest

    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
     
    , Dec 15, 2006
    #1
    1. Advertising

  2. Zealot Zuo Guest

    Re: Dynamic allocation/Deallocation of strings

    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[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?
     
    Zealot Zuo, Dec 15, 2006
    #2
    1. Advertising

  3. Ondra Holub Guest

    Re: Dynamic allocation/Deallocation of strings

    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.
     
    Ondra Holub, Dec 15, 2006
    #3
  4. John Carson Guest

    <> wrote in message
    news:
    > 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.


    --
    John Carson
     
    John Carson, Dec 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BigBrian
    Replies:
    12
    Views:
    605
    Pete Becker
    Apr 7, 2005
  2. Ken
    Replies:
    24
    Views:
    3,952
    Ben Bacarisse
    Nov 30, 2006
  3. chris
    Replies:
    6
    Views:
    1,033
    chris
    Oct 28, 2005
  4. pasa_1
    Replies:
    19
    Views:
    535
    David Harmon
    Nov 2, 2006
  5. Replies:
    18
    Views:
    771
Loading...

Share This Page