Home > Backend Development > C++ > body text

How do pointers implement dynamic memory allocation?

王林
Release: 2024-06-05 13:13:05
Original
760 people have browsed it

Pointers and dynamic memory allocation: Pointers are features in programming languages ​​used to store the address of another block of memory. By using pointers, the required memory can be allocated as needed at runtime. Use an allocator function such as malloc() or new to store the memory address in a pointer variable. Practical case: Use pointers to dynamically allocate an array to store student grades read from a text file.

How do pointers implement dynamic memory allocation?

Pointers and Dynamic Memory Allocation

A pointer is a programming language feature that stores an address pointing to another piece of memory. By using pointers, we can achieve dynamic memory allocation, which allocates memory as needed at runtime.

Principle

When a pointer variable is created, it will point to an area of ​​memory that has not yet been allocated. To allocate memory we need to use an allocator function like malloc() or new. The allocator function returns the address of a new memory block of the specified size and stores it in a pointer variable.

Syntax

##C/C++

int *ptr; // 声明一个指向 int 型变量的指针
ptr = (int *) malloc(sizeof(int)); // 分配 sizeof(int) 大小的内存并存储地址到 ptr
Copy after login

Java

int[] arr; // 声明一个指向 int 型数组的指针
arr = new int[10]; // 分配大小为 10 的数组并存储地址到 arr
Copy after login

Practical Case

Suppose we have a text file

grades.txt that contains student grades. We are going to create a program that reads this file and stores the grades in a dynamically allocated array.

C++ code

#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ifstream file("grades.txt");
  int numStudents;
  file >> numStudents;

  // 分配一个具有 numStudents 个元素的数组
  int *grades = new int[numStudents];

  // 读取文件并存储成绩
  for (int i = 0; i < numStudents; i++) {
    file >> grades[i];
  }

  // 打印成绩
  for (int i = 0; i < numStudents; i++) {
    cout << grades[i] << " ";
  }
  cout << endl;

  // 释放动态分配的内存
  delete[] grades;

  return 0;
}
Copy after login

Java code

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) throws IOException {
    File file = new File("grades.txt");
    int numStudents = Integer.parseInt(Files.readAllLines(Paths.get(file.getPath())).get(0));

    // 分配一个具有 numStudents 个元素的数组
    int[] grades = new int[numStudents];

    // 读取文件并存储成绩
    for (int i = 0; i < numStudents; i++) {
      grades[i] = Integer.parseInt(Files.readAllLines(Paths.get(file.getPath())).get(i + 1));
    }

    // 打印成绩
    for (int i = 0; i < numStudents; i++) {
      System.out.print(grades[i] + " ");
    }
    System.out.println();
  }
}
Copy after login

The above is the detailed content of How do pointers implement dynamic memory allocation?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!