The malloc function is used to allocate a certain amount of memory during the execution of a program. It will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory and malloc will return a pointer to the reserved space.
When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free.
When you declare variables or when you use strings within double quotes, the program is taking care of all the memory allocation. You do not have to think about it.
/**
* cisfun - function used for concept introduction
* @n1: number of projects
* @n2: number of tasks
*
* Return: nothing.
*/
void cisfun(unsigned int n1, unsigned int n2)
{
int n;
char c;
int *ptr;
char array[3];
}The malloc function allocates a specific number of bytes in memory and returns a pointer to the allocated memory. This memory will have read and write permissions.
- Prototype:
void *malloc(size_t size); - where
void *means it is a pointer to the type of your choice - and
sizeis the number of bytes your need to allocate
#include <stdio.h>
#include <stdlib.h>
/**
* main - introduction to malloc and ree
*
* Return: 0.
*/
int main(void)
{
char *str;
str = malloc(sizeof(char) * 3);
str[0] = 'O';
str[1] = 'K';
str[2] = '\0';
printf("%s\n", str);
return (0);
}
// OUTPUT
OKWrite a function that creates an array of chars, and initializes it with a specific char.
- Prototype:
char *create_array(unsigned int size, char c); - Returns
NULLif size =0 - Returns a pointer to the array, or
NULLif it fails
Write a function that returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter.
- Prototype:
char *_strdup(char *str); - The
_strdup()function returns a pointer to a new string which is a duplicate of the stringstr. Memory for the new string is obtained withmalloc, and can be freed withfree. - Returns
NULLif str = NULL - On success, the
_strdupfunction returns a pointer to the duplicated string. It returnsNULLif insufficient memory was available
FYI: The standard library provides a similar function: strdup. Run man strdup to learn more.
Write a function that concatenates two strings.
- Prototype:
char *str_concat(char *s1, char *s2); - The returned pointer should point to a newly allocated space in memory which contains the contents of
s1, followed by the contents ofs2, and null terminated - if
NULLis passed, treat it as an empty string - The function should return
NULLon failure
Write a function that returns a pointer to a 2 dimensional array of integers.
- Prototype:
int **alloc_grid(int width, int height); - Each element of the grid should be initialized to
0 - The function should return
NULLon failure - If
widthorheightis0or negative, returnNULL
Write a function that frees a 2 dimensional grid previously created by your alloc_grid function.
- Prototype:
void free_grid(int **grid, int height); - Note that we will compile with your
alloc_grid.cfile. Make sure it compiles.
Write a function that concatenates all the arguments of your program.
- Prototype:
char *argstostr(int ac, char **av); - Returns
NULLifac == 0orav == NULL - Returns a pointer to a new string, or
NULLif it fails - Each argument should be followed by a
\nin the new string
Write a function that splits a string into words.
- Prototype:
char **strtow(char *str); - The function returns a pointer to an array of strings (words)
- Each element of this array should contain a single word, null-terminated
- The last element of the returned array should be
NULL - Words are separated by spaces
- Returns
NULLifstr == NULLorstr == "" - If your function fails, it should return
NULL