dynamic-memory-allocation

Windows memory allocation questions

爱⌒轻易说出口 提交于 2019-12-12 07:27:28
问题 I am currently looking into malloc() implementation under Windows. But in my research I have stumbled upon things that puzzled me: First, I know that at the API level, windows uses mostly the HeapAlloc() and VirtualAlloc() calls to allocate memory. I gather from here that the Microsoft implementation of malloc() (that which is included in the CRT - the C runtime) basically calls HeapAlloc() for blocks > 480 bytes and otherwise manage a special area allocated with VirtualAlloc() for small

Double pointer vs array of pointers(**array vs *array[])

隐身守侯 提交于 2019-12-12 07:10:27
问题 Im not clearly sure what is the difference between those 2. My professor wrote that **array is same as *array[] and we were presented an example where he used **array (so after classes I tried exchanging that with *array[] and it didn't work), could anyone tell me if those 2 are actually the same as he wrote?? Anyway the class was about dynamic memory allocation @As soon as I changed the double pointer, this line started throwing error lines = malloc(sizeof(char*)); and a few others where the

How can I have a dynamically allocated 2D array in C? [duplicate]

拈花ヽ惹草 提交于 2019-12-12 03:35:36
问题 This question already has answers here : How do I correctly set up, access, and free a multidimensional array in C? (5 answers) Closed 4 years ago . So I have a program with a struct typedef struct s_struct { int rows; int cols; char* two_d; //This is supposed to be the 2D array } *GRID; I want to create a struck and dynamically allocate memory to it and then fill the 2D array but I don't know how. Here is what I have for the create(int prows, int pcols) function: GRID grid = malloc(sizeof

Dynamic array in Fortran 77

瘦欲@ 提交于 2019-12-12 03:05:06
问题 I have to write a subroutine in Fortran 77(i'm using Intel Fortran), which reads the measured values from a text file and stores them in a matrix. Since the number of measured values is always variable, I must dynamically allocate the matrix. I know that the dynamic allocation is only possible from Fortran 90, but at that time people had the same problems, so it is also possible. How would you proceed? I do not want to reserve too much space for the matrix because the method is impractical

C++ Segmentation Fault After When Trying to Write to Matrix

时光怂恿深爱的人放手 提交于 2019-12-12 02:57:52
问题 I have this 3D matrix I allocated as one block of memory, but when I try to write to the darn thing, it gives me a segmentation fault. The thing works fine for two dimensions, but for some reason, I'm having trouble with the third...I have no idea where the error is in the allocation. It looks perfect to me. Here's the code: phi = new double**[xlength]; phi[0] = new double*[xlength*ylength]; phi[0][0] = new double[xlength*ylength*tlength]; for (int i=0;i<xlength;i++) { phi[i] = phi[0] +

Stack Smashing Detected at End of Program

夙愿已清 提交于 2019-12-12 02:49:03
问题 I am currently testing a program on a smaller scale to distinguish a problem when I attempt to exit the program by return 0; at the end of the main function. Main.c #include <stdio.h> #include <stdlib.h> #include "Header.h" int main (void) { int i; int Fin = 0; Student sStu; Array aAry; Student *Stu = &sStu; Array *Ary = &aAry; InitArray(Ary, 1); while(Fin != 2) { printf("Please choose a selection.\n"); printf("1. Add Student\n"); printf("2. Print Students\n"); printf("3. Exit\n"); scanf("%d"

c++ linked list missing nodes after allocation in multiple threads, on x64 linux; why?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 02:07:57
问题 I've included a source code you can compile and see the problem for yourself. compile with g++ -lpthread list-memchk.cpp -o list-memchk RUN THIS, FOR EXAMPLE, ./list-memchk 43000000 30000000 10 ive included three files, first one, list-memchk.cpp #include <cstdlib> #include <iostream> #include <pthread.h> using namespace std; struct node { public : unsigned int part1; // 4 bytes unsigned int part2; // 4 bytes node *next; //pointer, 8 bytes on 64 bit system unsigned int read_part1(); }; struct

When should one use dynamic memory allocation function versus direct variable declaration?

一世执手 提交于 2019-12-11 19:33:31
问题 Below is an example of direct variable declaration. double multiplyByTwo (double input) { double twice = input * 2.0; return twice; } Below is an example of dynamic memory allocation. double *multiplyByTwo (double *input) { double *twice = malloc(sizeof(double)); *twice = *input * 2.0; return twice; } If I had a choice, I will use direct variable declaration all the time because the code looks more readable. When are circumstances when dynamic memory allocation is more suitable? 回答1: "If I

Allocating memory to 2D array using an array of NULL (c)

删除回忆录丶 提交于 2019-12-11 17:18:54
问题 thanks for taking the time in reading this. In my question a "vector" is defined as a 1D dimensional array of integers. Therefore an array of vectors would be a 2D dimensional array in which every vector can be of a different length. I'm asked to use: int** vectors- the 2D array int size -an integer that represents how many vectors exist inside **vectors int* sizes-a 1D array of integers that represents the length of the vectors for example,for: vectors = {{4,3,4,3},{11,22,33,44,55,66},NULL,

error C2512: no appropriate default constructor available (not classes)

不羁岁月 提交于 2019-12-11 13:48:31
问题 I'm starting out with structures, and I'm having problems dynamically allocating my structure array. I'm doing what I see in my book and on the internet, but I can't get it right. Here's both full error messages: C2512: 'Record' : no appropriate default constructor available IntelliSense: no default constructor exists for class "Record" #include <iostream> #include <string> using namespace std; const int NG = 4; // number of scores struct Record { string name; // student name int scores[NG];