dynamic-memory-allocation

Why, or when, do you need to dynamically allocate memory in C?

别说谁变了你拦得住时间么 提交于 2019-11-27 03:51:11
Dynamic memory allocation is a very important topic in C programming. However, I've been unable to find a good explanation of what this enables us to do, or why it is required. Can't we just declare variables and structs and never have to use malloc()? As a side note, what is the difference between: ptr_one = (int *)malloc(sizeof(int)); and int *ptr_one = malloc(sizeof(int)); You need to use dynamic memory when: You cannot determine the maximum amount of memory to use at compile time; You want to allocate a very large object; You want to build data structures (containers) without a fixed upper

Is a destructor called when an object goes out of scope?

梦想的初衷 提交于 2019-11-27 03:13:11
For example: int main() { Foo *leedle = new Foo(); return 0; } class Foo { private: somePointer* bar; public: Foo(); ~Foo(); }; Foo::~Foo() { delete bar; } Would the destructor be implicitly called by the compiler or would there be a memory leak? I'm new to dynamic memory, so if this isn't a usable test case, I'm sorry. Yes, automatic variables will be destroyed at the end of the enclosing code block. But keep reading. Your question title asks if a destructor will be called when the variable goes out of scope. Presumably what you meant to ask was: will Foo's destructor be called at the end of

CUDA new delete

佐手、 提交于 2019-11-27 01:09:45
问题 Can someone give a clear explanation of how the new and delete keywords would behave if called from __device__ or __global__ code in CUDA 4.2? Where does the memory get allocated, if its on the device is it local or global? It terms of context of the problem I am trying to create neural networks on the GPU, I want a linked representation (Like a linked list, but each neuron stores a linked list of connections that hold weights, and pointers to the other neurons), I know I could allocate using

Thread '<main>' has overflowed its stack when allocating a large array using Box

混江龙づ霸主 提交于 2019-11-26 23:39:20
问题 I'm implementing combsort. I'd like to create fixed-size array on the stack, but it shows stack overflow . When I change it to be on the heap (Rust by Example says to allocate in the heap we must use Box), it still shows stack overflow . fn new_gap(gap: usize) -> usize { let ngap = ((gap as f64) / 1.3) as usize; if ngap == 9 || ngap == 10 { return 11; } if ngap < 1 { return 1; } return ngap; } fn comb_sort(a: &mut Box<[f64]>) { // previously: [f64] let xlen = a.len(); let mut gap = xlen; let

C: adding element to dynamically allocated array

旧巷老猫 提交于 2019-11-26 23:32:48
问题 I've tried to search out a solution via Google: I couldn't find anything that helped; it even seemed as if I was doing this correctly. The only pages I could find regarding sending my dynamically allocated array through a function dealt with the array being inside a struct, which is scalar of course, so behaves differently. I don't want to use a struct right now -- I'm trying to learn about DAM and working with pointers and functions. That said, I'm sure it's very elementary, but I'm stuck.

Dynamic Memory Allocation in MPI

人走茶凉 提交于 2019-11-26 21:45:07
问题 I am new to MPI. I wrote a simple code to display a matrix using multiple process. Say if I have a matrix of 8x8 and launching the MPI program with 4 processes, the 1st 2 rows will be printed my 1st process the 2nd set of 2 rows will be printed by 2nd thread so on by dividing itself equally. #define S 8 MPI_Status status; int main(int argc, char *argv[]) { int numtasks, taskid; int i, j, k = 0; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &taskid); MPI_Comm_size(MPI_COMM_WORLD,

Possible memory leak without a virtual destructor?

故事扮演 提交于 2019-11-26 17:23:39
问题 #include <iostream> using namespace std; class base { int a; public: base() {a =0;} }; class derv :public base { int b; public: derv() {b =1;} }; int main() { base *pb = new derv(); delete pb; } I don't have a virtual destructor in derv class, does it delete only base part of derv object?? 回答1: It might. Because base does not have a virtual destructor, your code exhibits undefined behavior. Anything might happen. It might appear to work as you expect. It might leak memory. It might cause your

When and why to use malloc?

☆樱花仙子☆ 提交于 2019-11-26 15:37:14
问题 Well, I can't understand when and why it is needed to allocate memory using malloc . Here is my code : #include <stdlib.h> int main(int argc, const char *argv[]) { typedef struct { char *name; char *sex; int age; } student; //Now I can do two things student p; //or student *ptr = (student *)malloc(sizeof(student)); return 0; } Why is it needed to allocate memory when I can just use student p; ? 回答1: malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means

Simple C implementation to track memory malloc/free?

给你一囗甜甜゛ 提交于 2019-11-26 14:08:51
问题 programming language: C platform: ARM Compiler: ADS 1.2 I need to keep track of simple melloc/free calls in my project. I just need to get very basic idea of how much heap memory is required when the program has allocated all its resources. Therefore, I have provided a wrapper for the malloc/free calls. In these wrappers I need to increment a current memory count when malloc is called and decrement it when free is called. The malloc case is straight forward as I have the size to allocate from

dynamic allocation/deallocation of 2D & 3D arrays

天涯浪子 提交于 2019-11-26 13:08:36
问题 I know about algorithms to allocate/deallocate a 2D array dynamically, however I\'m not too sure about the same for 3D arrays. Using this knowledge and a bit of symmetry, I came up with the following code. (I had a hard time visualizing in 3D during coding). Please comment on the correctness and suggest any better alternative (efficiency-wise or intuitively), if any. Also, I think both these 2D and 3D arrays can be accessed normally like static arrays like arr2D[2][3] and arr3D[2][3][2].