Thread: chek this code please

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    chek this code please

    Code:
    THIS is the program works fine but after reallocation i will only able to print values of 5678, not all values form 1,2,3,4,5,6,7,8
    kindly help me it my assignment.
    Sample Output
    Program to dynamically Allocates an array of integers.
    Enter the size of the array: 4
    Enter a value: 1
    Enter a value: 2
    Enter a value: 3
    Enter a value: 4
    
    
    The elements of the array are:
    1 2 3 4
    
    Enter the elements in the array after reallocation:
    Enter a value: 5
    Enter a value: 6
    Enter a value: 7
    Enter a value: 8
    
    1 2 3 4 5 6 7 8
    
    here is program
    
    Code:
    
    #include<stdio.h>
    #include<iostream.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    main()
    {
          int x,i,arrysize,*iptr,*sptr;
          cout<<"Enter the size of the array:";         // take values from user
          cin>>arrysize;
          iptr=(int*)calloc(arrysize,sizeof(int));         // memory alloaction process & error check
          if(iptr==NULL)
          {
            cout<<"there is problem in memory allocation ";
            }
          sptr=iptr;
          for(i=0; i<arrysize; i++)                      //geting valuse from user in arry
          {
            cout<<"Enter a value:"; 
            cin>>*sptr;
            sptr++;
            }
          cout<<"The elements of the array are:\n";        // printing the values befor realloction
          for(i=0; i<arrysize; i++)
          {
            cout<<*iptr;
            iptr++;
            }
            realloc(iptr,(arrysize*2)*sizeof(int));                      // memory reallocation & error check
            if(iptr==NULL)
          {
            cout<<"there is problem in memory REallocation ";
            }
            cout<<"\nEnter the elements in the array after reallocation:\n";
            for(i=0; i<arrysize; i++)                      //geting valuse from user in arry after reallocation
          {
            cout<<"Enter a value:"; 
            cin>>*sptr;
            sptr++;
            }
            cout<<"The elements of the array after reallocation:\n";        // printing the values after REealloction
          for(i=0; i<arrysize; i++)
          {
            cout<<*iptr;
          iptr++;
            }   
            free(iptr);
            sptr=NULL;
            
          getch();
          }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. You don't change arraysize
    2. You don't assign the result of realloc to a variable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM