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*[3];[/b]
for( int i=0; i<3; i++ )
[b]int2dimarray[i] = new int[5];[/b]
[/green][red]
for( int i=0; i<3 ; i++ )
[b]delete []int2dimarray[i];
delete []int2dimarray;[/b][/red][/code]

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

[code][b]int **int2dimarray;[/b]
int i;

[green][b]int2dimarray = malloc( sizeof(int)*3 );[/b]
for( i=0; i<3; i++ )
[b]int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );[/b]
[/green][red]
for( i=0; i!=3; i++ )
[b]free( int2dimarray[i] );
free( int2dimarray );[/b][/red]
[/code]
Thanks for reading. Hope someone can help me asap.

#7

Comments

  • : 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*[3];[/b]
    : for( int i=0; i<3; i++ )
    : [b]int2dimarray[i] = new int[5];[/b]
    : [/green][red]
    : for( int i=0; i<3 ; i++ )
    : [b]delete []int2dimarray[i];
    : delete []int2dimarray;[/b][/red][/code]
    :
    : I understand I could write it as [b]int2dimarray[3][5][/b] but I need to de/allocate memory dynamically. Am I doing the right thing if I do the ff?
    :
    : [code][b]int **int2dimarray;[/b]
    : int i;
    :
    : [green][b]int2dimarray = malloc( sizeof(int)*3 );[/b]
    : for( i=0; i<3; i++ )
    : [b]int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );[/b]
    : [/green][red]
    : for( i=0; i!=3; i++ )
    : [b]free( int2dimarray[i] );
    : free( int2dimarray );[/b][/red]
    : [/code]
    : Thanks for reading. Hope someone can help me asap.
    :
    : #7
    :
    [blue]
    Code looks correct...[/blue]
  • : [blue]
    : Code looks correct...[/blue]
    :

    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

  • : : [blue]
    : : Code looks correct...[/blue]
    : :
    :
    : 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.

  • : : [blue]
    : : Code looks correct...[/blue]
    : :
    :
    : 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
    [blue]
    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 :-)[/blue]

  • :
    : [blue]-Of course, this is nonportable, and would be coincisdered bad programming practice :-)[/blue]
    :


    what is [blue]this[/blue] that is non-portable?
  • :
    : :
    : : [blue]-Of course, this is nonportable, and would be coincisdered bad programming practice :-)[/blue]
    : :
    :
    :
    : what is [blue]this[/blue] that is non-portable?
    :
    [blue]
    Virtual Memory
    [/blue]
  • : : : [blue]
    : : : Code looks correct...[/blue]
    : : :
    : :
    : : 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

  • Found one minor bug. Changes in [b][blue]blue[/b][/blue] below:


    : [code][b]int **int2dimarray;[/b]
    : int i;
    :
    : [green][b]int2dimarray = malloc( sizeof([blue]int*[/blue])*3 );[/b]
    : for( i=0; i<3; i++ )
    : [b]int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );[/b]
    : [/green][red]
    : for( i=0; i!=3; i++ )
    : [b]free( int2dimarray[i] );
    : free( int2dimarray );[/b][/red]
    : [/code]
    : Thanks for reading. Hope someone can help me asap.
    :
    : #7
    :

  • : Found one minor bug. Changes in [b][blue]blue[/b][/blue] below:
    :
    :
    : : [code][b]int **int2dimarray;[/b]
    : : int i;
    : :
    : : [green][b]int2dimarray = malloc( sizeof([blue]int*[/blue])*3 );[/b]
    : : for( i=0; i<3; i++ )
    : : [b]int2dimarray[i] = (int *) malloc ( sizeof(int)*5 );[/b]
    : : [/green][red]
    : : for( i=0; i!=3; i++ )
    : : [b]free( int2dimarray[i] );
    : : free( int2dimarray );[/b][/red]
    : : [/code]
    : : Thanks for reading. Hope someone can help me asap.
    : :
    : : #7
    : :
    :
    :

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

    #7

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories