char * and object

Hello,
I have one question about the scope of char * with c++ object.
For Example :
I have this simple class :
Hello.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Hello
{
public:
Hello();

void save(char *cPtr);
void say();

private:
char * msgPtr;
};
End_of Hello.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hello.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include
#include "hello.h"

Hello::Hello(){}

void Hello::save(char * cPtr) {
msgPtr = cPtr;
}

void Hello::say() {
std::cout << msgPtr << "
";
}
End_of Hello.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I want to use this class like this :
main.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "hello.h"
int main(int argc, char *argv[]) {
Hello hello1;
{
char *message = "Helloworld!"; //point_1
hello1.save(message); //point_2
} //point_3
hello1.say(); //point_4
}
End_of main.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In my opinion, this piece of code is not correct.
At the point_1, I allocate the memory with "Helloworld!".
At the point_2, I store the pointer into the object hello1.
At the point_3, End of the scope for the char *message, the memory and data is no longer guarantee for me
At the point_4, I try to print the data.

Do you agree with me?
- If yes, how can I store char[] into one object? I have to use malloc/free to allocate memory?

- If no, plz feel free to explain me your point of view :)

Many thx

Comments

  • [code]
    : class Hello
    : {
    : public:
    : Hello();
    :
    : void save(char *cPtr);
    : void say();
    :
    : private:
    : char * msgPtr;
    : };
    [/code]
    :
    : Hello.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    [code]
    : #include
    : #include "hello.h"
    :
    : Hello::Hello(){}
    :
    : void Hello::save(char * cPtr) {
    : msgPtr = cPtr;
    : }
    :
    : void Hello::say() {
    : std::cout << msgPtr << "
    ";
    : }
    [/code]
    :
    : I want to use this class like this :
    : main.cpp~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    [code]
    : #include "hello.h"
    : int main(int argc, char *argv[]) {
    : Hello hello1;
    : {
    : char *message = "Helloworld!"; //point_1
    : hello1.save(message); //point_2
    : } //point_3
    : hello1.say(); //point_4
    : }
    [/code]
    :
    : In my opinion, this piece of code is not correct.
    : At the point_1, I allocate the memory with "Helloworld!".
    : At the point_2, I store the pointer into the object hello1.
    : At the point_3, End of the scope for the char *message, the memory
    : and data is no longer guarantee for me
    : At the point_4, I try to print the data.
    :
    : Do you agree with me?
    : - If yes, how can I store char[] into one object? I have to use
    : malloc/free to allocate memory?
    :
    : - If no, plz feel free to explain me your point of view :)
    :
    : Many thx
    :
    [color=Blue]
    The code is correct.

    Any text pointer defined as "text" - anything between quotation marks - is a static memory, not dynamic. The compiler put this text statically into EXE file, so it is not allocated by any dynamic means. You simply copy that pointer into your object and it exist there until program ends. Scope closing does not affect it. Scope only destroys variable 'message' - pointer itself, but it cannot destroy the contents of that memory, because it is static memory. If you would have a real char array in scope and then used the same technique - that would be the big problem:
    [code]
    {
    char s [20];
    strcpy (s, "Hello!");
    object.save (s);
    }
    // <-- here s is destroyed and object has that address stored. Error!!
    [/code]
    [/color]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion