954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

delete array and assigns array to pointer

Hello,
This is my first time using forum to ask around for some assistance.
I will make it briefly.

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;
}



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!

Turyturbo
Newbie Poster
6 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>*(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:

if(String[i+1] == '\0')
            String[i+1] = String[i+1];



It still have same output. Am I missing something here?

Turyturbo
Newbie Poster
6 posts since Mar 2007
Reputation Points: 10
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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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, astring in C always ends in \0 and that must be accounted for in the buffer.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
> 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:

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;



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.

Turyturbo
Newbie Poster
6 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

> 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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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. :)

Turyturbo
Newbie Poster
6 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

You don't have enough memory for all the letters + the null character. Allocate one more character to each buffer!

ThundrbltMN
Newbie Poster
2 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You