Can the compiler assume that malloc will never return NULL?

In this video it is mentioned that the compiler can assume that malloc never returns NULL and is allowed to optimize accordingly. I have never heard of this and couldn’t find any reference to it in the C-Standard. Can anyone tell me if this is true and if so, where this behaviour is specified? The… Read More Can the compiler assume that malloc will never return NULL?

Why do I always hear to avoid allocating memory when I can in C?

I am always told to not allocate memory whenever I can achieve the exact same thing without it. Do people say that because freeing things in your code can take some time, or is there an efficiency-oriented explanation? (e. g. better performance) >Solution : Because managing memory is complicated and prone to all kinds of… Read More Why do I always hear to avoid allocating memory when I can in C?

C code not throwing error for initializing extra string character than it is supposed to be

I was running this code to expect an error but to my surprise, it didn’t. Even if I initialize a string*(5 bytes)* that is greater than what was allocated using malloc (4bytes). #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char * name = malloc(4); name = "hello"; printf("%s\n",name); return 0; } This is… Read More C code not throwing error for initializing extra string character than it is supposed to be

looping array of pointers to free them makes program crash in c

Full code: #include <stdio.h> #include <stdlib.h> #include <string.h> void printarray(int* array, int arraysize){ for (int i = 0; i<arraysize; i++){ printf("%d\n", *array); array++; } } void printStrArray(char** array, int arraysize){ int j = 0; for (int i = 0; i<arraysize; i++){ j = 0; while (array[i][j] != ‘\0’){ printf("%c", array[i][j]); j++; } printf("\n"); } }… Read More looping array of pointers to free them makes program crash in c

Two functions with exact same definition gives different results on valgrind. Dynamic Memory Alocation

The Problem I was having some troubles with a library, wich makes use of flexible array members. There is no issues to allocate the object, but i can’t reliably free it. Whenever the object has to be resized or free’d, using free() or realloc(), valgrind outputs errors of reads of uninitialized values, invalid free() calls,… Read More Two functions with exact same definition gives different results on valgrind. Dynamic Memory Alocation

C++ using vector<vector> to represent matrix with continuous data buffer

#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<vector<float>> func(int M) { // res = matrix size MxM vector<vector<float>> res; float* buffer = static_cast<float*>(malloc(M * M * sizeof(float))); res.reserve(M); for (int i=0; i<M; i++) { res.emplace_back(buffer + i * M, buffer + (i + 1) * M); /// res[i] = compute_the_matrix(); } return res;… Read More C++ using vector<vector> to represent matrix with continuous data buffer