dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Better way of handling the global char array in c++.

  1. #1
    Join Date
    May 2015
    Posts
    213

    Better way of handling the global char array in c++.

    I have the following defined in the .h.
    static const char *pszPath = "\\stemp\\new\\example";

    I want to change to global in the dll, so it can be changed in the various functions.
    What is the best way to do this, and how do I take care of deleting this memory

    change to :
    extern const char *pszPath = "\\stemp\\new\\example";

    file1.cpp
    {
    string out = "temp";

    pszModelParams = (char *)malloc(sizeof(out));


    memcpy(pszModelParams, out.c_str(), sizeof(out));

    int x = 0;


    }

    file2()
    {

    }


    main()
    {

    // delete pointer here
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: Better way of handling the global char array in c++.

    Memory should be allocated/deleted in the same .exe/.dll. So if a certain .dll allocates the memory, the same .dll should delete it. The allocated pointer can be passed and used in the .exe and other .dlls but only the creator should delete it. You don't have 'global in the .dll'. You have a .dll function that allocates/sets the memory which returns a pointer. You then have another .dll function which takes the allocated memory pointer and deletes it. Note that the initial allocation/initialisation can be done in the .dll's DLL_PROCESS_ATTACH notification. Then the .dll function just returns the required pointer.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  3. #3
    Join Date
    May 2015
    Posts
    213

    Re: Better way of handling the global char array in c++.

    Thanks for the answer kaud.

    In my case, the gloal pointer is used only within functions of my dll only. So i need to take care of allocation and deletion.

    Now my Q is regarding the better way of implementing the global char array.

    I set this variable when a call to certain function within my dll calls it. And when another function from my dll is called, this global is used there within my function

    As it is char *, i need to allocate the memory in function1, and on completinn of last function call in the dll, i need to delete this pointer.

    What us the better way if implemeting this in c++, is it better to go for smart pointer ? Or will it be ok, if i just user char * and proceed with the kind of implentation which is almost similar to present one

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: Better way of handling the global char array in c++.

    If it is used totally within one dll with no external exposure, why not just a simple global std::string?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

  5. #5
    Join Date
    May 2015
    Posts
    213

    Re: Better way of handling the global char array in c++.

    I prefer to keep char *, as these are passed to some functions within the tlb file and i want to retain that interface.

    But in the project where i reference, these are stored as static as it is used for testing some particular value. But in my case, it cannot be static, as it is changed in runtime

    i.e the example where i took it was defined like this in .h file
    static char *pszPath = "\\\\temp\\New";

    And interface function it was called like
    m_pInteropModel->SetSpecialData(bstr_t(pszPath))


    But in my case,
    I moved to .cpp:
    char *pszpath;

    and .h :
    extern char *psz;


    function1()
    {
    malloc(pszData);
    }

    function2()
    {
    //use pszpath
    }

    function3
    {
    //delete pszpath

    }

    All three above functions are within my dll


    Hope this is ok !

  6. #6
    Join Date
    May 2015
    Posts
    213

    Re: Better way of handling the global char array in c++.

    Not sure why they already declared as static char *, instead of string ?

    I just wanted to make it non static to be able to write in runtime , so was trying to keep the same way it is done earlier instead of changing to string.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,900

    Re: Better way of handling the global char array in c++.

    You are confusing static with const. const means it can't be changed. static has different meanings depending upon where it is used. If used with a global variable, that variable can only be used from its point of definition to the end of that compilation unit (internal linkage).

    Does the .dll consist of multiple .cpp files? If it has only 1 .cpp file, why is pszpath extern (external linkage) - as you have already stated in post #3 that pszpath is used only within the .dll functions.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.9)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)