Ulrich Krause
Ulrich Krause

Reputation: 1111

How to make variable values persistent between function calls in c / c++

I have the following szenario. I am calling functions in a DLL from a IBM Notes application (Lotusscript).

One of the functions uses type structures and we encounter problems on 64Bit. All values in the TYPE can only be passed to the function ByRef, but I need to pass the values ByVal.

So I decided to have single arguments in the function header for every value. So far, so good. I have around 43 arguments, but LS can only handle 31 ...

Next idea was to call i.e INIT_REG_PERSON_INFO(arg1,arg2, ... ) before I make a call to the function that registers a new user.

INIT_REG_PERSON_INFO(arg1,arg2, ... );
INIT_REG_ID_INFO(arg1,arg2, ... );
REGISTER_USER();

In C, I have a function ( I know, only one argument, but it is only for testing )

    /*====================================================================================*/
    /* FUNCTION: BCC_InitRegPersonInfo                                                    */
    /*====================================================================================*/
    STATUS LNPUBLIC BCC_InitRegPersonInfo(char *FirstName){
        try {
            //extern char *REG_PERSON_INFO_FIRSTNAME;
            memset(&reg_person_info, 0, sizeof (reg_person_info));
            reg_person_info.FirstName = FirstName;

            return NOERROR;

        } catch (...)    {
            LogEventText("* BCC_InitRegPersonInfo::Exception *", NULLHANDLE, NOERROR);
            return 1;
        }
    }

whre reg_person_info is declared as a global variable

REG_PERSON_INFO reg_person_info;

Later, in my REGISTER_USER(), I try to access the value from the global variable

LogEventText("* BCC_RegNewPerson1():reg_person_info.FirstName = %s", NULLHANDLE, NOERROR,
                    reg_person_info.FirstName);

But the value is always empty. I assume, the variable is deleted / reset, as soon as I exit INIT_REG_PERSON_INFO(arg1,arg2, ... );

How would I make the value persistent between function calls?

Upvotes: 0

Views: 3081

Answers (2)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

A practice I have seen and used often to share global variables among units is:

/* globals.h */
#ifndef EXTERN
#define EXTERN extern
#endif

EXTERN int gMyGloablVar;

/* some-other-C-file */
#include "globals.h"

/* main.c */
#define EXTERN
#include "globals.h"

In main.c the storage is allocated; in all other compilation units they variables are declared as extern and so are known. In link time the linker fixes all references to the variables in main.

Upvotes: 0

ha9u63a7
ha9u63a7

Reputation: 6824

You need to create an external linkage for reg_person_info structure. In this way you are implicitly saying "I want this to be available to all units who declares it, including the main declaration unit".

In the begininning of the file where you have:

STATUS LNPUBLIC BCC_InitRegPersonInfo(char *FirstName){...}

Simply put extern typeOfReg_Person_Info reg_person_info;

by the looks of your code snippet, I am assuming that you have declared reg_person_info somewhere in a header file and #include the header file where you need? I'll update my answer once I have seen more code in the question.

Upvotes: 1

Related Questions