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
-3
votes
4 answers

Understanding code with use of the malloc() function and pointers

I'm trying to document some code to improve my knowledge of pointers and general ANSI C ability. ... int static_store = 30; const char * pcg = "String Literal"; int main() { int auto_store = 40; char auto_string[] = "Auto char…
Masutatsu
  • 434
  • 1
  • 7
  • 19
-3
votes
1 answer

Initializing an array with a vairable in C

I forgot that an array can't be regularly initialized with a variable in C and during a test instead of initializing the array using malloc and passing It the variable I have initialized it that way - int arr[size] when size is an int variable that…
scifie
  • 65
  • 1
  • 2
  • 14
-3
votes
2 answers

how to dynamically allocate memory for flexible array in C++

#define NUMBER_OF_INFO 3 struct info { int name; int age; struct address* addressInfo; }; struct address { int number; int building; }; I have the above struct, and I want to allocate memory for a array of struct info, and also…
ratzip
  • 1,571
  • 7
  • 28
  • 53
-3
votes
1 answer

C code behaves differently as a main, as a called function

I am writing a program that processes users text. First i wrote all the functions as different mini-programs consisting of only main-func and everything worked perfectly. But now assembling all the code i got stuck, because one function behaves…
Greg
  • 3
  • 2
-3
votes
3 answers

How can i store New Object in Vector?

I am having trouble figuring out how i can store new object into a vector, and be able to pull that information out. What i am trying to do is, storing different data from files in a series of objects, then going through these objects and pulling…
nub
  • 17
  • 1
  • 7
-4
votes
2 answers

dynamically allocate an Array

I want to declare a 2D Array without an initial size. It keeps on giving me an error: Error C2078: too many initializes. I have tried to dynamically allocate my array but nothing worked out so far as I am not too familiar with dynamic…
-4
votes
2 answers

differences between new and malloc in c++

#include #include using namespace std; class Box { public: Box() { cout << "Constructor called!" <
zaidjan1295
  • 39
  • 1
  • 8
-4
votes
1 answer

Dynamic allocation of an array of pointers

I have a problem with a code in C. I need to enter a list of names separated with the enter button. The input is stopped when the user enters the word "QUIT". the program needs to print the list of names in an alphabetical order (all letters are…
Evi
  • 1
  • 1
-4
votes
2 answers

Why don't I have to use free() after allocating without malloc()

Everybody knows that you have to free() pointers, when you use malloc() because the memory is allocated in the heap, which is not kept account of by the process. But why don't I have to use free() when assigning a pointer without a heap: char* text0…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
-4
votes
1 answer

c++ - double precision leads to non-deterministic output, float does not

I try to fill dynamically allocated arrays with numbers that are calculated in the program. One of these arrays is very large (1000 x 900 x 3). If I fill it with doubles, the program output will occasionally be reasonable, but on successive runs…
-5
votes
1 answer

How to dynamically append new object at run time in C++ array of object and print them

I have a class point. I have an event call onPoint change. This may occur any number of times. In each event a new point is given by the event. So I don't know in advance how may points to be allocated in advance. It may but 1 2 or hundreds. So I…
1 2 3
33
34