2 Replies - 968 Views - Last Post: 31 May 2009 - 07:02 AM Rate Topic: -----

#1 techi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-May 09

Write a program to illustrate the usage of malloc and alloc functions.

Posted 31 May 2009 - 06:06 AM

 

#include <stdio.h>
#incliude<malloc.h>
Main()
{
Int *base;
Int I;
Int cnt=0;
Int sum=0;
Printf(“how many integers you have to store\n”);
Scanf(“%d”,&cnt);
Base=(int)*malloc(cnt *sizeof(int));
Printf(“the base of allocation id %16lu \n”base);
If(base)
Printf(“uable to allocate size\n”);
Else
{
For(int j=0;j<cnt;j++)
{
*(base+j)=5;
}
Sum =0;
For(int j=0;j<cnt;j++)
{
Sum=sum+ *(base+j);
Printf(“Total  sum is %d\n”,sum);
Free(base);
Printf(“the base of allocation is %16lu \n”,base);
Base=(int *)malloc(cnt *sizeof(int));
Printf(“the base of allocation is %16lu \n”,base);
Base=(int *)malloc(cnt *sizeof(int));
Printf(“the base of allocation is %16lu \n”,base);
Base=(int *)calloc(10,2);
Printf(“the base of allocation is %16lu \n”,base);
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Write a program to illustrate the usage of malloc and alloc functions.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Write a program to illustrate the usage of malloc and alloc functions.

Posted 31 May 2009 - 06:14 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Write a program to illustrate the usage of malloc and alloc functions.

Posted 31 May 2009 - 07:02 AM

Many of your names there start with capital letters when they should not. Remember that C is case sensitive. Looks like MSWord got a hold of your program...

also note that the format for main should be at least int main() { /*...program here...*/ return 0; } of course you don't have to return the value of zero -- but main always returns and integer result (even if you forget to put it in there).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1