Leetcode--c language pointers

Source: Internet
Author: User

Although the C language for a long time, I think the pointer is also a good learning. Recently wrote O-c, recently began to regain C language, found themselves about the stack in C is not fully understood.

function malloc in C:

struct listnode* head = (struct listnode *)malloc(sizeof(struct listnode));

malloc () obtains space from the heap and assigns the address of the heap space to the pointer. That is, the pointer returned by the function is a piece of memory pointing to the heap. The operating system has a linked list that records the free memory address. When the operating system receives a request for a program, it iterates through the list and then finds the heap node where the first space is larger than the requested space, and then deletes the node from the list of idle nodes and assigns the space of that node to the program.

Heap is a common space for everyone , divided into global heap and local heap. The global heap is all unallocated space, and the local heap is the space allocated by the user. The heap is allocated when the operating system initializes the process, and it can be added to the system during the run, but remember to return it to the operating system, or memory leaks.

The stack is thread-unique , saving its running state and local automatic variables. The stack is initialized at the beginning of the thread, and the stacks are independent of each other. Each function has its own stack, which is used to pass arguments between functions. The operating system automatically switches the stack when switching threads, or switches the SS/ESP register. Stack space does not require explicit allocation and deallocation in high-level languages.

Therefore, when we want the function to return an array, a linked list and other data structures, we need to dynamically request memory (Request heap memory). Because the normal stack memory (which holds the running state of the current function, the local variable) is automatically released after the program returns a return. The contents of the pointer we return are also released, and if we go on to the memory that has been freed, it will cause a memory leak, access illegal, and so on, causing the program to crash.

Let me tell you about the mistakes I made before.

function needs to return a list of links (n nodes known)

//Cur1 and CUR2 are two nodes, and the values of the 2 node values are the value of the current node structlistnode* head = (structListNode *)malloc(sizeof(structListNode)); intVal=0; intdigit =0; Pre=Head; //allocate space before reuse (address value is rvalue)         while(Cur1 &&CUR2) {            //rear = new (struct listnode);Rear = (structListNode *)malloc(sizeof(structListNode)); Val= Cur1->val + Cur2->val +Digit; Rear->val = val%Ten;//Standarddigit = val/Ten; Cur1= cur1->Next; CUR2= cur2->Next; Pre->next =Rear; Pre=Rear; }

A single node of the application, and then connect them together. Here, you need to request space for the node, and then point to the next pointer to the previous node.

My mistakes are:

 structlistnode* head = (structListNode *)malloc(sizeof(structListNode)); intVal=0; intdigit =0; Pre=Head;  while(Cur1 &&CUR2) {Val= Cur1->val + Cur2->val +Digit; Pre->val =Val; Cur1= cur1->Next; CUR2= cur2->Next; Pre= pre->next;//The error is here. Pre->next is null, the pre is null after assignmentPre= (structListNode *)malloc(sizeof(structListNode));//the pre points to a heap of memory, but at this point no node points to the node where the pre is located
}

It's right to change the second sentence.

// Allocate space First (address value is rvalue) ! pre->next= (struct listnode *)malloc(sizeof(struct listnode));
Pre = pre->next;

Leetcode--c language pointers

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.