| | |
delete array and assigns array to pointer
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
Hello,
This is my first time using forum to ask around for some assistance.
I will make it briefly.
Both String and StringPtr contains different data.
How to make StringPtr identical to String?
Output:
========================================
allocation was successful
String = abcdefghijklmnopqrstuvwxyz
StringPtr = abcdefghijklmnopqrstuvwxyz²²²²½½½½½½½½ε■
========================================
For "delete [] String;" part, I tried to use the for loop to see if it works.
for(i=0;i<26;++i)
delete String[i];
not working.. I understand this for loop statement is for array of pointer but I have no idea why "delete [] String" is not working even it is only array of char not pointer unless the destructor already have its job for array of char that I do not need to do the "delete [] String;"
This is one of my homework, I have discovered 23 out of 25 errors. Two errors remain which is "delete String[i]" and "two strings identical".
Thank you!
This is my first time using forum to ask around for some assistance.
I will make it briefly.
C++ Syntax (Toggle Plain Text)
void main() { char *StringPtr; //pointer of char char String[26]; //array of chars strcpy(String, "abcdefghijklmnopqrstuvxyz"); if((StringPtr = new char[26]) == NULL) cerr << "\n\a allocation failed" << endl; else cout << "allocation was successful" << endl; for(i=0;String[i] != '\0'; ++i) *(StringPtr+i) = String[i]; cout << "String = " << String << endl; cout << "StringPtr = " << StringPtr << endl; delete [] String; //C4154 Warning... delete [] StringPtr; }
How to make StringPtr identical to String?
Output:
========================================
allocation was successful
String = abcdefghijklmnopqrstuvwxyz
StringPtr = abcdefghijklmnopqrstuvwxyz²²²²½½½½½½½½ε■
========================================
For "delete [] String;" part, I tried to use the for loop to see if it works.
for(i=0;i<26;++i)
delete String[i];
not working.. I understand this for loop statement is for array of pointer but I have no idea why "delete [] String" is not working even it is only array of char not pointer unless the destructor already have its job for array of char that I do not need to do the "delete [] String;"
This is one of my homework, I have discovered 23 out of 25 errors. Two errors remain which is "delete String[i]" and "two strings identical".
Thank you!
>void main()
int main()
>if((StringPtr = new char[26]) == NULL)
This may or may not correctly handle errors depending on how old your compiler is.
>*(StringPtr+i) = String[i];
You know, you can do
>cout << "StringPtr = " << StringPtr << endl;
You forgot the last character, the '\0'.
>delete [] String;
And why are you trying to delete an array?
>For "delete [] String;" part, I tried to use the for loop to see if it works.
Why don't you take that entire line away and see if it works? You're not supposed to release memory unless you explicitly allocate it.
int main()
>if((StringPtr = new char[26]) == NULL)
This may or may not correctly handle errors depending on how old your compiler is.
>*(StringPtr+i) = String[i];
You know, you can do
StringPtr[i] = String[i]; too. It's a lot easier to read and write.>cout << "StringPtr = " << StringPtr << endl;
You forgot the last character, the '\0'.
>delete [] String;
And why are you trying to delete an array?
>For "delete [] String;" part, I tried to use the for loop to see if it works.
Why don't you take that entire line away and see if it works? You're not supposed to release memory unless you explicitly allocate it.
New members chased away this month: 4
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
>*(StringPtr+i) = String[i];
You know, you can doStringPtr[i] = String[i];too. It's a lot easier to read and write.
>cout << "StringPtr = " << StringPtr << endl;
You forgot the last character, the '\0'.
Now, for StringPtr aspect, I agree with you about StringPtr[i] is easier to read and write than *(StringPtr+1). I tried StringPtr[i] and it still contains same data so I thought there must be something to fix from *(StringPtr+1) which is not true. Thank you. Now back to '\0'. What do you mean I forgot the last character, the '\0'?
I tried to add:
C++ Syntax (Toggle Plain Text)
if(String[i+1] == '\0') String[i+1] = String[i+1];
And when you
strcpy(String, "abcdefghijklmnopqrstuvxyz"); into a 26 character array like this you just blew past your array bounds because a 27th character (\0) is loaded too. Remember, a string in C always ends in \0 and that must be accounted for in the buffer. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
> strcpy(String, "abcdefghijklmnopqrstuvxyz");
> if((StringPtr = new char[26]) == NULL)
The alphabet, plus a \0 is 27 chars in total.
Both the array and your allocated memory are overstepping their limits.
That's why I use
for(i = 0; String[i] != '\0'; i++) so that way, it won't include the 27th char.For example:
C++ Syntax (Toggle Plain Text)
for(i = 0; String[i] != '\0'; i++) { cout << String[i]; StringPtr[i] = String[i]; cout << StringPtr[i] << " "; } cout << endl << "String = " << String << endl; cout << "StringPtr = " << StringPtr << endl;
==========================================
aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz
String = abcdefghijklmnopqrstuvwxyz
StringPtr = abcdefghijklmnopqrstuvwxyz²²²²½½½½½½½½ε■
==========================================
You can see the matched data between String and StringPtr from the for loop statement. Then why didn't StringPtr itself contains exactly datas it showed in the loop statement for the cout part.
![]() |
Similar Threads
- Array/String intersect and Array/Array intersect code (C#)
- pointer and array arithmetic (C)
- Is there a way to tokenize an array of strings (C++)
- multidimensional array (C)
- how to increase the size of an array? (C)
Other Threads in the C++ Forum
- Previous Thread: errase function usage
- Next Thread: Can't Read Input File
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







