[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

CArray  and SetSize member error.

Posted on 1999-10-29
14
Medium Priority
?
716 Views
Last Modified: 2008-02-20
The helpturorial is recommending me to
use the SetSize memberfunction i CArray.
The problem is that an exception is thrown when i do that. Here are the important code in different places in my project CDoc .



protected:

    CArray<CString*, CString*> m_strings;







BOOL CMySerializeDoc::OnNewDocument()
{
      if (!CDocument::OnNewDocument())
            return FALSE;
m_strings.SetSize( 1000);


      // TODO: add reinitialization code here
      // (SDI documents will reuse this document)

      return TRUE;
}







      CString *mystring=new CString;
      *mystring="This is a string written by the author";
                        
      m_strings.Add(mystring);
                        

      ::MessageBox(0,"",*(m_strings.GetAt(0)),0);



Your sincerely andla.

0
Comment
Question by:andla
  • 7
  • 7
14 Comments
 

Accepted Solution

by:
mnbmnb earned 200 total points
ID: 2169750
Why you do not want to use CStringArray? I myself used it and SetSize worked OK.
0
 
LVL 1

Author Comment

by:andla
ID: 2171115
Yes you are right thanks :-), but i think it's important why it doesent work with CArray. With the nowledge of the problem i can deal with other similar problems.
0
 
LVL 1

Author Comment

by:andla
ID: 2171183
Yes you are right thanks :-), but i think it's important why it doesent work with CArray. With the nowledge of the problem i can deal with other similar problems.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 1

Author Comment

by:andla
ID: 2171185
Oops! Double ...
0
 

Expert Comment

by:mnbmnb
ID: 2171310
Did you include <afxtempl.h>?
0
 
LVL 1

Author Comment

by:andla
ID: 2172401
Yes i did. Do you get the same exception?
0
 

Expert Comment

by:mnbmnb
ID: 2172655
I check the same CArray<CString*, CString*> on VC++ 6.0  and its SetSize works OK. I do not see all your code, but for member m_strings try to set "public" access specifier instead of "protected".
0
 

Expert Comment

by:mnbmnb
ID: 2172659
Or place here what "exception" compiler throws.
0
 
LVL 1

Author Comment

by:andla
ID: 2174603
Unhandled exception in MySerialize (MFC40D.DLL): 0XC0000005: Access Violation.

*It does not look pretty this message* :-)
0
 
LVL 1

Author Comment

by:andla
ID: 2174622
Tryed to change to public but the same problem occured.


*Thanks for trying to help me* :-)
0
 

Expert Comment

by:mnbmnb
ID: 2175666
This exception means that you try to access to restricted or invalid area of memory. As long as you create your Carray on the stack, probably you simply set too big size. Try to create your array on the heap:

CArray<CString*, CString*> *m_strings = new CArray<CString*, CString*>;

Hope this help.
0
 

Expert Comment

by:mnbmnb
ID: 2175669
Naturally, member functions further must be called with -> operator:

m_strings->SetSize(1000);
0
 
LVL 1

Author Comment

by:andla
ID: 2178650
Same error. I even tryed to make the setsize smaller. Try to make this sample your self and tell me if it works.

Create a simple MDI project.

Add to CDoc class
public:
CArray<CString*, CString*> *m_strings;

Add to OnNewDocument i CDoc
BOOL CMySerializeDoc::OnNewDocument()
{
      if (!CDocument::OnNewDocument())
            return FALSE;
      m_strings= new CArray<CString*, CString*>;
      m_strings->SetSize(1);
      // TODO: add reinitialization code here
      // (SDI documents will reuse this document)

      return TRUE;
}


Add to serialize in CDoc
void CMySerializeDoc::Serialize(CArchive& ar)
{
      if (ar.IsStoring())
      {
            // TODO: add storing code here
            //ar<<mytest[0];
      CString *mystring=new CString;
      *mystring="This is a string written by the author";
                        
      m_strings->Add(mystring);
                        

      ::MessageBox(0,"",*(m_strings->GetAt(0)),0);

      }//CArray

      else
      {

      }
}


The reason i use it in serialize is because im learning about it.

Good luck!

:-)
0
 

Expert Comment

by:mnbmnb
ID: 2178761
*This* code works OK. Try to do the same(e.g. create new project, past frome here your code, do not forget include in the header file of CDoc class file <afxtempl.h>) and then during debugging step into function SetSize() and catch the line on which exception happens.
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

607 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