dynamic-memory-allocation

How do I dynamically allocate memory to a polymorphic data type?

浪子不回头ぞ 提交于 2019-12-25 01:49:52
问题 I have a set class that can polymorphically hold and manage anything of type Multinumber. This can be an object of type Pair, Rational, or Complex. The problem that I am running into is that this class requires dynamic memory management and in many cases, such as the function below, I don't know what type to allocate. Since the set is type agnostic, I can't know if I am supposed to do Multinumber* var=new Complex, Rational, or Pair. Is there any way to check the type of what I am adding first

Is it possible to mark a segment of memory as “out of bounds” so the heap manager doesn't allocate from it?

不羁的心 提交于 2019-12-24 14:14:57
问题 Earlier today I asked this question. After spending some time investigating this issue, I have discovered what is going on. I am posting this as a new question because I think it is interesting enough to track as a separate issue. I will update that question with the answer (and a link to this one). Launching unit test from debugger // Construct object Object* pObject = new Object(...); // Pointer value of pObject == 0x05176960 // Lots of other code // ... // Destroy object delete pObject; //

MPI_Gatherv: create and collect arrays of variable size (MPI+C)

╄→гoц情女王★ 提交于 2019-12-24 08:12:34
问题 I am new to MPI and I am trying to manage arrays of different size in parallel and then pass them to the main thread, unsuccessfully so far. I have learned that MPI_Gatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int *recvcounts, const int *displs, MPI_Datatype recvtype, int root, MPI_Comm comm) is the way to go in this case. Here is my sample code, which doesn't work because of memory issues (I think). #include <stdio.h> #include <stdlib.h> #include

Dynamically set array access pattern in C

不羁岁月 提交于 2019-12-24 00:59:03
问题 I would like to do something like this in C(99): int n = compute_size_of_matrices(); int T = compute_number_of_matrices(); float matrices[][n][n] = malloc( sizeof(float) * n*n* T ); //[sic!] Watch the missing size in the first dimension. I would like to set the size of the dimension dynamically in order to do something like the following /* Load each matrix with the unit matrix */ for( int t = 0; t < T; t++ ) for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) matrices[t][i][j] = (i==j)

Read numbers from file into a dynamically allocated array

拥有回忆 提交于 2019-12-24 00:58:26
问题 I need a function that reads grades (integers) from from file and returns a dynamically allocated array in which they are stored. This is what I have tried: int *readGrades() { int *grades; int x; scanf("%d", &x); grades = malloc(x * sizeof(int)); return 0; } However I don't get anything when I run the code. The grades are stored in file called 1.in : 29 6 3 8 6 7 4 8 9 2 10 4 9 5 7 4 8 6 7 2 10 4 1 8 3 6 3 6 9 4 and I run my program using: ./a.out < 1.in Can anyone tell me what I did wrong?

Assembly: dynamic memory allocation without malloc and syscalls? [FreeDOS application]

强颜欢笑 提交于 2019-12-23 12:53:55
问题 My question is about the logic of dynamic memory allocation in assembly (particularly, MASM). There are lot of articles on this topic and all of them rely on the use of malloc or brk. However, according to my understanding, malloc as a part of C language must (or could) be certainly written on assembly. Idem for brk, because it's a part of the operating system, thus also written on C which can be replaced 1 to 1 by assembly. Very very long time ago I have seen an article in PCMag about

How does Go allocate memory in make or new calls?

自闭症网瘾萝莉.ら 提交于 2019-12-23 12:27:15
问题 When I create a new slice or struct with a make or new call: s := make([]int64, 10, 100) o := new(MyStruct) How much memory does Go allocate with a memory allocation system call? Does it pre-allocate memory, so that subsequent calls don't trigger new system calls? I am asking this because I need to allocate memory frequently in my code. I am not sure if I need to implement a memory allocater myself, or if I can depend on Go to do the dirty work. And if Go does pre-allocate memory, can I

Dynamic memory allocation in 'c' Issues

大城市里の小女人 提交于 2019-12-23 10:08:31
问题 I was writing a code using malloc for something and then faced a issue so i wrote a test code which actually sums up the whole confusion which is below:: # include <stdio.h> # include <stdlib.h> # include <error.h> int main() { int *p = NULL; void *t = NULL; unsigned short *d = NULL; t = malloc(2); if(t == NULL) perror("\n ERROR:"); printf("\nSHORT:%d\n",sizeof(short)); d =t; (*d) = 65536; p = t; *p = 65536; printf("\nP:%p: D:%p:\n",p,d); printf("\nVAL_P:%d ## VAL_D:%d\n",(*p),(*d)); return 0

C++ - Allocating memory on heap using “new”

假如想象 提交于 2019-12-23 08:18:06
问题 If I have the following statement: int *x = new int; In this case, I have allocated memory on the heap dynamically. In other words, I now have a reserved memory address for an int object. Say after that that I made the following: delete x; Which means that I freed up the memory address on the heap. Say after that I did the following again: int *x = new int; Will x point to the same old memory address it pointed to at the heap before it was deleted? What if I did this before delete : x = NULL;

Changing child object pointer to parent pointer (without memory leak)

筅森魡賤 提交于 2019-12-23 05:15:09
问题 In this question: What do conditionals do to polymorphic objects in C++? (inclusion polymorphism) I have a situation where a parent pointer is pointing at one of two possible objects determined by the preceding conditional statement. Here is the code: (Reposted with solution) ClassParent *parentPointer; //Declare pointer to parent if (condition) { ClassChild1* mychild = new mychild1(); //Initialize ClassChild1 object with pointer mychild parentPointer = mychild;//Parent pointer points to