0% found this document useful (0 votes)
34 views21 pages

1.CPE6 Unit 2

This document discusses data structures and algorithms in computer engineering. It covers pointers, dynamic memory allocation, arrays, and structures. It provides instructions on installing C++ and writing sample code. Pointers are defined as variables that store memory addresses. Dynamic memory allocation commands like new and delete are explained for allocating and freeing memory during runtime. Arrays can be allocated using new[] and freed with delete[]. Structures are explained as user-defined types that can group different data types under a single name.

Uploaded by

Angel Aquino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views21 pages

1.CPE6 Unit 2

This document discusses data structures and algorithms in computer engineering. It covers pointers, dynamic memory allocation, arrays, and structures. It provides instructions on installing C++ and writing sample code. Pointers are defined as variables that store memory addresses. Dynamic memory allocation commands like new and delete are explained for allocating and freeing memory during runtime. Arrays can be allocated using new[] and freed with delete[]. Structures are explained as user-defined types that can group different data types under a single name.

Uploaded by

Angel Aquino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Computer Engineering - CPE6

Data
Structures and
Algorithms
Pointers, Dynamic Memory Allocation,
Arrays, Structures

Week 2
Computer Engineering - CPE6

Contents of this presentation

Here’s what you’ll find in this presentation:

1.Intallation of C++ programming language


2.Pointers
3.Dynamic Memory Allocation and Arrays
4.Structures

Week 2
Computer Engineering - CPE6

Intallation of C++

/01 Install CodeBlocks /02 Add to path


Copy the address of bin and
Just type code blocks and hit
paste it in environment
downloads
variables

/03 Write sample code /04 Compile and run


Write a simple hello world
In command line, compile
program(C++ syntax) in
and run your code.
your text editor

Week 2
Computer Engineering - CPE6

Week 2
Computer Engineering - CPE6

Pointers
A pointer is a variable whose value is the address of
another variable

Week 2
Computer Engineering - CPE6

&
symbol Variable Value Address
It returns the address of a variable if used
x 10 0x12345
before the variable name.

Usage:
int x = 10;

cout<< &x;

Week 2
Computer Engineering - CPE6

* symbol
Variable Value Address
It returns the value of a variable if used
before an address. x 10 0x12345

Usage:
int x = 10;

cout<< *(&x);

Week 2
Computer Engineering - CPE6

* symbol
Variable Value Address
It returns the value of a variable if used
before an address. x 10 0x12345

Usage: ptr 0x12345 0x98765


int x = 10;
int *ptr = &x;

cout<< *ptr;
cout<< ptr;

Week 2
Computer Engineering - CPE6

Points to remember when using Pointers


• & symbol is used to get the address of a variable.
• * symbol is used to create a pointer variable.(i.e. *ptr)
• * can be used to retrieve a value when used before an
address.(i.e. *(&x) )
• A pointer variable has a separate address that is different
from its value.

Week 2
Computer Engineering - CPE6

Dynamic Memory
Allocation and Arrays
Dynamic Memory Allocation is the process of assigning the memory
space during the execution time or run time.

Week 2
Computer Engineering - CPE6

Memory allocation commands


C++ has a variety of commands for memory management:

1. new
2. delete
3. new[]
4. delete[]

Week 2
Computer Engineering - CPE6

new
The new keyword is used to allocate a single object on the heap. Here’s how
you implement it.

int *num = new int;

Week 2
Computer Engineering - CPE6

delete
The delete keyword is used to deallocate memory that was previously allocated
using new. Here’s how you implement it.

delete num;

Week 2
Computer Engineering - CPE6

new[ ]
It is used to allocate memory for an array of objects on the heap.

int *arr = new int[10];

Week 2
Computer Engineering - CPE6

delete[ ]
It is used to deallocate memory of the previous allocated memory using new[].

delete[] arr;

Week 2
Computer Engineering - CPE6

Example:
#include<iostream>
using namespace std;

int main() {
int *ptr = new int;
int *arr = new int[10];

//your code here


delete ptr;
delete arr[];
return 0;
}

Week 2
Computer Engineering - CPE6

Things to remember when implementing


DMA

Initialize the Loop over the


Pointer variable
pointer pointer

Set the pointer’s size based Access the elements of the


Declare a pointer variable.
on the user input. pointer.

Week 2
Computer Engineering - CPE6

Structure in C+
+
Structure is user-defined type which contains a collection
of variables with different data types under a single
name.

Week 2
Computer Engineering - CPE6

Example of Structure in C++


#include <iostream>
#include <string>
using namespace std;

struct Person {
    string fname;
    string lname;
    int age;
};

int main() {
    Person person1;
    person1.fname = "John";
    person1.lname = "Smith";
    person1.age = 30;
    cout << "Name: " << person1.fname + " " + person1.lname<< endl;
    cout << "Age: " << person1.age << endl;

    return 0;
}

Week 2
Computer Engineering - CPE6

Example of Structure in C++


using namespace std;
#include <iostream>
#include <string>

struct Person {
    string name;
    int age;
};

int main() {
    Person people[3];
    people[0] = {"Alice", 25};
    people[1] = {"Bob", 30};
    people[2] = {"Charlie", 28};
    for (int i = 0; i < 3; ++i) {
        cout << "Person " << i + 1 << ": Name = " << people[i].name << ", Age =
" << people[i].age << endl;
    }
    return 0;
}

Week 2
Computer Engineering - CPE6

THANKS!
DO YOU HAVE ANY QUESTIONS?

Louievic Samortin Sancon

Week 2

You might also like