dynamic-memory-allocation

dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

百般思念 提交于 2020-08-27 10:07:08
问题 Is there any way to free some part of memory you created by using malloc(); suppose:- int *temp; temp = ( int *) malloc ( 10 * sizeof(int)); free(temp); free() will release all 20 byte of memory but suppose i only need 10 bytes. Can i free last 10 bytes. 回答1: You should use the standard library function realloc . As the name suggests, it reallocates a block of memory. Its prototype is (contained in the header stdlib.h ) void *realloc(void *ptr, size_t size); The function changes the size of

How do strings allocate memory in c++?

自闭症网瘾萝莉.ら 提交于 2020-07-07 06:51:27
问题 I know that dynamic memory has advantages over setting a fixed size array and and using a portion of it. But in dynamic memory you have to enter the amount data that you want to store in the array. When using strings you can type as many letters as you want(you can even use strings for numbers and then use a function to convert them). This fact makes me think that dynamic memory for character arrays is obsolete compared to strings. So i wanna know what are the advantages and disadvantages

C - Compare Pointers From Different Allocations?

让人想犯罪 __ 提交于 2020-06-25 07:03:50
问题 I have implemented an AVL tree in C. Only later did I read that pointer comparison is only valid between objects in the same array. In my implementation, I do certain equality tests. For example, to test whether a node is a right child of a parent I might test node==node->parent->right . However, the nodes are allocated as needed, not in a contiguous chunk. Is this behavior defined? How would you write this code instead if it is not? 回答1: For equality and inequality, in the standard (ISO/IEC

finding dynamic memory allocation errors in arrays of strings and structures

大憨熊 提交于 2020-06-17 03:38:09
问题 So, I have an array of a structures called vitorias (in English, "victories"), an array of structures and an array of that structure and an array of strings. Structure and arrays: char **sistema_eq; typedef struct { int id; char nome[MAX_CHARS]; int vit; } vitorias; The problem is that when I use cppcheck it gives an error saying: (error) Common realloc mistake: 'conj_vit' nulled but not freed upon failure (error) Common realloc mistake: 'sistema_eq' nulled but not freed upon failure (error)

finding dynamic memory allocation errors in arrays of strings and structures

て烟熏妆下的殇ゞ 提交于 2020-06-17 03:38:07
问题 So, I have an array of a structures called vitorias (in English, "victories"), an array of structures and an array of that structure and an array of strings. Structure and arrays: char **sistema_eq; typedef struct { int id; char nome[MAX_CHARS]; int vit; } vitorias; The problem is that when I use cppcheck it gives an error saying: (error) Common realloc mistake: 'conj_vit' nulled but not freed upon failure (error) Common realloc mistake: 'sistema_eq' nulled but not freed upon failure (error)

Does Iterator::collect allocate the same amount of memory as String::with_capacity?

ぐ巨炮叔叔 提交于 2020-06-12 06:47:11
问题 In C++ when joining a bunch of strings (where each element's size is known roughly), it's common to pre-allocate memory to avoid multiple re-allocations and moves: std::vector<std::string> words; constexpr size_t APPROX_SIZE = 20; std::string phrase; phrase.reserve((words.size() + 5) * APPROX_SIZE); // <-- avoid multiple allocations for (const auto &w : words) phrase.append(w); Similarly, I did this in Rust (this chunk needs the unicode-segmentation crate) fn reverse(input: &str) -> String {

Efficient linked list in C++?

北城余情 提交于 2020-05-09 19:07:11
问题 This document says std::list is inefficient: std::list is an extremely inefficient class that is rarely useful. It performs a heap allocation for every element inserted into it, thus having an extremely high constant factor, particularly for small data types. Comment: that is to my surprise. std::list is a doubly linked list, so despite its inefficiency in element construction, it supports insert/delete in O(1) time complexity, but this feature is completely ignored in this quoted paragraph.

Memory location of Fortran allocatable arrays on assigment?

孤者浪人 提交于 2020-02-03 19:01:42
问题 Suppose I have something like: real, dimension(:), allocatable :: S integer, dimension(:) :: idx ... S = S(idx) where S and idx are properly allocated/initialized before the assignment. What does the Fortran standard(s) say, if anything, about the memory location (address) of S ? Should it stay in the same place after the assigment? Is it unspecified (up to the compiler to decide)? Does it make a difference if S is not allocatable ? Full example: $ cat test.f90 program test implicit none real

Memory location of Fortran allocatable arrays on assigment?

雨燕双飞 提交于 2020-02-03 19:00:12
问题 Suppose I have something like: real, dimension(:), allocatable :: S integer, dimension(:) :: idx ... S = S(idx) where S and idx are properly allocated/initialized before the assignment. What does the Fortran standard(s) say, if anything, about the memory location (address) of S ? Should it stay in the same place after the assigment? Is it unspecified (up to the compiler to decide)? Does it make a difference if S is not allocatable ? Full example: $ cat test.f90 program test implicit none real

Memory location of Fortran allocatable arrays on assigment?

ぐ巨炮叔叔 提交于 2020-02-03 18:58:28
问题 Suppose I have something like: real, dimension(:), allocatable :: S integer, dimension(:) :: idx ... S = S(idx) where S and idx are properly allocated/initialized before the assignment. What does the Fortran standard(s) say, if anything, about the memory location (address) of S ? Should it stay in the same place after the assigment? Is it unspecified (up to the compiler to decide)? Does it make a difference if S is not allocatable ? Full example: $ cat test.f90 program test implicit none real