0% found this document useful (0 votes)
3 views

LectureNote_05

The document covers advanced concepts in C programming, focusing on memory allocation techniques using pointers, including malloc and calloc. It also discusses the use of structures to manage different data types efficiently. Key takeaways include the importance of freeing allocated memory and the utility of structures in organizing complex data.

Uploaded by

denny020908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LectureNote_05

The document covers advanced concepts in C programming, focusing on memory allocation techniques using pointers, including malloc and calloc. It also discusses the use of structures to manage different data types efficiently. Key takeaways include the importance of freeing allocated memory and the utility of structures in organizing complex data.

Uploaded by

denny020908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Embedded Data Structure

(Lecture Note 5)

Revisit C Programming (4)

2025. 3. 18

Prof. Wonjun Kim


School of Electrical and Electronics Engineering

Deep Computer Vision Lab.


Review of the Last Class
Content
 Basic concept of advanced operations for pointers
• Pointer and string
- Inputs of functions related to string are pointers in general
- Remember the relation between pointer of pointer and string

• Pointer and its applications


- Array of pointers, function pointer, and array of function pointers

• Remember how to read/write a file

Deep Computer Vision Lab.


1
Memory Allocation (1/4)
Content
 Dynamic management with pointers
• Two basic types of memory allocation
- int *p = (int *)malloc(height*width*sizeof(int));
Consider the return type
- int *p = (int *)calloc(height*width, sizeof(int));

※ malloc  initialize space with garbage values, calloc  0

* Free the memory if you want to …

int *p = (int *)calloc(height*width, sizeof(int));

for(y = 0; y < height*width; y++){


p[y] = 9;
}

free(p); Free the “address” (i.e., another pointer can reserve it !)

Deep Computer Vision Lab.


2
Memory Allocation (2/4)
Content
 Dynamic management with pointers – cont’d
• Interpret the allocation procedure with drawing
- For example, consider a specific data whose size 9x9

float *data = (float *)calloc(9*9, sizeof(float));


data 1
// load data using “data”
// compute the average 2
for(y = 0; y<9*9; y++){ 3
avg += data[y];
} 4
avg /= (9*9);

free(data); Where is our data stored ?

※ If we do not free the memory space, what happens ? (draw it !)

Deep Computer Vision Lab.


3
Memory Allocation (3/4)
Content
 Allocation with pointer of pointers
• If you want to reserve the memory with pointer of pointers …
- Try to conduct memory allocation by using “float **data”
(For example, 9x39 data is stored in a two-dimensional array)

float **data =

Hint : use the for loop

※ Draw the overall procedure as follows :

data 7

Deep Computer Vision Lab.


4
Memory Allocation (4/4)
Content
 Simple implementation example
• Compute the average of your own data (input by keyboard)

#include <stdio.h>
#include <stdlib.h>

void main(){
int len;
printf(“Input your square matrix size :”);
scanf(“%d”, &len);

// memory allocation using “int **matrix”

// compute the average

Deep Computer Vision Lab.


5
Structure (1/5)
Content
 Structure including a set of different types of data
• “struct” instruction

struct Tag{ ※ What is the difference btw.


Data_type member 1; array and structure ?
Data_type member 2;

};

struct ProgramClass{ Tag (it is not a variable !)


char name[100];
int ID;
int score; Structure member
float attendance;
float grade; struct ComplexNum{
}; float re;
float im;
Example of structures };

Deep Computer Vision Lab.


6
Structure (2/5)
Content
 Initialization
• Syntax and initialization scheme
Difference with “*name” ?
#include <stdio.h> struct ProgramClass{
char name[100];
void main() int ID;
{ int score;
struct ProgramClass s1; }s1;
struct ProgramClass s2, s3;

s1 = {“Kim”, 1, 90, 1, 4.3}; struct ProgramClass{


} char name[100];
int ID;
int score;
K i m \0 … 1 90 4.3 }s1 = {“Kim”, 1, 90};

In your memory space … [ Other initialization strategies ]

Deep Computer Vision Lab.


7
Structure (3/5)
Content
 Referencing
• We can access the structure member using “.” as follows:

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h> struct ComplexNum{
float re;
void main() float im;
{ };
struct ProgramClass s1;
struct ProgramClass s2, s3; void main()
{
strcpy(s1.name, “Kim”);
s1.ID = 1;
s1.score = 90;
Make your code !
s1.attendance = 1.0;
s1.grade = 4.3;
} }

Deep Computer Vision Lab.


8
Structure (4/5)
Content
 Structure with typedef
• We can redefine data type (or structure) efficiently
: For example, typedef unsigned char BYTE
: For example, typedef struct student STUDENT

#include <stdio.h> typedef struct ProgramClass{


char name[100];
typedef unsigned char BYTE; int ID;
int score;
void main() float attendance;
{ float grade;
BYTE val; }ProgramClass;
}
Is it Ok ?
Typedef struct student STUDENT; Then, main advantage of
typedef is …
STUDENT s;
Deep Computer Vision Lab.
9
Structure (5/5)
Content
 Practical example
• Complex multiplication using typedef and structure

#include <stdio.h> void main()


{
Typedef struct _complex{ CNum a = {1.3, 3.9};
float re; CNum b = {-3.3, 8.4};
float im; CNum result;
}CNum;

CNum multiply(CNum a, CNum b)


{ }
CNum c;

Check the result and print out


}

Deep Computer Vision Lab.


10
Summary
Content
 Memory allocation and structure
• Memory allocation : key technique in embedded systems
- Remember the usage of “malloc” and “calloc”
- Remember the memory allocation based on pointer of pointer

• Structure : a kind of cluster containing different modalities


- Very useful to handle data of different modalities in a unified way
- Key technique in considering the data structure

Deep Computer Vision Lab.


14

You might also like