answersLogoWhite

0


Best Answer

char* new_string; // could be any type

new_string = (char*) malloc (5120); // allocate memory - typecast is necessary

if (new_string == NULL) ... memory exception ...

... use the data ...

free (new_string); // release memory when done

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

|Make yourself conversant calloc, malloc, realloc and free.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you allocate memory dynamically in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is heap -short?

The microprocessor architecture divides the memory into distinct areas. Heap is one of them. This is where you can statically/dynamically allocate memory.


How do you write a C plus plus statement to allocate memory cells dynamically and store the memory address in a pointer to an integer named gred?

int * gred ; gred = new int [100] ; // this example snippet creates 100 ints


What is an object in Java?

object is an instance of a class. it's used to allocate memory dynamically at run time to access class members.


What is the process of allocation of memory for an integer type variable?

To dynamically allocate memory, use the following function (stdlib.h I believe): int *variable1 = malloc(sizeof(int));


Which data type assigns values automatically?

None of the data types available in C assigns valur to the variable. Initially all the variables have a garbage value. But when we use calloc() to allocate memory dynamically only then it assigns NULL to the memory block assigned.


What are new and delete operators in c plus plus?

New and Delete are the memory management operators in c++,like c language we use malloc() and calloc() functions to allocate memory and free() functiong to release the memory similarily we use new to allocate memory in C++ and Delete to release the allocated memory....


When memory allocate to Static data in c?

Compilation time.


What will happen if you allocate memory using new and free it using free or allocate sing calloc and free it using delete?

The C++ new uses malloc internally to allocate memory and the C++ delete uses free internally to revoke memory. However, they are not interchangeable and so memory allocated with new MUST be revoked with delete. If you mix them up, you will have a memory leak! Haya.


What is the memory management operator in c plus plus?

There is no memory management operator in C++ -- it is an unmanaged language. You use the C++ new operator to allocate memory, and use the C++ delete operator to release previously allocated memory.


What is the operator is used to allocate the memory in c plus plus?

calloc operator,malloc operator


How do you allocate memory dynamically for a string of unknown size in c?

With function strdup (here is an implementation, if you don't have one already)char *strdup (const char *f){size_t len;char *to;len= strlen (f);to = malloc (len+1);if (to) memcpy (to, f, len+1);return to;}


Static and dynamic memory allocation in c plus plus?

dynamic memory allocation is that type of memory which create to allocate the memory on running time or at compile time by the function of malloc , calloc , realloc and free. dynamic memory allocation is give the best utilization of memory which gives the sufficient use of memory.