Hi ,
I'm having some probs in this code ....

#include <iostream>
#include <cstdlib>
using namespace std ;

#define SIZE 10
#define EXTRA 50
int main()
{
    int *i ;
    int j ;

    cout << "Size of int is : " << sizeof(int) << endl ;
    i = (int *)malloc(SIZE) ;
    if(!i)
    {
        cout << " Allocation error " ;
        exit(1) ;
    }
    cout << "After allocating memory... " << SIZE << endl ;

    for(j=0; j<(SIZE+EXTRA); j++)
        i[j] = j ;

    for(j=0; j<(SIZE+EXTRA); j++)
        cout << "\t" << j << " : "<< i[j]  << endl ;

    free(i) ;

    cout << "after freeing memory ..... " << endl ;

    for(j=0; j<(SIZE+EXTRA); j++)
      cout << "\t" << j << " : "<< i[j]  << endl ;


    return 0 ;
}


I'm new to dynamic allocation. When i compile this code, some strange things
happen. Here, I allocate 10 new intizer. But assigning value upto 50 intizer
(using EXTRA in for loops) give me no error . Why was that happen since a
I've assigned only 10 ?

Another problem is when I free those memory, still it gives me the same
value as before freeing the memory. Why are those values kept still there
and how ?

I'm currently compiling in Code Blocks with GCC compiler.

Thank in advance :)

-- 
                 Shamir Shakir
      http://www.DewsWorld.741.com

:::.:.......... Hope never ends...........:::::


[Non-text portions of this message have been removed]

Reply via email to