-4

I have two classes, class B comtains a static function "funcEx" which has a pointer as param. funcEx should insert the param data into a map.

Class A uses this funx in order to keep params inside the static map. What would happen after class A is destroyed. Will the memory allocation of "param_name" will be released?

class A{
B::funcEx("param_name");
}

class B{
static map<const char*, int, cmp_str> *Map1 ;
static Map1 = new std::map<const char *, int, cmp_str>();

static funcEx(const char * param){
Map1.insert(param,8)
}

}
15412s
  • 3,298
  • 5
  • 28
  • 38
  • 1
    This is not C++ – YSC Dec 26 '18 at 10:17
  • Would you mind to give us a more complete example (ideally a [mcve]). We need to know how exactly the string literal is stored in `class A`, etc. to give you a correct answer. – πάντα ῥεῖ Dec 26 '18 at 10:19
  • this is not c++ syntax but the question is how c++ reacts in kind of this situation when it gets "char array" from the function param into a pointer – 15412s Dec 26 '18 at 10:21
  • You question doesn't make much sense: "a char array which was set on class A" does a poor job as describing _something_. This is why we need an example, and we need a [mcve] to help you. – YSC Dec 26 '18 at 10:23
  • You wrote _"the static map"_; I ask **which map**? – YSC Dec 26 '18 at 10:24
  • 2
    `"param_name"` is a string literal in your example, it will live until the end of the program and a pointer to it may therefore be safely saved in a map. Is that your question? If it is, your pseudo-code doesn't make sense though, as `func` is part of class `B` but called in `A`. –  Dec 26 '18 at 10:25
  • I reshaped the question hope its better. yes this was my question, thank you very much. I was worried that in some point after class A is destroyed "param_name" will be freed – 15412s Dec 26 '18 at 10:29
  • 1
    Your example still doesn't make much sense. If you want to call `funcEx` in `A`, you must use a qualified name to look it up, e.g. `B::funcEx`. Furthermore the string literal cannot be passed as `char*`, only `const char*`. Also note that my previous comment only holds for string literals, not char arrays. Additionally of course the `map` declaration, access specifiers and an enclosing function for the call in `A` are missing. (and `Class` should be `class`) –  Dec 26 '18 at 10:33
  • Possible duplicate of [Lifetime of a string literal returned by a function](https://stackoverflow.com/questions/2579874/lifetime-of-a-string-literal-returned-by-a-function) –  Dec 26 '18 at 10:35
  • Thanks again. I fixed the example above. yes I used a const char* in the code to point to the string, so it now seems to me that the map will point to the string which be allocated in memory till the end of the program – 15412s Dec 26 '18 at 10:50
  • What is the reason you don't use `std::string` and just not have to worry about it? – UnholySheep Dec 26 '18 at 10:51
  • @15412s _"I fixed the example above"_ Your now edited example is still far from valid c++. Also you still left the question open how the `const char*` is stored within 'class A'. – πάντα ῥεῖ Dec 26 '18 at 10:55
  • @15412s Even if I fix the most basic syntax, your code won't compile: http://coliru.stacked-crooked.com/a/3d3f908eb5d0d26b – πάντα ῥεῖ Dec 26 '18 at 10:59
  • const char* is not stored in class a, const char* is the type of input from B::funcEx. what would be the difference from using std::string instead of const char* to point to the string? – 15412s Dec 26 '18 at 10:59
  • 2
    If you use `std::string` instead of `const char*` the class will automatically copy the string (literal) and you don't need to worry about lifetime of the original anymore. – UnholySheep Dec 26 '18 at 11:07

1 Answers1

1

As it was said a literal string like "param_name" is immortal.

To try to answer you :

#include <map>

class B {
  public:
    static void funcEx(const char * param) {
      map[param] = 8;
    }
  private:
    static std::map<const char *, int> map;
};

std::map<const char *, int> B::map;

class A {
  public:
    A(char c) { member[0] = c; member[1] = 0; }
    void f() { B::funcEx("param_name"); }
    void g() { B::funcEx(member); }
    void h(char * s) { B::funcEx(s); }
  private:
    char member[2];
};

int main(int, char **)
{
   A * x = new A('x');
   {
     A y('y');

     {
       char s[] = "ab";

       x->f(); // nothing change with y->f()
       x->g();
       y.g();
       x->h(s); // x can be y

       // here all the keys/pointers into the map still exist

       delete x;

       // here x is deleted
       // => x->member has an unknown value
       // => "x" used as a key in the map is an invalid pointer, may be it is not anymore the string "x"
     }

     // here 's' does not exist anymore
     // the key "ab" in the map is an invalid pointer, may it is not anymore the string "ab"
   }

   // here 'y' does not exist
   // => y.member has an unknown value
   // => "y" used as a key in the map is an invalid pointer, may be it is not anymore the string "y"

   return 0;
}

Because all is static in B that class has no real interest

bruno
  • 32,421
  • 7
  • 25
  • 37