Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Please help! C++'s new operator and C's malloc function.
Please help me. I need to translate the ff C++ code snippet into C (the code here is simplified, e.g. hard coded numbers, but it reflects the code I want to translate without precision loss):
[code][green][b]int **int2dimarray = new int*;[/b]
for( int i=0; i<3; i++ )
[b]int2dimarray[i] = new int;[/b]
for( int i=0; i<3 ; i++ )
I understand I could write it as [b]int2dimarray[/b] but I need to de/allocate memory dynamically. Am I doing the right thing if I do the ff?
[green][b]int2dimarray = malloc( sizeof(int)*3 );[/b]
for( i=0; i<3; i++ )
[b]int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );[/b]
for( i=0; i!=3; i++ )
[b]free( int2dimarray[i] );
free( int2dimarray );[/b][/red]
Thanks for reading. Hope someone can help me asap.#7