dynamic-memory-allocation

dynamic memory in QList

青春壹個敷衍的年華 提交于 2019-12-04 15:21:53
I don't have much experience with QT and this problem came out today. QList<int> memList; const int large = 100000; getchar(); for (int i=0; i<large; i++) { memList.append(i); } cout << memList.size() << endl; getchar(); for (int i=0; i<large; i++) { memList.removeLast(); } cout << memList.size() << endl; getchar(); After first loop when I check memory usage it goes up as new elements are appended to the memList but after removing them within second loop the memory usage stays at the same level. I thought that QList was dynamic and it would free memory when element is removed. So either I'm

C - pointer is not null after freeing it

前提是你 提交于 2019-12-04 07:08:23
问题 Does the value of pointer become NULL after freeing it? int* p = malloc(sizeof(*p)); free(p); if(p==NULL) printf("Null\n"); else printf("Not null\n"); Output: Not null Well, I assume not; Anyway, I have asked a question earlier today : Check it out here: C - How can I free dynamically allocated memory? List* head1 = NULL; insertFront(&head1, 1); insertFront(&head1, 2); print(head1); while (head1) { List *temp = head1; head1 = head1->next; free(temp); } if(head1 == NULL) printf("Null\n"); else

Do I need to create three separate arrays for my assignment?

久未见 提交于 2019-12-04 06:33:16
问题 Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to

understand passing parameters by reference with dynamic allocation

泪湿孤枕 提交于 2019-12-04 05:27:01
问题 I'm trying understand how to pass a parameter by reference in C language. So I wrote this code to test the behavior of parameters passing: #include <stdio.h> #include <stdlib.h> void alocar(int* n){ n = (int*) malloc( sizeof(int)); if( n == NULL ) exit(-1); *n = 12; printf("%d.\n", *n); } int main() { int* n; alocar( n ); printf("%d.\n", *n); return 0; } Here is printed: 12. 0. Example 2: #include <stdio.h> #include <stdlib.h> void alocar(int* n){ *n = 12; printf("%d.\n", *n); } int main() {

Dynamic memory allocation error in Fortran2003 using LAPACK

早过忘川 提交于 2019-12-04 05:23:33
问题 I'm struggling with LAPACK's dgetrf and dgetri routines. Below is a subroutine I've created (the variable fit_coeffs is defined externally and is allocatable, it's not the problem). When I run I get memory allocation errors, that appear when I assign fit_coeffs, due to the matmul(ATA,AT) line. I know this from inserting a bunch of print statements. Also, both error checking statements after calls to LAPACK subroutines are printed, suggesting an error. Does anyone understand where this comes

How to declare a variable size 2D array in C?

半城伤御伤魂 提交于 2019-12-04 03:16:17
I have a problem with a project. I have to make a variable size 2D array for storing some prediction error..so this is about images. The trouble is that I have to load images of different sizes so for each image I would have to get into a file the 2D array with the coresponding number of pixels..I've searched among your questions but it's not what I'm looking for.Can anyone help me? Thank you If you have a modern C compiler (at least C99) in function scope it is as simple as: unsigned arr[n][m]; this is called a variable length array (VLA). It may have problems if the array is too large. So if

Reading all content from a text file - C

孤人 提交于 2019-12-03 16:27:29
I am trying to read all content from a text file. Here is the code which I wrote. #include <stdio.h> #include <stdlib.h> #define PAGE_SIZE 1024 static char *readcontent(const char *filename) { char *fcontent = NULL, c; int index = 0, pagenum = 1; FILE *fp; fp = fopen(filename, "r"); if(fp) { while((c = getc(fp)) != EOF) { if(!fcontent || index == PAGE_SIZE) { fcontent = (char*) realloc(fcontent, PAGE_SIZE * pagenum + 1); ++pagenum; } fcontent[index++] = c; } fcontent[index] = '\0'; fclose(fp); } return fcontent; } static void freecontent(char *content) { if(content) { free(content); content =

realloc() invalid old size

被刻印的时光 ゝ 提交于 2019-12-03 14:45:06
I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it. Here is what I have written (partially, some part is taken from the book directly):- #include <stdio.h> #include <stdlib.h> int MAXLINE = 10; int INCREMENT = 10; void copy(char longest[], char line[]){ int i=0; while((longest[i] = line[i]) != '\0'){ ++i; } } int _getline(char s[]){ int i,c; for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){ if(i == MAXLINE - 1){ s = (char*)realloc(s,MAXLINE + INCREMENT); if(s == NULL){ printf("%s",

Creation of Dynamic Array of Dynamic Objects in C++

天涯浪子 提交于 2019-12-03 08:39:05
I know how to create a array of dynamic objects. For example, the class name is Stock. Stock *stockArray[4]; for(int i = 0 ; i < 4;i++) { stockArray[i] = new Stock(); } How do you change this to dynamic array of dynamic objects? What I tried: Stock stockArrayPointer = new Stock stock[4]; It doesn't work and the error is "The value of Stock** cannot be used to initalize an entity of type Stock. Second question is after the creation of dynamic array of dynamic objects, what is the syntax to access the pointers in the array. Now, I use stockArray[i] = new Stock(); How will this change? Need some

How to Dynamically Allocate Memory Using Assembly and System Calls Under Linux

柔情痞子 提交于 2019-12-03 05:44:01
问题 I'm looking for some good code examples of dynamic memory allocation using an assembly language under Linux and using system calls, not malloc and friends. What are some of the simplest but effective ways to do this? On Intel 386+ computers. 回答1: brk(2). And take a look at ELF. 回答2: On Linux mmap2 is a sensible system call to use for this at a low level. It takes 6 arguments, so in IA32 you can call it using: mov eax, 192 ; mmap2 xor ebx, ebx ; addr = NULL mov ecx, 4096 ; len = 4096 mov edx,