dynamic character arrays assignment

Hi, I need to create an array of dynamic character arrays and assign values to them in order to pass them one by one to the function.

typedef char* CharArrayPntr;
CharArrayPntr *m = new CharArrayPntr[5];
for (i = 0; i < 5; i++) { m[i] = new char[20]; }



const char *m[0] = "you";
const char *m[1] = "are";


Question: can I create a regular array of dynamically allocated arrays?(and later pass them one by one to the function)?

And how do I assign the value to each of the dynamic arrays? why can't I do
it simple, like m[0] = "you" ?

I've read about assigning it as constant, but don't understand the theory behind it. Why do they need to be constant. Would you please, let me know how to assign the values to dynamic arrays, Please, any help is appreciated.
because m[0] is a pointer to char. You cannot point non-const char pointer to a string literal.
Best advice: do not use c-strings. Use std::string. This is the C++ way do do this.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    char **m = new char*[5];
    for (int i = 0; i < 5; i++)
    {
        m[i] = new char[20];
    }

    m[0] = "you";
    m[1] = "are";
    // ...
    return 0;
}


Like MiiNiPaa said, try avoiding c-strings if possible. C++ offers std::string, that can use the algorithms in <algorithm>.
Not that in the code above, memory is leaked. You should not assign new pointer values to m[x] after they've already been assigned the addresses of newed memory without first deleting the memory they are pointing to.

strcpy would've been more appropriate for lines 9/10.
@cire

According to my knowledge, memory is not leaked when a program terminates after a new. On most OS. But if used in real code, a complementary delete [] is needed.
But if used in real code
I advise against things like "I will do it now but for sure I won't do that in real code". Better to avoid developement of bad habits and develop good ones with each written code snippet.
According to my knowledge, memory is not leaked when a program terminates after a new. On most OS. But if used in real code, a complementary delete [] is needed.


Memory is leaked when we lose all reference to it, and memory is definitely leaked in that code. On line 11, for instance, you cannot access the memory that was previously pointed to by m[0] or m[1]. Why? Because it was leaked.

By your definition programs that end are not capable of leaking, which doesn't seem like a particularly useful definition.
You are quite right in that, I however meant to say that this program ends immediately, and thus it doesn't really leak. Imagine the same code with delete calls at the end, right before main returns. Would this matter on a modern operating system? I also fail to see how the references are lost to m at line 11, please enlighten me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{

    char **m = new char*[5];
    for (int i = 0; i < 5; i++)
        m[i] = new char[20];

    std::cout << "The memory addresses held by:\n" <<
        "\tm[0]: " << static_cast<void*>(m[0]) <<
        "\n\tm[1]: " << static_cast<void*>(m[1]) << '\n' ;

    m[0] = "you";
    m[1] = "are";

    std::cout << "The memory addresses held by:\n" <<
        "\tm[0]: " << static_cast<void*>(m[0]) <<
        "\n\tm[1]: " << static_cast<void*>(m[1]) << '\n' ;

    // ...
    return 0;
}
The memory addresses held by:
        m[0]: 0057C360
        m[1]: 0057C3A0
The memory addresses held by:
        m[0]: 0127DCA8
        m[1]: 0127DC8C


As you can see the memory address returned by new and stored in m[0] and m[1] is lost after the assignment; thus, it is leaked - there is no way for us to recover or release the lost memory within the program.
Oh didn't see that >.<, gotta remember that "X" = const char *. I guess the for loop needs to be removed as to not leak memory.
Topic archived. No new replies allowed.