Click here to Skip to main content
16,011,647 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: why is this crashing? CString question Pin
carrie2-Oct-02 10:36
carrie2-Oct-02 10:36 
GeneralRe: why is this crashing? CString question Pin
User 66582-Oct-02 10:45
User 66582-Oct-02 10:45 
Generalthank you! Thank you! Pin
ns2-Oct-02 11:17
ns2-Oct-02 11:17 
GeneralRe: why is this crashing? CString question Pin
Stephane Rodriguez.2-Oct-02 10:40
Stephane Rodriguez.2-Oct-02 10:40 
GeneralRe: why is this crashing? CString question Pin
ns2-Oct-02 10:50
ns2-Oct-02 10:50 
GeneralRe: why is this crashing? CString question Pin
Ravi Bhavnani2-Oct-02 11:52
professionalRavi Bhavnani2-Oct-02 11:52 
GeneralRe: why is this crashing? CString question Pin
Anders Molin2-Oct-02 13:44
professionalAnders Molin2-Oct-02 13:44 
GeneralRe: why is this crashing? CString question Pin
TyMatthews2-Oct-02 11:53
TyMatthews2-Oct-02 11:53 
I think there are a couple fundamental concepts you're missing here, so I'll try to explain it... hopefully this makes sense.

In a nutshell, your assignment of pArray[i] = "abc" fails because pArray isn't pointing at anything useful in your sample code. Your first line creates the pointer, but at that point it's merely a pointer to a CString; it's not really a CString yet.

A CString is an object, which is very different from the char data type. What's the difference between a CString pointer and a char pointer? You can do this with a char pointer:

char *c;
c = "abc";


When the compiler sees a literal string like "abc", it actually creates that string as a globally-accessible char array variable, which can then be "pointed at". This happens automatically for you, and you're not even really aware that it's happening. The char pointer in the above example can then point at that global variable because the datatypes match. It's a character array located at some address in memory, and you've got a character pointer that can store that address. Essentially, the above code is equivalent to doing this:

char cArray[4];
char *c;
cArray[0] = 'a';
cArray[1] = 'b';
cArray[2] = 'c';
cArray[3] = 0;
c = cArray;


Obviously this is a pain in the ass to code, so conveniently C and C++ compilers handle character strings automatically for you.

Okay, so now where does your CString come in? A CString is a C++ class that wraps character arrays; by itself it's not actually an array of characters. It's not a primitive datatype like char or int. If you tried the following, you'd get a compiler error:

CString *p;
p = "abc";


In this case, p is supposed to be pointing to a CString. "abc" as you learned is actually an array of characters, exactly 4 bytes long. You can't convert from an array of characters to a CString pointer; they're incompatible. You need to point at an actual CString object.

Knowing that, why does your code compile? Your code compiles fine because you access the pArray pointer through the reference operator []. This special operator lets you treat the pointer as a sort of index into an array. You can move anywhere within the array by just changing the index. However, there's a problem in your example. Your pArray isn't pointing at anything. You never initialized pArray to point to an array of CString objects. pArray is pointing at some garbage value off in no-man's land in memory. pArray[i] is trying to move around in that range, and it throws an access violation.

So, the bottom line is you need to initialize your pArray variable to actually point at some valid location in memory. You can follow some of the other examples, or even do something like this:

int iArraySize = 4;
CString *pArray = new CString[iArraySize];
for( int i = 0; i < iArraySize; i++ )
{
    pArray[i] = "abc";
}
 
// Don't forget to delete the array
delete []pArray;


In this case, pArray points to an array of CString objects, dynamically allocated on the heap. The pointer can access each of the four through the reference operator.




    Ty


"The significant problems we face cannot be solved at the same level of thinking we were at when we created them." -Albert Einstein


Generalexcellent explanation Pin
ns3-Oct-02 1:14
ns3-Oct-02 1:14 
GeneralRe: why is this crashing? CString question Pin
NormDroid3-Oct-02 7:04
professionalNormDroid3-Oct-02 7:04 
Generalint to CString Pin
ns2-Oct-02 9:58
ns2-Oct-02 9:58 
GeneralRe: int to CString Pin
User 66582-Oct-02 10:22
User 66582-Oct-02 10:22 
GeneralRe: int to CString Pin
ns2-Oct-02 10:28
ns2-Oct-02 10:28 
GeneralCoercing a CFrameWnd to look like a Dialog based app Pin
Anonymous2-Oct-02 9:37
Anonymous2-Oct-02 9:37 
GeneralAdvanced Doc/View question Pin
Jim Crafton2-Oct-02 9:12
Jim Crafton2-Oct-02 9:12 
GeneralRe: Advanced Doc/View question Pin
Daniel Ferguson2-Oct-02 12:46
Daniel Ferguson2-Oct-02 12:46 
GeneralTelephony Pin
SuperGeek2-Oct-02 9:09
SuperGeek2-Oct-02 9:09 
GeneralRe: Telephony Pin
Michael P Butler2-Oct-02 9:24
Michael P Butler2-Oct-02 9:24 
GeneralRe: Telephony Pin
Anders Molin2-Oct-02 13:49
professionalAnders Molin2-Oct-02 13:49 
GeneralRe: Telephony Pin
Michael P Butler2-Oct-02 20:06
Michael P Butler2-Oct-02 20:06 
GeneralRe: Telephony Pin
Anders Molin3-Oct-02 0:05
professionalAnders Molin3-Oct-02 0:05 
GeneralRe: Telephony Pin
Anders Molin2-Oct-02 13:54
professionalAnders Molin2-Oct-02 13:54 
GeneralRe: Telephony Pin
SuperGeek3-Oct-02 15:58
SuperGeek3-Oct-02 15:58 
Questionpointer to a CSTring? Pin
ns2-Oct-02 9:03
ns2-Oct-02 9:03 
AnswerRe: pointer to a CSTring? Pin
DanielO3-Oct-02 4:33
DanielO3-Oct-02 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.