Realloc Invalid Pointer Error Parsing

Source: Internet
Author: User
Realloc Invalid Pointer Error

Char * temp = (char *) realloc (SRC, sizeof (char) * 100 );

As shown in the above lineCode. The error occurs because SRC points to a non-null or heap address.

The specific requirements of the realloc function are as follows:

1. src = NULL

2. the SRC pointer must be malloc (), calloc (), or realloc () allocated

 

Code that causes errors

The error code is generally in the following format:

Char * src = "Hello world! "; // Error code

Or

Char SRC [100]; // error code

The above two methods are used to allocate memory in the stack (if SRC is a local variable) or static area (if SRC is a global variable ). If SRC is passed to realloc, The realloc Invalid Pointer Error will occur.

 

Solution:

1. Use SRC to display allocated memory with malloc

Char * src = "Hello world! ";// Error code

You can replace it with the following code:

Char * src = (char *) malloc (strlen ("Hello world! "));

Strcpy (SRC, "Hello world! "); // The string. h header file is used.

 

Char SRC [2, 100];// Error code

You can replace it with the following code:

Char * src = (char *) malloc (sizeof (char) * 100 );

 

2. Set SRC to null

Char * src = NULL;

This article is original by Ladd. For more information, see the source.

Http://www.cnblogs.com/ladd/archive/2012/07/02/2572112.html

Realloc Invalid Pointer Error

Char * temp = (char *) realloc (SRC, sizeof (char) * 100 );

For example, the above line of code may contain errors in the title. The error occurs because SRC points to a non-null or heap address.

The specific requirements of the realloc function are as follows:

1. src = NULL

2. the SRC pointer must be malloc (), calloc (), or realloc () allocated

 

Code that causes errors

The error code is generally in the following format:

Char * src = "Hello world! "; // Error code

Or

Char SRC [100]; // error code

The above two methods are used to allocate memory in the stack (if SRC is a local variable) or static area (if SRC is a global variable ). If SRC is passed to realloc, The realloc Invalid Pointer Error will occur.

 

Solution:

1. Use SRC to display allocated memory with malloc

Char * src = "Hello world! ";// Error code

You can replace it with the following code:

Char * src = (char *) malloc (strlen ("Hello world! "));

Strcpy (SRC, "Hello world! "); // The string. h header file is used.

 

Char SRC [2, 100];// Error code

You can replace it with the following code:

Char * src = (char *) malloc (sizeof (char) * 100 );

 

2. Set SRC to null

Char * src = NULL;

This article is original by Ladd. For more information, see the source.

Http://www.cnblogs.com/ladd/archive/2012/07/02/2572112.html

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.