0% found this document useful (0 votes)
33 views2 pages

Ass 2

Uploaded by

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

Ass 2

Uploaded by

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

CS580U

Assignment # 2

Deadline Sunday Oct 12, 2024, Midnight (See GitHub Submission Instructions Below)

(1) Write a simple C program that demonstrates the use of pointers and structures to represent
different types of vehicles (like cars, trucks, and vans). This program also contains a memory
error that you can detect using Valgrind (as discussed in the class). Details should be as under:

1. Structures and Pointers:


o A Vehicle structure is defined to hold the type of vehicle and the number of wheels.
o Pointers are used to dynamically allocate memory for vehicle objects and their
string types.
2. Memory Management:
o The program allocates memory for both the Vehicle structure and its type string.
o It correctly frees the memory for trucks and vans, but it intentionally forgets to free
cars to create a memory leak.
3. Valgrind:
o You should compile your program and show detailed info about vehicles.
o Execute valgrind to show the memory leak report.
o Fix memory leak and show 2nd report after executing valgrind.

(2) You need to implement an Animal struct with the associated functions demonstrates how to
manage dynamic memory in C. By incorporating proper memory management techniques
and using Valgrind, you can ensure that your code is free from memory leaks and that you
safely manage allocated memory.

1. Define Animal struct with properties such as name and age. The name will be
dynamically allocated.
2. Implement the three functions:

- new_Animal: Here, allocate and initialize a new Animal.


- copy_Animal: Here, creates a copy of an existing Animal.
- del_Animal: Here, frees the allocated memory for an Animal

3. Write down the main function where call the above functions. The output will be look
like as follows:

Animal 1: Name = Goat, Age = 3


Animal 2: Name = Goat, Age = 3

4. Submit a memory leak report using valgrind – freeing memory without any leak will
carry significant weight.
This is the GitHub link to submit Ass 1: https://classroom.github.com/a/l2r-_95G
Step 1: Click on the above link if you already have a GitHub account, if NOT create your own
GitHub account, then click on the above link and join the classroom.
Step 2: After opening the link above accept the test Assignment.
Step 3: Click on the newly appeared GitHub link and see the attached PDF.
Step 4: Upload your C program on GitHub using the Add file tab.

You might also like