problem allocating/freeing memory of multidimensional arrays 
Author Message
 problem allocating/freeing memory of multidimensional arrays

Hi, I'm a beginner to C/C++
I wrote a program for matrixoperations which should manage storage (memory)
dynamicly.
The elements of a matrix should be float.
The problem is when allocating memory with malloc() I assign a float pointer
to the return-value of malloc and when freeing the allocated memory I
provide free with that pointer. I think that free() "thinks" that it just
has to free a 4 byte memory block but indeed it is an array of floats to be
freed.
What can I do ?
Here is a part of my sourcecode.
If you need more, please tell me:

[ccode]
//...
typedef struct
{
 int nCol;
 int nRow;
 float* pContents;

Quote:
} Matrix;

Matrix MatCreate(int m, int n)
{

 Matrix z;
 bool ok;
 z.nCol = -1; z.nRow = -1; z.pContents = NULL;
 if (m>-1 && n>-1)
 {
  z.pContents = (float*) malloc(z.nCol * z.nRow * sizeof(float));
  ok = (z.pContents!=NULL);
  if (ok)
  {
   z.nRow = m;
   z.nCol = n;
  }
 }
 return z;

Quote:
}

//...
void MatDelete(Matrix z)
{
 free(z.pContents);
Quote:
}

//...
int main()
{
 Matrix M1;
 M1 = MatCreate(2,1);
 //...
 MatDelete(M1);
 return 0;
Quote:
}

[/ccode]

Thanx 4 reading.



Wed, 08 Sep 2004 23:35:57 GMT  
 problem allocating/freeing memory of multidimensional arrays


Quote:
> Hi, I'm a beginner to C/C++
> I wrote a program for matrixoperations which should manage storage
(memory)
> dynamicly.
> The elements of a matrix should be float.
> The problem is when allocating memory with malloc() I assign a float
pointer
> to the return-value of malloc and when freeing the allocated memory I
> provide free with that pointer. I think that free() "thinks" that it just
> has to free a 4 byte memory block but indeed it is an array of floats to
be
> freed.
> What can I do ?
> Here is a part of my sourcecode.
> If you need more, please tell me:

> [ccode]
> //...
> typedef struct
> {
>  int nCol;
>  int nRow;
>  float* pContents;
> } Matrix;

> Matrix MatCreate(int m, int n)
> {

>  Matrix z;
>  bool ok;
>  z.nCol = -1; z.nRow = -1; z.pContents = NULL;
>  if (m>-1 && n>-1)
>  {
>   z.pContents = (float*) malloc(z.nCol * z.nRow * sizeof(float));

Simple.  You use "z.nCol" and "z.nRow" before setting them to "m, n".

Tom



Thu, 09 Sep 2004 00:00:28 GMT  
 problem allocating/freeing memory of multidimensional arrays

Quote:

> I wrote a program for matrixoperations which should manage storage (memory)
> dynamicly.
> The elements of a matrix should be float.
> The problem is when allocating memory with malloc() I assign a float pointer
> to the return-value of malloc and when freeing the allocated memory I
> provide free with that pointer. I think that free() "thinks" that it just
> has to free a 4 byte memory block but indeed it is an array of floats to be
> freed.

... snip ...
> typedef struct
> {
>  int nCol;
>  int nRow;
>  float* pContents;
> } Matrix;

> Matrix MatCreate(int m, int n)
> {

>  Matrix z;
>  bool ok;
>  z.nCol = -1; z.nRow = -1; z.pContents = NULL;
>  if (m>-1 && n>-1)
>  {
>   z.pContents = (float*) malloc(z.nCol * z.nRow * sizeof(float));

Just take a look at the above statement.  First, you should not
have a cast in it, although that is not the main problem.  How
many floats are you really allocating space for?  What is the
product of -1 and -1?

Try:
     z.pContents = malloc(m * n * sizeof *z.pContents);

Quote:
>   ok = (z.pContents!=NULL);
>   if (ok)
>   {
>    z.nRow = m;
>    z.nCol = n;
>   }
>  }
>  return z;
> }

... snip ...

Later, make sure you are not freeing a NULL pointer.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Thu, 09 Sep 2004 00:47:06 GMT  
 problem allocating/freeing memory of multidimensional arrays


[snip]

Quote:

> Later, make sure you are not freeing a NULL pointer.

There is no danger in giving free a NULL pointer...

--
Thomas.

Approaching singularity.



Thu, 09 Sep 2004 01:04:33 GMT  
 problem allocating/freeing memory of multidimensional arrays

Quote:



> [snip]

> > Later, make sure you are not freeing a NULL pointer.

> There is no danger in giving free a NULL pointer...

I just checked the standard and you are right. I have a bug in my
malloc/free implementation to take care of :-(

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Thu, 09 Sep 2004 06:13:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Allocating memory to a multidimensional array using malloc ??

2. problem with allocating multidimensional array

3. newbie malloc problem: free memory allocated to structures

4. Dynamically allocating a multidimensional Array Question

5. Allocating multidimensional arrays per FAQ 2.14 doesnt work!

6. dynamically allocating a multidimensional array

7. Problem De-allocating Memory in variable sized arrays

8. memory allocation of multidimensional array and permutation maker

9. How to calculate memory address for a multidimensional array

10. How are multidimensional array stored in memory?

11. Multidimensional Arrays and Dynamic Memory

12. How to write memory allocator for multidimensional arrays?

 

 
Powered by phpBB® Forum Software