C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Please help! C++'s new operator and C's malloc function. Posted by chenfeng7 on 8 Feb 2007 at 9:25 PM
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):

int **int2dimarray = new int*[3];
for( int i=0; i<3; i++ )
    int2dimarray[i] = new int[5];

for( int i=0; i<3 ; i++ )
    delete []int2dimarray[i];
delete []int2dimarray;


I understand I could write it as int2dimarray[3][5] but I need to de/allocate memory dynamically. Am I doing the right thing if I do the ff?

int **int2dimarray;
int i;

int2dimarray = malloc( sizeof(int)*3 );
for( i=0; i<3; i++ )
    int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );

for( i=0; i!=3; i++ )
    free( int2dimarray[i] );
free( int2dimarray );

Thanks for reading. Hope someone can help me asap.

#7
Report
Re: Please help! C++'s new operator and C's malloc function. Posted by MT2002 on 8 Feb 2007 at 10:07 PM
: 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):
:
:
int **int2dimarray = new int*[3];
: for( int i=0; i<3; i++ )
:     int2dimarray[i] = new int[5];
: 
: for( int i=0; i<3 ; i++ )
:     delete []int2dimarray[i];
: delete []int2dimarray;

:
: I understand I could write it as int2dimarray[3][5] but I need to de/allocate memory dynamically. Am I doing the right thing if I do the ff?
:
:
int **int2dimarray;
: int i;
: 
: int2dimarray = malloc( sizeof(int)*3 );
: for( i=0; i<3; i++ )
:     int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );
: 
: for( i=0; i!=3; i++ )
:     free( int2dimarray[i] );
: free( int2dimarray );
: 

: Thanks for reading. Hope someone can help me asap.
:
: #7
:

Code looks correct...

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by chenfeng7 on 8 Feb 2007 at 10:21 PM
:
: Code looks correct...

:

Thanks. I have read that pointers can be very dangerous and was afraid that once the memory space is "full", my app would start to behave like a crazy program.
#7

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by stober on 9 Feb 2007 at 3:40 AM
: :
: : Code looks correct...

: :
:
: Thanks. I have read that pointers can be very dangerous and was afraid that once the memory space is "full", my app would start to behave like a crazy program.
: #7
:
:

That's correct whether C or C++, or any other programming language. Computers have a finite amount of memory, when that's full then the entire operating system may crash and die a horrible death.

About the code you posed -- in C it is not necessary to typecast the return value of malloc(). Otherwise the code looks ok. If you are really concerned about your program running out of memory then you should check the malloc()'s return value for NULL before using it.

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by chenfeng7 on 9 Feb 2007 at 4:28 AM
: : :
: : : Code looks correct...

: : :
: :
: : Thanks. I have read that pointers can be very dangerous and was afraid that once the memory space is "full", my app would start to behave like a crazy program.
: : #7
: :
: :
:
: That's correct whether C or C++, or any other programming language. Computers have a finite amount of memory, when that's full then the entire operating system may crash and die a horrible death.
:
: About the code you posed -- in C it is not necessary to typecast the return value of malloc(). Otherwise the code looks ok. If you are really concerned about your program running out of memory then you should check the malloc()'s return value for NULL before using it.
:
:
Yeah. That one. Thanks for reminding me. #7
#7

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by MT2002 on 9 Feb 2007 at 3:40 AM
: :
: : Code looks correct...

: :
:
: Thanks. I have read that pointers can be very dangerous and was afraid that once the memory space is "full", my app would start to behave like a crazy program.
: #7

While they can be (Primarily memory curruption), alot of systems (Such as Win32) emulate memory on the hard disk (Known as Virtual Memory). So, if your memory space becomes all used, Windows will increase the virtual memory on the hard drive for it.

-Of course, this is nonportable, and would be coincisdered bad programming practice

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by stober on 9 Feb 2007 at 3:42 AM

:
: -Of course, this is nonportable, and would be coincisdered bad programming practice
:


what is this that is non-portable?
Report
Re: Please help! C++'s new operator and C's malloc function. Posted by MT2002 on 9 Feb 2007 at 3:46 AM
:
: :
: : -Of course, this is nonportable, and would be coincisdered bad programming practice
: :
:
:
: what is this that is non-portable?
:

Virtual Memory

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by Lundin on 9 Feb 2007 at 6:34 AM
Found one minor bug. Changes in blue[/blue] below:


:
int **int2dimarray;
: int i;
: 
: int2dimarray = malloc( sizeof(int*)*3 );
: for( i=0; i<3; i++ )
:     int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );
: 
: for( i=0; i!=3; i++ )
:     free( int2dimarray[i] );
: free( int2dimarray );
: 

: Thanks for reading. Hope someone can help me asap.
:
: #7
:

Report
Re: Please help! C++'s new operator and C's malloc function. Posted by chenfeng7 on 9 Feb 2007 at 6:38 AM
: Found one minor bug. Changes in blue[/blue] below:
:
:
: :
int **int2dimarray;
: : int i;
: : 
: : int2dimarray = malloc( sizeof(int*)*3 );
: : for( i=0; i<3; i++ )
: :     int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );
: : 
: : for( i=0; i!=3; i++ )
: :     free( int2dimarray[i] );
: : free( int2dimarray );
: : 

: : Thanks for reading. Hope someone can help me asap.
: :
: : #7
: :
:
:

Thanks for that detailed inspection. That's a great debugging.

#7




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.