473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Functions to allocate and de-allocate memory.

hey everyone im trying to write a code that will have different functions which will allocate memory, de-allocate, reallocate etc. this is my code
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void main()
  4. {
  5.     void allocate(int *ptr);
  6.     void reallocate(int *ptr);
  7.     void ffr(int *ptr);
  8.     int ans=0;
  9.     int *ptr;
  10.  
  11.     allocate(ptr);
  12.     reallocate(ptr);
  13.  
  14.  
  15.  
  16.  
  17.  
  18. }
  19.  
  20.  
  21.  
  22. void allocate(int *ptr)
  23. {
  24.     int size;
  25.  
  26.     printf("Enter the size that you want to allocate:");
  27.     scanf("%d", &size);
  28.     ptr=(int *)malloc(size*sizeof(int));
  29.     if(ptr!=NULL)
  30.     {
  31.         printf("Memory is allocated\n");
  32.     }
  33.     else
  34.     {
  35.         printf("Failed to allocate memory\n");
  36.  
  37.     }
  38.     printf("The address of the allocated memory is :%u\n", &ptr);
  39.  
  40.     printf("Would you like to free the memory?\n");
  41.     int choice;
  42.     printf("If yes, press 1.\n if no, press 2.\n");
  43.     scanf("%d", &choice);
  44.     if(choice==1)
  45.     {
  46.         free(ptr);
  47.  
  48.     }
  49.  
  50.  
  51. }
  52. void reallocate(int *ptr)
  53. {
  54.     int size;
  55.     printf("Enter the new size you want to allocate");
  56.     scanf("%d", &size);
  57.  
  58.     ptr=(int *)realloc(ptr,size*sizeof(int));
  59.     if(ptr!=NULL) {
  60.         printf("Now allocating more memory... \n");
  61.  
  62.     }
  63. }
i need some help.
when i allocate memory and print the address, its always the same, is that the normal?
im supposed to free the pointer in a different function but i couldnt do it.
and my program crashes when i enter the new size i want to reallocate.
May 23 '10 #1
1 2164
Banfa
9,065 Expert Mod 8TB
Try printing out the value of the pointer in all parts of your program, allocate, after calling main in allocate and in reallocate and see what you see, then answer

How does allocate pass the pointer back to main?
May 24 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reimar Bauer | last post by:
I am a bit confused by writing or using my user functions. The only way I know at the moment to call my own functions is to put them in one file with the main program. this example comes from...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
6
by: TS | last post by:
Does anyone know if the Mac Browsers (IE5 / Safari) support anonymous functions or lambda functions? Ive been trying to get a site working with the Mac browsers and just cant get it to work. Here...
6
by: Alex Vinokur | last post by:
Here are results of comparative performance tests carried out using the same compiler (gcc 3.2) in different environments (CYGWIN, MINGW, DJGPP) on Windows 2000 Professional. The following...
20
by: cylin | last post by:
Dear all, I open a binary file and want to write 0x00040700 to this file. how can I set write buffer? --------------------------------------------------- typedef unsigned char UCHAR; int...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
14
by: Alan Silver | last post by:
Hello, I have spent ages trawling through Google, looking for information about global functions in ASP.NET and I'm still not clear about the best way to go about this (or not). I am writing...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
0
by: victorsk | last post by:
Hello, I hope that nobody will get mad at me but I am still having difficulty running an app I downloaded at: ...
14
by: Remo D. | last post by:
I know that a variable (void *) is guaranteed to be able to store any pointer and to allow conversion back to the original pointer. I think, then, that I can store a pointer to a function in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.