Questions tagged [dynamic-allocation]

Use for questions related to *dynamic allocation of memory*, such as `malloc()` in C, `new` in C++, etc. . Please notice that the tag is not language specific.

From Wikipedia: "C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free."

Please notice that the tag is not language specific.

506 questions
1
vote
2 answers

Dynamically Allocated Array vs. Automating Declared Array with Global Scope (C language)

What is the difference between declaring an array "dynamically", [ie. using realloc() or malloc(), etc... ] vs declaring an array within main() with Global scope?, eg. int main() { int array[10]; return 0; } I am learning, and at the…
carl13
  • 69
  • 1
  • 1
  • 7
1
vote
4 answers

Allocation memory error with use struct for c

I wrote a code for managing a library; the compilation is done but during the simulation I obtained an Allocation error (case2) and I don't know why. The first case works correctly but if I entered more than one name in the first case, the second…
Juliette
  • 65
  • 7
1
vote
1 answer

How to check dynamic allocation

How can i check if i am using right the dynamic allocation in my C code. It is an assignment for uni. When i put my code in the auto corrector system of my professor i get an error for dynamic allocation. My code : #include #include…
Spyros_av
  • 854
  • 2
  • 8
  • 24
1
vote
3 answers

Lazy creation of auto variables inside C function

I`m curious about such C code: void test(int y){ if (y) { int x = 1; printf("Test: %d", x + y); } // other important code (don`t use x var) } Necessity of x variable depends on y. For instance: if we call test(0), we don't…
kirugan
  • 2,514
  • 2
  • 22
  • 41
1
vote
2 answers

Is Passing a Dynamically Allocated Array to a Function in C an Instance of Pass-by-value or Pass-by-reference?

To demonstrate, here is an example code recreating the instance of passing a dynamically allocated array to a function. #include #include void fx1(int* arr) {/* code here */ } int main() { int *arr = (int *)…
Mercado
  • 606
  • 6
  • 20
1
vote
1 answer

C dynamic allocated pointer to main function

I cant figure out how to have my pointer argv maintain its memory that I dynamically allocate in another function called parseCommand. I need to call parseCommand, allocate the needed memory for the parsed strings and then return to main and print…
brec9824
  • 13
  • 3
1
vote
1 answer

Pacing algorithm for ad delivery system

Suppose there are 3 ads that my system can deliver. If I want all three to be delivered roughly the same amount of times over the course of a week, I can just choose a random number between 1-3 each time, then deliver ad 1, 2, or 3. var ads = [ …
rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
1
vote
1 answer

Segmentation Fault outside the loop only

Using C on Linux, I'm writing a code that stores all the information about the files in a directory using function stat() and prints them on the Terminal The algorithm is quite simple, I made a structure array of "files" and dynamically allocated…
1
vote
2 answers

allocate memory for node in linked list but unexpectedly, the next node inside it is also allocated

I'm trying to implement a linked list but when I allocate memory for one note, the pointer inside it is not NULL. Here is my struct template struct Node { T value; Node* next; }; and I allocate memory for a note first = new…
thinh
  • 21
  • 4
1
vote
1 answer

Why do I get a still reachable block after mallocing a char*?

I have the following code: #include #include #include #include #include void print_usage() { printf("%s\n", "usage"); } int file_exist (char *filename) { struct stat buffer; return…
Patryk
  • 22,602
  • 44
  • 128
  • 244
1
vote
1 answer

Invoke the copy construction of a derived class from a pointer to the base class?

I have 1- class A {int m;}; 2- class B: public A {float n;}; 3- class C: public A {string n;}; I store all instances of class A in vector myInstansesofA; It is stored as a pointer because I dynamically create them according to some if…
1
vote
1 answer

Disappearing binary Tree

Im trying to call the function buildExpressionTree with the string "(4+5)" I tried to debug and found out that the tree is created successfully but when returned back to main, "tr" is empty and contains garbage. why does it fail to return the…
Dor Amar
  • 171
  • 1
  • 7
1
vote
3 answers

can i apply delete on this pointer inside a member function?

As I understand if the member function has been called using pointer to an object which is allocated dynamically, the object would get delete. But if the member function has been called using the object, which is allocated statically, then what will…
Prashant Mishra
  • 167
  • 1
  • 9
1
vote
0 answers

how to add multiple textfields to same indexpath.row in ios 8

i want to create an 10 text field on same cell index path how can i do that , get values from them and i want to do this dynamically in storyboard can any one suggest me how can i do this. if (cell != nil) { cell = [[tableViewCell…
1
vote
1 answer

Two-dimensional dynamic allocated arrays created inside a class in C++

I'm relatively new at c++ and other object oriented languages as a whole (have done one semester of C classes and now I'm having c++ classes). I'm having problems concerning the making of a dynamically allocated two-dimensional array through a class…