0

I am having trouble with free. I want to allocate a 2d array of *chars and free them, but it fails at runtime. I am learning pointers and probably over complicating this.

  //NUMBEROFLINES, NUMBEROFDIE, and WIDTHOFDIE are some ints (3, 2, 3),
  int iLineSize, iLine, iDie;
  char **piDieString;

  piDieString = (char**)malloc(NUMBEROFLINES * sizeof(*piDieString)); //Allocate number of lines
  iLineSize = NUMBEROFDIE * WIDTHOFDIE * sizeof(char); //Size of line
  for(iLine = 0; iLine < NUMBEROFLINES; iLine++)
  {
     piDieString[iLine] = (char*)malloc(iLineSize); //Allocate size of lines
  }

  //Stuff happens

  /*Freeing*/
  for(iLine = 0; iLine < NUMBEROFLINES; iLine++)
     {
     free(piDieString[iLine]);
     }
  free(piDieString);
  • @nikhilmehta ***No, that is absolutely, horribly wrong!*** Don't give bad advice. It is **better** to write `sizeof(*piDieString)`, because it's safer (since it removes redundancy). The argument of the `sizeof` operator is **not evaluated,** so dereferencing an uninitialized pointer inside `sizeof` is **valid.** – user3447428 Mar 25 '14 at 04:54
  • 2
    `malloc()` `free()` looks ok, may be problem is in `//Stuff happens`. – Rohan Mar 25 '14 at 04:56
  • 1
    I don't supposed you have a `#include ` at the top of this source file? and since it wasn't linked see ["**Do I cast the result of `malloc()`?**"](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) as a related reason why I ask. – WhozCraig Mar 25 '14 at 05:06
  • If you did `#include ` then there is an error in the code you didn't show, probably a buffer overrun on one of the lines. Post a complete program that gives the errors. – M.M Mar 25 '14 at 06:04

2 Answers2

1

I believe there is something wrong in //Stuff happens .

Added prints to verify if memory is getting allocated and freed properly as follows:

#include <stdio.h>
#define NUMBEROFLINES 3
#define NUMBEROFDIE 2
#define WIDTHOFDIE 3
int main(void) {

  int iLineSize, iLine, iDie;
  char **piDieString;

  piDieString = (char**)malloc(NUMBEROFLINES * sizeof(*piDieString)); //Allocate number of lines
  printf("Allocated piDieString: 0x%x\n",piDieString );
  iLineSize = NUMBEROFDIE * WIDTHOFDIE * sizeof(char); //Size of line

  for(iLine = 0; iLine < NUMBEROFLINES; iLine++)
  {
     piDieString[iLine] = (char*)malloc(iLineSize); //Allocate size of lines
     printf("Allocating piDieString[%d]: 0x%x\n",iLine,piDieString[iLine] );
  }

  //Stuff happens

  /*Freeing*/
  for(iLine = 0; iLine < NUMBEROFLINES; iLine++)
  {
     printf("Freeing piDieString[%d]: 0x%x\n",iLine,piDieString[iLine] );
     free(piDieString[iLine]);
  }

  printf("\nFreeing piDieString: 0x%x\n",piDieString );
  free(piDieString);
  return 0;
}

The output was this which seems just fine.

Allocated piDieString: 0x966e008
Allocating piDieString[0]: 0x966e018
Allocating piDieString[1]: 0x966e028
Allocating piDieString[2]: 0x966e038
Freeing piDieString[0]: 0x966e018
Freeing piDieString[1]: 0x966e028
Freeing piDieString[2]: 0x966e038

Freeing piDieString: 0x966e008

Elaborate on "but it fails at runtime"...As in do you see a crash ? If yes Check //Stuff happens for inavlid memory access...

kundan
  • 1,278
  • 14
  • 27
0

Have you print the address of your 2d pointer if not then try you will see something astonishing because in redhat I saw here in 2d array dynamically the address is not contiguous else this place I didn't saw any where that array having non contiguous memory. May be this non contiguous creates some problem. I don't the exact reason.

asif aftab
  • 69
  • 5