C ++ pointer passed as function parameter

Source: Internet
Author: User

 

Today, when I write a function that converts string letters to uppercase and lowercase letters, the modification and modification are completed. The Code is as follows:
# Include "stdafx. H"
# Include <string. h>
# Include <iostream. h>
Char * transform (char * CSTR, char * STR, int length );

Int main (INT argc, char * argv [])
{
Char * STR = "woaini38 ";
Cout <STR <Endl;
Char s [10];
Transform (S, STR, strlen (STR ));
Cout <S <Endl;
Return 0;
}

Char * transform (char * CSTR, char * STR, int length)
{
For (INT I = 0; I <Length + 1; I ++)
{
If (STR [I]> = 'A' & STR [I] <= 'Z ')
{
CSTR [I] = STR [I] + 'a'-'A ';
Continue;
}
If (STR [I]> = 'A' & STR [I] <= 'Z ')
{
CSTR [I] = STR [I]-'A' + 'a ';
}
Else
{
CSTR [I] = STR [I];
}
}
Return CSTR;
}

Ah! I admit that my foundation is not as good as I think. Make a mistake when writing this code.
Originally, we wanted to define transform as (char * CSTR, char * Str ). Then, in the function body, allocating memory dynamically for the CSTR industry is the target pointer, so I wrote:

Char * transform (char * CSTR, char * Str)
{

Int Len = strlen (STR );
CSTR = new char [Len]; // dynamically allocates a len-length memory.

For (INT I = 0; I <Len + 1; I ++) // The following is the same
{
If (STR [I]> = 'A' & STR [I] <= 'Z ')
{
CSTR [I] = STR [I] + 'a'-'A ';
Continue;
}
If (STR [I]> = 'A' & STR [I] <= 'Z ')
{
CSTR [I] = STR [I]-'A' + 'a ';
}
Else
{
CSTR [I] = STR [I];
}
}
Return CSTR;

}

However, a packet memory error occurs during running, indicating that a block of memory cannot be read, or a block of memory cannot be written. After programming for so long, I immediately thought that this error should be caused by a null pointer. I checked it carefully but did not find it. During the single-step operation, it was known that the CSTR of transform had a problem. But I have allocated dynamic memory for him! I am sad. I can't find the reason. I tried another method, so I deleted the dynamically allocated statement: But the variable of the string length is required during the loop. So I passed it a length variable. Then, in the main function, I can define only one array. Then the transform parameter is passed. So you can see the code at the beginning.
Then everything went well.
But I have never figured out why I cannot pass a null pointer to transform and then dynamically allocate memory in the function body?
On the internet I see such an article http://99381837.iteye.com/blog/905237

The analysis by the author is quite good. He told me that if I want to pass a null pointer, I should write char * transform (char ** CSTR, char * Str) {* CSTR = (char *) malloc (strlen (STR) + 1 );}
The analysis later is painful. It cannot be completely viewed.
In fact, writing this article is to prevent yourself from making such a mistake. But this error reminds me of a programming habit. Try not to use a null pointer. However, in programming, we sometimes do not know that we are using a null pointer.
So I wrote the following code in VC:
# Include "stdafx. H"
# Include <iostream. h>
# Include <string. h>

Int main (INT argc, char * argv [])
{
Char * STR = "woaini ";
Char * s;
Strcmp (S, STR );
Cout <S <Endl;
Return 0;
}

At this time, I guess you should know how to write this code.
In fact, an error is ignored when a null pointer is used as a parameter. If you want to dynamically allocate memory in the function body, when will the memory you allocate be released? The dynamically allocated memory needs to be manually released. This is also a problem, right. Therefore, null pointers cannot be passed as parameters.
In most MFC programming, parameters like strings are cstring objects. Sometimes when you need some functions of the SDK, if the parameter is a pointer, you must remember to allocate the memory without errors.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.