Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ALP4-Tutorials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mariah15
ALP4-Tutorials
Commits
52654272
Commit
52654272
authored
2 years ago
by
Mactavish
Browse files
Options
Downloads
Patches
Plain Diff
add dynamic memory management
parent
bcfdfeb0
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
clang.md
+69
-0
69 additions, 0 deletions
clang.md
with
69 additions
and
0 deletions
clang.md
+
69
−
0
View file @
52654272
...
...
@@ -384,6 +384,75 @@ int main(void)
}
```
### Dynamic Memory Allocation
C supports function for heap management:
-
`malloc`
: allocate a block of
**uninitialized**
memory
-
`calloc`
: allocate a block of
**zeroed**
memory
-
`free`
: free previously allocated block of memory
-
`realloc`
: change size of previously allocated block (
**careful**
- it might move!)
The following is an example of binary tree implementation using dynamic memory allocation:
```
c
#include
<stdio.h>
#include
<stdlib.h>
struct
TreeNode
{
int
val
;
struct
TreeNode
*
left
;
struct
TreeNode
*
right
;
};
struct
TreeNode
*
create_node
(
int
val
)
{
struct
TreeNode
*
node
=
(
struct
TreeNode
*
)
malloc
(
sizeof
(
struct
TreeNode
));
node
->
val
=
val
;
node
->
left
=
NULL
;
node
->
right
=
NULL
;
return
node
;
}
// Traverse the tree in-order and print the values
void
traverse
(
struct
TreeNode
*
node
)
{
if
(
node
==
NULL
)
{
return
;
}
traverse
(
node
->
left
);
printf
(
"%d "
,
node
->
val
);
traverse
(
node
->
right
);
}
struct
TreeNode
*
insert
(
struct
TreeNode
*
root
,
int
val
)
{
if
(
root
==
NULL
)
{
return
create_node
(
val
);
}
if
(
val
<
root
->
val
)
{
root
->
left
=
insert
(
root
->
left
,
val
);
}
else
{
root
->
right
=
insert
(
root
->
right
,
val
);
}
return
root
;
}
int
main
()
{
struct
TreeNode
*
root
=
NULL
;
root
=
insert
(
root
,
5
);
root
=
insert
(
root
,
3
);
root
=
insert
(
root
,
7
);
root
=
insert
(
root
,
2
);
root
=
insert
(
root
,
4
);
root
=
insert
(
root
,
6
);
root
=
insert
(
root
,
8
);
traverse
(
root
);
return
0
;
}
```
> There is a problem for this program, can you find out?
## C Compilation Process
Unlike other interpreted programming languages, we use compilers to compile C written programs.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment