Solved

How to copy this char array?

Posted on 2004-10-07
8
525 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

20 Experts available now in Live!

Get 1:1 Help Now