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
0
votes
2 answers

Why calloc wasn't intended to assign arbitrary values?

As per Why malloc+memset is slower than calloc? malloc+memset is slower than calloc under certain conditions. Why wasn't calloc written in such a way that it can take an extra value argument ( like memset) to override default assignment by zero?…
user13107
  • 3,239
  • 4
  • 34
  • 54
0
votes
1 answer

why move_alloc is not working in gfortran (4.6.3), but it is in ifort?

I've read here that move_alloc works since gfortran 4.2. I'm with gfortran 4.6 installed in my ubuntu 12.04 but move_alloc is not working! The move_alloc is used five times inside a loop that runs 10 times. After compiled (without any error or…
0
votes
1 answer

Variably modified array at file scope and subscripted value is neither array nor pointer

I have the following code for calculating n-queen puzzle using pthreads. But when I try to compile that code I get the following error message: wikithread.c:7:5: error: variably modified ‘hist’ at file scope #include #include…
user1726549
0
votes
1 answer

Using a pointer to dynamically allocate array

I am trying to use a dynamically allocate an array using a pointer in my tic tac toe game. I am a little confused on how exactly pointers work. We are using the cplusplus.com website as our textbook, and I think the section on pointers is a little…
user2085224
  • 49
  • 1
  • 6
  • 14
0
votes
1 answer

C - dynamically allocating a circular-buffer of structs within a struct

I am attempting to develop a dynamically-allocated circular-buffer in C using two structs. One holds detailed information and another is essentially used as a pointer from main to the circular-buffer structure (as there will be multiple arrays…
sudo_coffee
  • 888
  • 1
  • 12
  • 26
0
votes
2 answers

Is my C structure and memory allocation correct?

Not sure if I have the correct syntax; my code is working, just want to run it past anyone that would like to comment to help improve it. I assume that allocating 20480 is not consuming any space because it's just an array of pointers? So I can make…
JeffR
  • 765
  • 2
  • 8
  • 23
0
votes
0 answers

LED matrix pattern manipulation with a byte matrix

I'm with few questions about how to implement a function for adding a byte matrix to some "pattern" structure vector. Here is my code structure: struct pattern
{ byte** map; int size; }; struct pattern* pvec; int patterns = 0; void…
Ramon Saraiva
  • 518
  • 1
  • 5
  • 14
0
votes
2 answers

Attempting to dynamically allocate a multidimensional array in C results in a crash

For a multitude of reasons, I'd like to allocate multidimensional arrays in contiguous chunks of memory. I can do this by allocating them manually,…
0
votes
2 answers

C Dynamic allocation of matrices and vectors

I'm writing this code in C to implement a genetic algorthm. This is the part where I do the dynamic allocation of all the working structures (matrices and vectors). When I run this, sometimes (3 times on 5) it crashes. I'm doing something wrong with…
Malo
  • 111
  • 1
  • 3
  • 8
0
votes
2 answers

Dynamic allocation vs using the stack

I have a relatively simple question. In one of my CS classes, for an assignment, we have to make a simple side-scrolling game using C++ and the XLib libraries. In a forum we have for the class, a lot of students complained about memory leaks and…
Tesla
  • 822
  • 2
  • 13
  • 27
0
votes
1 answer

issues with dynamic allocation of struct

I'm working with a structure IMAGE_T (shown below, excuse the French) with its own alloc function. typedef struct { int nbl; /* nombre de ligne de l'image */ int nbc; /* nombre de colonnes de l’image */ unsigned char…
freewilly
  • 3
  • 1
0
votes
1 answer

Would repeatedly allocate a char array in dynamic lead to problems?

Pseudo code in C++ char* data = new char[determine_size()]; // ... do some stuff with data delete[] data; // ... repeat process So basically, data gets repeatedly allocated to a new array with a different size each time. Though every time the…
ains
  • 1,436
  • 4
  • 18
  • 33
0
votes
2 answers

Using malloc() for multiple inputs?

Alright I know that malloc or calloc can be used for dynamic allocation but as a new to C I don't know how to use that memory I allocated for inputting multiple inputs like in example of TC++ we have this code #include #include…
DevX
  • 308
  • 5
  • 16
0
votes
3 answers

SegFault in dynamically allocated array of structs, C

typedef struct { double x; double y; long out_x; long out_y; } coords; typedef struct { char name[FIGURE_LEN + 1]; int coordcount, size_tracker; coords *coordinate; } fig; fig figure; fig * figpoint; this is the functions that are…
wenincode
  • 376
  • 5
  • 10
  • 20
0
votes
4 answers

how to allocate memory for struct itself, and its members

I have this struct: struct foo { char *a; char *b; char *c; char *d; }; it's possible allocate space for struct itself and its members instead of e.g, struct foo f; f.a = malloc(); f.b = malloc(); f.c = malloc(); f.d =…
Jack
  • 16,276
  • 55
  • 159
  • 284