Solved

How to copy this char array?

Posted on 2004-10-07
8
526 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

809 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