programming4us
programming4us
ENTERPRISE

Dynamic Arrays: Using malloc() and realloc()

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

This had bugging me on table of listening during a small moment, but I appeared it. I will share my knowledge with the rest of the world! If you must take data of the user who could be any length, you could define a really large line as thus :

int array[100000];

There are several problems with this. Anyhow large the line is, the user could still have more entry. If the user does not have that much entry, wasted you the memory.

When you are using malloc(), realloc() and free() you need the following header file:

#include <stdlib.h>

First, I will show you how to allocate memory for a pointer. You can declare a pointer like so:

int *pointer;

The pointer can point to any location at first. You should always make it point to something or you can allocate some memory that your pointer will point to. To do this, you need to use the malloc() function. Use it like so:

pointer=malloc(2*sizeof(int));

malloc() returns a void pointer and takes an argument of how many bytes to allocate. Because pointer points to an integer, we use the 2*sizeof(int). Using malloc like the above is similar to doing this:

int array[2];

Error checking:
If the operating system can't allocate more memory for your program, malloc will fail and return a NULL value. It's always a good idea to make sure malloc is successful:

pointer=malloc(1*sizeof(*pointer));if (pointer==NULL) {printf("Error allocating memory!\n"); //print an error messagereturn 1; //return with failure}

Now I will show you how to use realloc(). You use realloc after you have used malloc to give a pointer more or less memory. Let's say you want to give a pointer 5 integers of memory. The code should look like this:

int *temp = realloc(pointer, 5*sizeof(int));if ( temp != NULL ) //realloc was successful{pointer = temp;}else //there was an error{free(pointer);printf("Error allocating memory!\n");return 1;}

This is just like malloc, except realloc takes two arguments. The first argument is the pointer you want to copy the data from. The above code copies pointer to temp, then copies temp back to pointer if everything goes correctly. You may have noticed a new function though, and that is free().

 

Free is used to free the memory you have allocated with malloc or realloc. All memory that you allocate should be freed when you are done using it. Free takes a pointer as an argument like so:

free(pointer);

Here a program of example which makes use of a dynamic line. 

#include <stdio.h>#include <stdlib.h>
int main()
{int *data,*temp;data=malloc(sizeof(int));int c; 
int i; 
for (i=0;;i++) {c=getchar(); 
if (c==EOF) 
break;data[i]=c;
temp=realloc(data,(i+2)*sizeof(int));
if ( temp != NULL ) {data=temp;} 
else {free(data);
printf("Error allocating memory!\n");return 1;}}
for (i--;i>=0;i--)putchar(data[i]);
Return success */return 0;
}
Other  
  •  Review : D-Link DSP-W215 mydlink Wi-Fi Smart Plug
  •  Review : Linksys EA9200 Tri-Band Smart Wi-Fi router
  •  Identity on Cisco Firewalls : Administrative Access Control on ASA
  •  Identity on Cisco Firewalls : Administrative Access Control on IOS
  •  Identity on Cisco Firewalls : User-Based Zone Policy Firewall (part 3) - Integrating Auth-Proxy and the ZFW
  •  Identity on Cisco Firewalls : User-Based Zone Policy Firewall (part 2) - Establishing user-group Membership Awareness in IOS - Method 2
  •  Identity on Cisco Firewalls : User-Based Zone Policy Firewall (part 1) - Establishing user-group Membership Awareness in IOS - Method 1
  •  Identity on Cisco Firewalls : IOS User-Level Control with Auth-Proxy (part 4) - Combining Classic IP Inspection (CBAC) and Auth-Proxy
  •  Identity on Cisco Firewalls : IOS User-Level Control with Auth-Proxy (part 3) - IOS Auth-Proxy with Downloadable ACLs
  •  Identity on Cisco Firewalls : IOS User-Level Control with Auth-Proxy (part 2) - IOS Auth-Proxy with Downloadable Access Control Entries
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us