- By Julien Barbier
- Weight: 1
- Ongoing second chance project - started 09-23-2021, must end by 09-25-2021 (in about 16 hours) - you're done with 0% of tasks.
- QA review fully automated.
- Auto QA review: 0.0/36 mandatory & 0.0/21 optional
- Altogether: 0.0%
- Mandatory: 0.0%
- Optional: 0.0%
- Calculation: 0.0% + (0.0% * 0.0%) == 0.0%
For this project, students are expected to look at this concept:
Read or watch:
man or help:
exit (3)
calloc
realloc
At the end of this project, you are expected to be able to explain to anyone, without the help of Google:
- How to use the
exit
function - What are the functions
calloc
andrealloc
from the standard library and how to use them
- Allowed editors:
vi
,vim
,emacs
- All your files will be compiled on Ubuntu 20.04 LTS using
gcc
, using the options-Wall -Werror -Wextra -pedantic -std=gnu89
- All your files should end with a new line
- A
README.md
file, at the root of the folder of the project is mandatory - Your code should use the
Betty
style. It will be checked using betty-style.pl and betty-doc.pl - You are not allowed to use global variables
- No more than 5 functions per file
- The only C standard library functions allowed are
malloc
,free
andexit
. Any use of functions likeprintf
,puts
,calloc
,realloc
etc... is forbidden - You are allowed to use _putchar
- You don't have to push
_putchar.c
, we will use our file. If you do it won't be taken into account - In the following examples, the
main.c
files are shown as examples. You can use them to test your functions, but you don't have to push them to your repo (if you do we won't take them into account). We will use our ownmain.c
files at compilation. Ourmain.c
files might be different from the one shown in the examples - The prototypes of all your functions and the prototype of the function
_putchar
should be included in your header file calledmain.h
- Don't forget to push your header file
--
#advanced
Score: 0.00% (Checks completed: 0.00%)
Write a function that reallocates a memory block using malloc
and free
- Prototype:
void *_realloc(void *ptr, unsigned int old_size, unsigned int new_size);
- where
ptr
is a pointer to the memory previously allocated with a call tomalloc
:malloc(old_size)
old_size
is the size, in bytes, of the allocated space forptr
- and
new_size
is the new size, in bytes of the new memory block - The contents will be copied to the newly allocated space, in the range from the start of
ptr
up to the minimum of the old and new sizes - If
new_size
>old_size
, the "added" memory should not be initialized - If
new_size
==old_size
do not do anything and returnptr
- If
ptr
isNULL
, then the call is equivalent tomalloc(new_size)
, for all values ofold_size
andnew_size
- If
new_size
is equal to zero, andptr
is notNULL
, then the call is equivalent tofree(ptr)
. ReturnNULL
- Don't forget to free
ptr
when it makes sense
FYI: The standard library provides a different function: realloc
. Run man realloc
to learn more.
julien@ubuntu:~/0x0b. more malloc, free$ cat 100-main.c
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* simple_print_buffer - prints buffer in hexa
* @buffer: the address of memory to print
* @size: the size of the memory to print
*
* Return: Nothing.
*/
void simple_print_buffer(char *buffer, unsigned int size)
{
unsigned int i;
i = 0;
while (i < size)
{
if (i % 10)
{
printf(" ");
}
if (!(i % 10) && i)
{
printf("\n");
}
printf("0x%02x", buffer[i]);
i++;
}
printf("\n");
}
/**
* main - check the code for
*
* Return: Always 0.
*/
int main(void)
{
char *p;
int i;
p = malloc(sizeof(char) * 10);
p = _realloc(p, sizeof(char) * 10, sizeof(char) * 98);
i = 0;
while (i < 98)
{
p[i++] = 98;
}
simple_print_buffer(p, 98);
free(p);
return (0);
}
julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-realloc.c -o 100-realloc
julien@ubuntu:~/0x0b. more malloc, free$ ./100-realloc
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62
julien@ubuntu:~/0x0b. more malloc, free$
Repo:
- GitHub repository:
alx-low_level_programming
- Directory:
0x0C-more_malloc_free
- File:
100-realloc.c
#advanced
Score: 0.00% (Checks completed: 0.00%)
Write a program that multiplies two positive numbers.
- Usage:
mul num1 num2
num1
andnum2
will be passed in base 10- Print the result, followed by a new line
- If the number of arguments is incorrect, print
Error
, followed by a new line, and exit with a status of98
num1
andnum2
should only be composed of digits. If not, printError
, followed by a new line, and exit with a status of98
- You are allowed to use more than 5 functions in your file
You can use bc
(man bc
) to check your results.
julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-mul.c _putchar.c -o 101-mul
julien@ubuntu:~/0x0b. more malloc, free$ ./101-mul 10 98
980
julien@ubuntu:~/0x0b. more malloc, free$ ./101-mul 235234693269436436223446526546334576437634765378653875874687649698659586695898579 28658034365084365083426083109679137608216408631430814308651084650816406134060831608310853086103769013709675067130586570832760732096730978014607369739567864508634086304807450973045703428580934825098342095832409850394285098342509834209583425345267413639235755891879970464524226159074760914989935413350556875770807019893069201247121855122836389417022552166316010013074258781583143870461182707893577849408672040555089482160343085482612348145322689883025225988799452329290281169927532160590651993511788518550547570284574715925006962738262888617840435389140329668772644708
6741363923575589187997046452422615907476091498993541335055687577080701989306920124712185512283638941702255216631601001307425878158314387046118270789357784940867204055508948216034308548261234814532268988302522598879945232929028116992753216059081057377926651337612618248332113256902485974371969385156015068813868274000683912187818601667058605418678284322237297213673482412392922068159291496274311170208689056585352782844484721140846367741649962638649229509281867896067208474178402156294978940712959518351846413859141792380853313812015295333546716634344284086426775480775747808150030732119704867805688704303461042373101473485092019906795014369069932
julien@ubuntu:~/0x0b. more malloc, free$
Repo:
- GitHub repository:
alx-low_level_programming
- Directory:
0x0C-more_malloc_free
- File:
101-mul.c