-8

I want to know that how can we allocate a memory block at run-time in C or C++ without using malloc and calloc functions.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
me_digvijay
  • 5,374
  • 9
  • 46
  • 83

8 Answers8

6

In C, use malloc. Don't forget to free after use.

In C++, use new and don't forget to delete. Or better, use std::vector if you want a dynamic array.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Sorry but I forgot to mention that this is to be done without malloc and calloc. – me_digvijay Jan 09 '12 at 11:40
  • 1
    @AmitSharma: then you need to use a platform-specific API. `malloc` is the only portable dynamic allocation mechanism in standard C. – Fred Foo Jan 09 '12 at 11:42
3

In C, using VLA ...

/* fill an array, allocated dinamically without malloc,
** with 1, 2, 3, 4, ...
** then sum all of the values and print the result */
#include <stdio.h>

void vlaalloc(size_t nmemb, size_t siz, void (*fx) (void *, size_t)) {
  unsigned char data[nmemb * siz];

  fx(data, nmemb);
}

int arraysum(int *arr, size_t len) {
  int val = 0;
  for (size_t i = 0; i < len; i++) val += arr[i];
  return val;
}

void seq(void *data, size_t len) {
  int *arr = data;
  for (size_t i = 0; i < len; i++) arr[i] = i + 1;
  printf("array sum is %d\n", arraysum(arr, len));
}

int main(void) {
  int n;

  if (scanf("%d", &n) == 1) {
    vlaalloc(n, sizeof (int), seq);
  }
}

see code running at ideone

pmg
  • 106,608
  • 13
  • 126
  • 198
2

Either malloc in C, or new in C++.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

In C use malloc()

int *a = malloc (sizeof(int) * block_size);

In C++ use new

int *a = new int[block_size];

Note: this code uses raw pointers. C++11 has better pointers such as unique_ptr and shared_ptr. It is generally a good practice to prefer these smart pointers over raw pointers.

EDIT: OP needs a block so I am updating the code

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
  • 3
    In C, don't cast the return value of `malloc`. The cast is, at best, redundant; and may hide an error the compiler would have caught in its absence. – pmg Jan 09 '12 at 11:20
  • C++03 has `auto_ptr`, which is not as good as `unique_ptr` but implements automatic cleanup. – Fred Foo Jan 09 '12 at 11:21
  • But in turn `auto_ptr` has many problems, so it's better to use the boost smart pointers. – Tamás Szelei Jan 09 '12 at 11:23
1

Unless I am missing the point of your question, as it has been adviced, you just need to use the C++ language standard constructs: new and delete/delete[].

Alex Net
  • 164
  • 12
0

In C you can use:

malloc() 

in C++:

malloc()
calloc()

In C++ it's better to use the new-operator.

Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • 2
    @Xeo: The question specifically talks about *allocating memory* (not constructing objects or primitive values). So pointing out `malloc` and `calloc` for c++ is certainly not wrong (though arguably incomplete). – Frerich Raabe Jan 09 '12 at 11:18
  • 2
    @FrerichRaabe: We still have a `new` for that: `::operator new(std::size_t size)`. And generally in C++, you do *not* only want to allocate memory. – Xeo Jan 09 '12 at 11:19
  • `calloc` is a C function, it's not C++-specific. – Fred Foo Jan 09 '12 at 11:27
  • 1
    @Xeo: That is correct (and I agree that explicit resource management is not such a good idea) but again: using `malloc` is certainly okay for allocating a memory block at runtime as asked by the OP. In fact, it's a plausible function to use when implementing that `new` overload you mention yourself! Note that the answer *does* mention `it's better to use the new-operator`, too. – Frerich Raabe Jan 09 '12 at 11:32
  • @FrerichRaabe: I could swear that last sentence was edited in after my comment... anyways. What I meant was that there is (most of the time) no good reason in C++ to only allocate memory and *not* construct something in it. If you find yourself in the need for that, you either implement a library or are playing around with something you shouldn't. And if you belong to the former group, you should use `std::vector` or the likes. – Xeo Jan 09 '12 at 11:36
  • @Xeo Don't we have placement `new` exactly for this reason - so we can separate memory allocation and object construction? – lapk Jan 09 '12 at 12:00
  • @AzzA: Just because we have it does not mean you should use it if it's not needed. – Xeo Jan 09 '12 at 12:13
0

I'm unsure you your question, but the simple answer is using

C:

malloc();

C++:

new

this will return a pointer to the memory and the Operating System will take care of finding it for you.

Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
0

In C, all memory allocation is done via malloc (it's in the rules), so if you want something other than malloc, it depends on what platform you're using, and you don't say.

On Linux, mmap might do what you want. No doubt windows has something else.

On some systems you might be able to grab it without asking, as long as you know where everything is, but this is mostly only for embedded systems using a basic (or no) operating system.

ams
  • 24,923
  • 4
  • 54
  • 75