Unable to initialize string in 64bit

Hi,

I am encountering a strange behaviour in solaris10. I have a C++ code which is working fine when I am compiling it in 32bit mode. But when I a compile it in 64bit mode I am facing the errors. My class has string variables(For using string I have coded
#include <string>
using namespace std;)

I have initialized the variables to NULL in the constructor and then I have checked the length of the variable (variable.length()). It returns a garbage value. This behaviour is only in 64bit mode. The same code works fine in 32bit mode.

Can anyone pls give me a clue of how to solve this error.

Regards,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef ACC_OVERVIEW_MPBX_HPP_INCLUDED
#define ACC_OVERVIEW_MPBX_HPP_INCLUDED

// System includes
#include <string>
#include <map>

// Sort key position
#define SK_POS_ACC_OVERVIEW_MPBX       "1-59,60-62"
#define SEQ_INIT_POS					10
#define COUNTER_SIZE					7
#define SET_FILL             			'0'
#define RECORD_TYPE_POS					59
#define RECORD_TYPE_LEN					3
#define HEADER_RECORD_TYPE				"000"
#define TRAILER_RECORD_TYPE				"900"
#define SPACE                  			 " "
#define START_RECORD_TYPE				"100"
#define ERR_ENV_VAR				    	"proper environment variable is not set"
#define ERR_NOT_SET				    	"varibale is not proper"

//using std:: map;
//using std:: map ::allocator;
using libbmb_mod::CError;
//using std:: string;
 using namespace std;
// Local includes
#include "merge_pass.hpp"
#include "common.h"
#include "generic_merge.hpp"
#pragma align 64 (mstrHeader)
#pragma pack(8)
class CAccOverviewMpbx : public CGenericMerge
{
public:

        // constructor
        CAccOverviewMpbx(const CMergeModule*         pmmModule,
                        const CAbstractMergeJob&    amjMergeJob);
		 /** Processes the specified input record.
		 * returns one of CONT, ERROR, NEXT or SKIP.
		 */
		 virtual PROCESSING_RESULT   ProcessInputRecord(string&     strRecord,
												   int         wRecordCount);
			/**
		 * Processes the specified output record.
		 * returns one of CONT, ERROR, NEXT or SKIP.
		 */
		 virtual PROCESSING_RESULT   ProcessOutputRecord(string& strRecord);

		 /// A no-op.
		 virtual PROCESSING_RESULT   EndOfOutput(string& strRecord);

private:
		int wDocSequence;

		int wDocSeqTemp;

		bool fIsHeaderSaved;

		bool fIsTrailerSaved;

		string mstrHeader;

		string mstrTrailer;

		
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
CAccOverviewMpbx::CAccOverviewMpbx(const CMergeModule*         pmmModule,
           						   const CAbstractMergeJob&    amjMergeJob) :
    CGenericMerge(pmmModule, amjMergeJob)
{
    char*   pchFunctionName = (char*) "CAccOverviewMpbx::CAccOverviewMpbx";

    //initialises the variables
    wDocSequence = 0;
    // added to increase the doc sequence of temp files
    wDocSeqTemp = 0;
    fIsHeaderSaved = true;
    fIsTrailerSaved = true;
	printf("%d\t%s\t%d\t**%s**\n",mstrHeader.length(),__FILE__,__LINE__,mstrHeader.c_str());fflush(stdout);
	DEBUG_TRACE;

	// invlkes the sorting method by passing the sort key details
    CSortConfig*    pscSortCfg = new CSortConfig(SK_POS_ACC_OVERVIEW_MPBX, false);

    if(!pscSortCfg)
    {
        throw CError(BMB_HARD_OTHER, ERR_MSG_MEM_ALLOC_FAILED);
    }

    CMergePass*     pmpMrgPass = new CMergePass(pscSortCfg, 0);

    if(!pmpMrgPass)
    {
        throw CError(BMB_HARD_OTHER, ERR_MSG_MEM_ALLOC_FAILED);
    }
    mplMergePasses.push_back(pmpMrgPass);
}

What exactly is the problem? Do you get compilation warnings?
In the class I have a string variable "mstrHeader". I have initialized the variable to null. In the constructor at line no 13 I am trying to print the length of the variable (mstrHeader.length()). As the variable is initialized to null the length should be 0. But the length is printed as 58.

I am working on solaris 5.10 environment and using CC compiler(Sun C++ 5.8). Compiler flags are "-xarch=v9 -xildoff -mt -D${PRODUCT} -DSOLARIS -DBT_UNIX -DBT_SOLARIS".
You initialised a string to null? That can't be right. Any chance of posting the code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
CAccOverviewMpbx::CAccOverviewMpbx(const CMergeModule*         pmmModule,
           				const CAbstractMergeJob&    amjMergeJob) :
    mstrHeader(""),CGenericMerge(pmmModule, amjMergeJob)
{
    char*   pchFunctionName = (char*) "CAccOverviewMpbx::CAccOverviewMpbx";

    //initialises the variables
    wDocSequence = 0;
    // added to increase the doc sequence of temp files
    wDocSeqTemp = 0;
    fIsHeaderSaved = true;
    fIsTrailerSaved = true;
	printf("%d\t%s\t%d\t**%s**\n",mstrHeader.length(),__FILE__,__LINE__,mstrHeader.c_str());fflush(stdout);
	DEBUG_TRACE;

	// invlkes the sorting method by passing the sort key details
    CSortConfig*    pscSortCfg = new CSortConfig(SK_POS_ACC_OVERVIEW_MPBX, false);

    if(!pscSortCfg)
    {
        throw CError(BMB_HARD_OTHER, ERR_MSG_MEM_ALLOC_FAILED);
    }

    CMergePass*     pmpMrgPass = new CMergePass(pscSortCfg, 0);

    if(!pmpMrgPass)
    {
        throw CError(BMB_HARD_OTHER, ERR_MSG_MEM_ALLOC_FAILED);
    }
    mplMergePasses.push_back(pmpMrgPass);
}


Above is the code. In the initialization section I have initialized mstrHeader variable to "". I am facing the issue only in 64bit. The code actually does not initialize the variable. But the behaviour was good in 32bit. And the error was generated in 64bit. Then I have initialized it to zero as there is a condition which passes only if the length is zero. For the length to be zero I have initialized it to null(""). Its of no use as it is retrieving a junk value.
Last edited on
You probably have a build problem. I don't see anything obviously wrong with the code.

Are you sure you're not picking up 32bit libs where you should be picking up 64 versions?

BTW, you don't need to initialise a string with "", the default constructor does the right thing, just as it does for mstrTrailer.
Yeah you are right regarding the initialization. We are acutally no doing it in 32bit mode. Still it was working fine as the default constructor was doing the initialization. But it is not the case in 64bit env. Though we are trying to initialize it to null we are still facing the issue.
Topic archived. No new replies allowed.