Heap memory or stack memory?

Source: Internet
Author: User

Heap memory or stack memory?

There is a question in "Sword refers to Offer", "replacing the space in a character array with the string" % 20 "". I read the ideas in the book, when too many spaces are entered by the program, the error "memory clobbered before allocated block" will be displayed ". The reason is that the stack memory and heap memory allocation are not clear. The error code is as follows:

# Include
 
  
# Include
  
   
# Include
   
    
Void replaceSpace (char string []); int main () {char string [] = "This is a test! "; // Character array, allocating memory replaceSpace (string); printf (" % s ", string); free (string); return 0;} in the stack memory area ;} void replaceSpace (char string []) {int index, spaceNum; size_t OriginLen, FinalLen; int OriginIndex, FinalIndex; index = 0; spaceNum = 0; OriginLen = 0; while (string [index]! = '\ 0') {if (string [index] = '') spaceNum ++; OriginLen ++; index ++;} FinalLen = OriginLen + spaceNum * 2; string = realloc (string, FinalLen); // The realloc function allocates memory OriginIndex = OriginLen In the heap memory area; FinalIndex = FinalLen; while (OriginIndex> = 0) {if (string [OriginIndex]! = '') {String [FinalIndex] = string [OriginIndex];} else {string [FinalIndex] = '0'; string [-- FinalIndex] = '2 '; string [-- FinalIndex] = '%';} -- FinalIndex; -- OriginIndex ;}}
   
  
 

The second line of the program defines and initializes an array of Characters string. Its memory allocation is automatically completed by the system and allocated on the stack.

When I want to replace the space in the string with "% 20" in the replaceSpace function, I need to extend the memory for it. This is what I think of as the realloc function. Unfortunately, the realloc function operates on the heap memory,

Our string is allocated on the stack, so the realloc function cannot find the address corresponding to the string on the stack, let alone extend the memory for it, so this function returns a null value, this is the cause of the error.

Solution:

1. Initially, the string character array is given enough space, which is not re-extended in the replaceSpace function.

char string[100] = "this is a test!";

2. Initially use the malloc function to allocate space to the string.

int main(){     char * string;     string = (char*)malloc(strlen("test")*sizeof(char));     strncpy(string,"test",strlen("test"));     ...    }void replaceSpace(char string[]){    string = realloc(string,FinalLen);    ...    if(string != NULL)    {        ...    }    else    {        printf("alloc memory error!\n");    }    ...}


PS. For details about stack and heap memory allocation, refer:

Http://bbs.csdn.net/topics/390147637

There are already too many other

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.