Solved

How to copy this char array?

Posted on 2004-10-07
8
524 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now