Solved

How to copy this char array?

Posted on 2004-10-07
8
528 Views
Last Modified: 2012-06-27
Dear Experts,
the program is reading records from an access database.
i'm using VC++ 6.0.
it extract the record to the program.
i want to copy the record (string).
there is no error when i compile it.
but when i run it, the system show this:


application error:
the instruction at "0x004209f1" referenced memory at "0x0044a1e8". the memory could not be "written".


this is the code:


void MainWnd::OpenDB ()
{
      _RecordsetPtr   spRS;
                _ConnectionPtr  spCON;
      char * temp = "";

      int count;
    try{
        CREATEiNSTANCE(spCON,Connection);
        spCON->ConnectionString = L"driver={sql server};SERVER=(local);Database=pubs;"
                                  L"UID=sa; PWD=;";
         spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
                                        L"DBQ=MPEG_List_db.mdb;DefaultDir=C:\\zWAMP_Dev\\VC_Code\\Sample_ADOVC1\\Code;";
 
        spCON->Open( "", "", "", -1 );
        CREATEiNSTANCE(spRS,Recordset)
        spRS->PutRefActiveConnection( spCON );
        spRS->Open("select directory, filename from TabMPEG", vtMissing, adOpenKeyset,
                    adLockBatchOptimistic, -1);

        count = spRS->RecordCount;

        for (int index =0; index <2 ; index++){
      

//i copy the record from here.

                strcpy(temp,(char *) _bstr_t(RsITEM(spRS,"directory")));
      tempFileList[index] = "";
      strcpy(tempFileList[index],temp);




}
        spRS->Close();
        spCON->Close();
       
    }
    catch( _com_error &e){
        _bstr_t bstrSource(e.Source());
        _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ")
            + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ")
            + _bstr_t(e.Description());
       
        MessageBox(0,bs,bstrSource, MB_OK);
    }          
}



thanks alot.

regards,
Kgc
0
Comment
Question by:Kgc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 12255730
......
     char * temp = "";
......
                //put a break-point here to check the correctness of my resolution of the problem
                strcpy(temp,(char *) _bstr_t(RsITEM(spRS,"directory")));
                tempFileList[index] = "";
                strcpy(tempFileList[index],temp);

temp is a char * and it is pointing somewhere in the memory - you don't know where. usually it is the case that when you do assignment using a string literal (example: "a string") it ends up in a read-only part of the memory. obviously whe you try to strcpy to read-only space, you will get an error. you need to dynamically allocate memory for temp using malloc(). and after you are done using the memory free up the resources using free().
Additionally you will also get an identical error with copying string to tempFileList[index] because you are doing the same kind of assignment.
btw, why don't you directly strcpy to tempFileList[index]?
0
 
LVL 30

Expert Comment

by:Axter
ID: 12255852
You don't need to use malloc for the required memory.

You can use a fixed size variable, or use new instead, and then delete when finish.

fix size variable method:
char  temp[99] = "";
0
 

Author Comment

by:Kgc
ID: 12255932
thanks jhshukla and Axter.
i'm very sorry and i think i left out some message.

at the header file, i have declare this:

char *  tempFileList [30];
// i was trying to create an array with 30 strings

thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 12255950
>>char *  tempFileList [30];
// i was trying to create an array with 30 strings

That would create an array of 30 pointers, and it's not what you want.

Instead use the following:
char tempFileList[30];
0
 
LVL 30

Accepted Solution

by:
Axter earned 125 total points
ID: 12255956
If you need an array of strings, you can use the following declaration:
char tempFileList[30][30];
0
 

Author Comment

by:Kgc
ID: 12256741
Dear Experts,

I just want to extract the records (String) from an access database and store it in a list.
The list will be use by the program after that.

Dear jhshukla, the temp is just for testing and i forget to comment it. Sorry about that.

thanks.
0
 
LVL 3

Expert Comment

by:atxondo
ID: 12259721
I think you must declare a function to copy all the string to another array of char pointers.

char ** duplicate (char * chararray[30])
{
      char *  tempFileList [30];

      for(int i=0;i<30;i++)
      {
           strcpy(tempFileList[i], chararray[i]);
      }
       return tempFileList;
}

I think something like this should work. I´m not sure if this is the exact code but i think it should be something like this.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12260011
>> You don't need to use malloc for the required memory.
Forgot that this is C++. not C

for the rest of it, go by Axter's suggestion.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question