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: Turyturbo is an unknown quantity at this point 
Solved Threads: 0
Turyturbo Turyturbo is offline Offline
Newbie Poster

delete array and assigns array to pointer

 
0
  #1
Mar 10th, 2007
Hello,
This is my first time using forum to ask around for some assistance.
I will make it briefly.
  1. void main()
  2. {
  3. char *StringPtr; //pointer of char
  4. char String[26]; //array of chars
  5.  
  6. strcpy(String, "abcdefghijklmnopqrstuvxyz");
  7. if((StringPtr = new char[26]) == NULL)
  8. cerr << "\n\a allocation failed" << endl;
  9. else
  10. cout << "allocation was successful" << endl;
  11.  
  12. for(i=0;String[i] != '\0'; ++i)
  13. *(StringPtr+i) = String[i];
  14.  
  15. cout << "String = " << String << endl;
  16. cout << "StringPtr = " << StringPtr << endl;
  17.  
  18. delete [] String; //C4154 Warning...
  19. delete [] StringPtr;
  20. }
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: delete array and assigns array to pointer

 
0
  #2
Mar 10th, 2007
>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 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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: Turyturbo is an unknown quantity at this point 
Solved Threads: 0
Turyturbo Turyturbo is offline Offline
Newbie Poster

Re: delete array and assigns array to pointer

 
0
  #3
Mar 10th, 2007
>*(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'.
That's what I thought about "delete [] String;". I commented this out.

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:
  1. if(String[i+1] == '\0')
  2. String[i+1] = String[i+1];
It still have same output. Am I missing something here?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: delete array and assigns array to pointer

 
0
  #4
Mar 10th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,121
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: delete array and assigns array to pointer

 
0
  #5
Mar 10th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: Turyturbo is an unknown quantity at this point 
Solved Threads: 0
Turyturbo Turyturbo is offline Offline
Newbie Poster

Re: delete array and assigns array to pointer

 
0
  #6
Mar 10th, 2007
Originally Posted by Salem View Post
> 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.
Correct. Alphabet contains 26 chars.
That's why I use for(i = 0; String[i] != '\0'; i++) so that way, it won't include the 27th char.

For example:
  1. for(i = 0; String[i] != '\0'; i++)
  2. {
  3. cout << String[i];
  4. StringPtr[i] = String[i];
  5. cout << StringPtr[i] << " ";
  6. }
  7. cout << endl << "String = " << String << endl;
  8. cout << "StringPtr = " << StringPtr << endl;
Output:
==========================================
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: delete array and assigns array to pointer

 
0
  #7
Mar 10th, 2007
> so that way, it won't include the 27th char.
That being the \0 character,
That being the character which stops cout from printing trailing garbage.

Your choice.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: Turyturbo is an unknown quantity at this point 
Solved Threads: 0
Turyturbo Turyturbo is offline Offline
Newbie Poster

Re: delete array and assigns array to pointer

 
0
  #8
Mar 11th, 2007
I figured it out.

Just remove for loop statement and use assignment operator:

StringPtr = String;

and remove:

delete[] StringPtr;

Now both are identical. Both's output are matched.

Thank you for giving some of your time to work out with me.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 2
Reputation: ThundrbltMN is an unknown quantity at this point 
Solved Threads: 0
ThundrbltMN ThundrbltMN is offline Offline
Newbie Poster

Re: delete array and assigns array to pointer

 
0
  #9
Apr 10th, 2009
You don't have enough memory for all the letters + the null character. Allocate one more character to each buffer!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC