Link to home
Start Free TrialLog in
Avatar of hxia
hxia

asked on

assign a char arry to std::string in VC++ 7.0

i was not able to assign any char array with more than 15 charactors into a std::string variable.  the variable will be corrupted.  it works fine with anything less or equal to 15 chars.  same code worked perfectly with VC++6.0.  why is this happening?  my program talks to an API, which expects its args as type of std::string, i have no control over that.  please help.

thanks!
hxia

Avatar of Exceter
Exceter
Flag of United States of America image

Post yout code. Here's an example that should work fine,

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    char arr[] = "My name is Exceter, well not really but it is my login name!!!";
    string s = arr;

    cout << s << endl;

    return 0;
}

Are you sure that the char array you are working with is null terminated?

Exceter
Avatar of jkr
Hmm, sounds strange - have you tried

char acLongArray [] = "kkropti89irokvlrotoitk,,,diieuhjmllwoooso88urjmmmmm!";

string str;

str.reserve ( strlen ( acLongArray));

str = acLongArray;

?
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hxia
hxia

ASKER

nothing works at this point, i tried both above.  it is sth with VC++ 7.0.   this was originally 6.0 code, then my .net upgraded to vc++ 7.0  then this happened.
Post your code.
Avatar of hxia

ASKER


      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

above code is just one of a many i tried.  i even started a new project to do this.  i am guessing my string libs are not running right on my machine, i just tried the same code on the other machine in VC++7.0 and it worked just fine.  what should i do at this point?
What header files are you using?

Are you using the <string> header file or the <string.h> header file?

You should be using <string> header file.

You should try posting an entire example code.
I just ran the following code on VC++ 7.1, and it ran just fine:

#include <stdlib.h>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

      printf("%s\n", str.c_str());

      system("pause");
      return 0;
}


Please post an entire example code, like the above, so that we can see exactly what's failing, and how you're getting the failure results.
Is your project a UNICODE project or ANSI?
Avatar of hxia

ASKER

// andy.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "andy.h"
#include <stdlib.h>
#include <string>
#include <stdio.h>
#define MAX_LOADSTRING 100

using namespace std;

// Global Variables:
HINSTANCE hInst;                                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                              // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                  // the main window class name

// Forward declarations of functions included in this code module:
ATOM                        MyRegisterClass(HINSTANCE hInstance);
BOOL                        InitInstance(HINSTANCE, int);
LRESULT CALLBACK      WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK      About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{

      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

       // TODO: Place code here.
      MSG msg;
      HACCEL hAccelTable;

      // Initialize global strings
      LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
      LoadString(hInstance, IDC_ANDY, szWindowClass, MAX_LOADSTRING);
      MyRegisterClass(hInstance);

      // Perform application initialization:
      if (!InitInstance (hInstance, nCmdShow))
      {
            return FALSE;
      }

      hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ANDY);

      // Main message loop:
      while (GetMessage(&msg, NULL, 0, 0))
      {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                  TranslateMessage(&msg);
                  DispatchMessage(&msg);
            }
      }

      return (int) msg.wParam;
}

The same code runs on other machines.  But on mine, it complies, runs and gets junks from the assignment.  This test code is win32.
How do you know it's failing?
The above code does not ouput the contents of str, so there's no way to tell if it fails.

Try modifiying it to the following:
      string str;
      char a[100];

      _snprintf(a, 100, "andy testing sjusfna;sdlfjasl;fsd");
      str = a;

      MessageBox(NULL,str.c_str(), "", 0);

When I run this on 7.1, I get a messagebox with the full contents.
Avatar of hxia

ASKER

i ran it in debuggar, and watched the values.  the string variable is corrupted after assignment.
>>i ran it in debuggar, and watched the values.  the string variable is corrupted after assignment.

Sometimes the debugger can display misleading information.  Try displaying it via MessageBox or on the screen to verify it's contents.

MessageBox(NULL,str.c_str(), "", 0);


That will tell you for sure if it's currupt.
Avatar of hxia

ASKER

ok, i tried to display.  only junks in it.  again, in the debugger, the str had a valid pointer and correct contents, but stringbuf is currupted and had those junks in there somehow.    none of this is happened on other machines when i tried.  at this point i will try to upgrade my .net to 2003, use VC++7.1 instead, hopefully, i will work right this time.  all the answers are correct i am sure, thank you all.
>> i will try to upgrade my .net to 2003, use VC++7.1 instead

7.1 is much better then 7.0, so it's well worth the effort and the cost for the upgrade.

The 7.1 version is more C++ compliant then the 3.x GNU compiler.