Dynamic memory allocation of two dimensional arrays 
Author Message
 Dynamic memory allocation of two dimensional arrays

Hi,
I am allocating memory for a two dimension array as in KB article Q104639.
But while doing free, it is giving an exception. It stops in _free_dbg_lk
while checking no-mans-land gaps! The code fragment is as follows:
==>
  strArray= (char**)malloc(rows*sizeof(char));//new char[50];
  if(strArray==NULL)
  {
    cout << "Not enough memory"<<endl;
    return nRetCode;
  }

  for(int i=0;i<rows;i++)
  {
    strArray[i]=(char*) malloc(sizeof(char)*cols);
    if(strArray[i]==NULL)
    {
      cout << "Not enough memory"<<endl;
      return nRetCode;
    }
  }
  strcpy(strArray[0],"12345678901");
  strcpy(strArray[1],"wrwefew");
  strcpy(strArray[2],"ertetrt");

  for(i=0;i<rows;i++)
    free(strArray[i]);

  free(strArray);
==>

Any suggestions, am I missing something?

Thanks & Regards,
Satish



Wed, 10 Sep 2003 03:02:59 GMT  
 Dynamic memory allocation of two dimensional arrays
This

  strArray= (char**)malloc(rows * sizeof(char));

should be

  strArray= (char**)malloc(rows * sizeof(char*));

You want to allocate an array of pointers, not an array of characters.
--
With best wishes,
    Igor Tandetnik


Quote:
> Hi,
> I am allocating memory for a two dimension array as in KB article Q104639.
> But while doing free, it is giving an exception. It stops in _free_dbg_lk
> while checking no-mans-land gaps! The code fragment is as follows:
> ==>
>   strArray= (char**)malloc(rows*sizeof(char));//new char[50];
>   if(strArray==NULL)
>   {
>     cout << "Not enough memory"<<endl;
>     return nRetCode;
>   }

>   for(int i=0;i<rows;i++)
>   {
>     strArray[i]=(char*) malloc(sizeof(char)*cols);
>     if(strArray[i]==NULL)
>     {
>       cout << "Not enough memory"<<endl;
>       return nRetCode;
>     }
>   }
>   strcpy(strArray[0],"12345678901");
>   strcpy(strArray[1],"wrwefew");
>   strcpy(strArray[2],"ertetrt");

>   for(i=0;i<rows;i++)
>     free(strArray[i]);

>   free(strArray);
> ==>

> Any suggestions, am I missing something?

> Thanks & Regards,
> Satish



Wed, 10 Sep 2003 03:12:19 GMT  
 Dynamic memory allocation of two dimensional arrays
Thanks Igor!

Regards,
Satish


Quote:
> This

>   strArray= (char**)malloc(rows * sizeof(char));

> should be

>   strArray= (char**)malloc(rows * sizeof(char*));

> You want to allocate an array of pointers, not an array of characters.
> --
> With best wishes,
>     Igor Tandetnik



> > Hi,
> > I am allocating memory for a two dimension array as in KB article
Q104639.
> > But while doing free, it is giving an exception. It stops in
_free_dbg_lk
> > while checking no-mans-land gaps! The code fragment is as follows:
> > ==>
> >   strArray= (char**)malloc(rows*sizeof(char));//new char[50];
> >   if(strArray==NULL)
> >   {
> >     cout << "Not enough memory"<<endl;
> >     return nRetCode;
> >   }

> >   for(int i=0;i<rows;i++)
> >   {
> >     strArray[i]=(char*) malloc(sizeof(char)*cols);
> >     if(strArray[i]==NULL)
> >     {
> >       cout << "Not enough memory"<<endl;
> >       return nRetCode;
> >     }
> >   }
> >   strcpy(strArray[0],"12345678901");
> >   strcpy(strArray[1],"wrwefew");
> >   strcpy(strArray[2],"ertetrt");

> >   for(i=0;i<rows;i++)
> >     free(strArray[i]);

> >   free(strArray);
> > ==>

> > Any suggestions, am I missing something?

> > Thanks & Regards,
> > Satish



Wed, 10 Sep 2003 03:36:05 GMT  
 Dynamic memory allocation of two dimensional arrays

Quote:

>Hi,
>I am allocating memory for a two dimension array as in KB article Q104639.
>But while doing free, it is giving an exception. It stops in _free_dbg_lk
>while checking no-mans-land gaps! The code fragment is as follows:
>==>
>  strArray= (char**)malloc(rows*sizeof(char));//new char[50];

Should be sizeof(char*).

Quote:
>  if(strArray==NULL)
>  {
>    cout << "Not enough memory"<<endl;
>    return nRetCode;
>  }

>  for(int i=0;i<rows;i++)
>  {
>    strArray[i]=(char*) malloc(sizeof(char)*cols);

Can be just cols. sizeof(char) == 1 by definition.

Quote:
>    if(strArray[i]==NULL)
>    {
>      cout << "Not enough memory"<<endl;
>      return nRetCode;

You'll get memory leaks here.

Quote:
>    }
>  }
>  strcpy(strArray[0],"12345678901");
>  strcpy(strArray[1],"wrwefew");
>  strcpy(strArray[2],"ertetrt");

>  for(i=0;i<rows;i++)
>    free(strArray[i]);

>  free(strArray);
>==>

>Any suggestions, am I missing something?

There are more efficient ways to do this. An easy alternative is to
allocate one big block of size rows*cols*sizeof(your_type) and make
the array of pointers point to the proper rows inside it. You can go
all the way and put the array of pointers at the front of this block,
but then you have to worry about alignment of the data following the
pointer array. Of course, you really don't need the pointer array at
all; it just makes the notation a bit more natural. Assuming you have
that big block of data just mentioned, you can calculate the 1D index
of the object at (i,j) with i*cols+j. If you write a 2D array class
template, then you can have:

T* Matrix::operator[](int row)
{
   // Return address of the row
   return p_data+row*n_cols;

Quote:
}

Matrix<int> m(10,20);
m[4][5] = 0;

The first subscript calls Matrix::operator[], while the second applies
to the pointer returned by this function.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Wed, 10 Sep 2003 03:42:57 GMT  
 Dynamic memory allocation of two dimensional arrays

Quote:
> Hi,
> I am allocating memory for a two dimension array as in KB article Q104639.
> But while doing free, it is giving an exception. It stops in _free_dbg_lk
> while checking no-mans-land gaps! The code fragment is as follows:
> ==>
>   strArray= (char**)malloc(rows*sizeof(char));//new char[50];

should be sizeof(char*)

Quote:
>   if(strArray==NULL)
>   {
>     cout << "Not enough memory"<<endl;
>     return nRetCode;
>   }

>   for(int i=0;i<rows;i++)
>   {
>     strArray[i]=(char*) malloc(sizeof(char)*cols);
>     if(strArray[i]==NULL)
>     {
>       cout << "Not enough memory"<<endl;
>       return nRetCode;
>     }
>   }
>   strcpy(strArray[0],"12345678901");
>   strcpy(strArray[1],"wrwefew");
>   strcpy(strArray[2],"ertetrt");

>   for(i=0;i<rows;i++)
>     free(strArray[i]);

>   free(strArray);
> ==>

> Any suggestions, am I missing something?
yes an asterisk

> Thanks & Regards,
> Satish



Wed, 10 Sep 2003 04:23:53 GMT  
 Dynamic memory allocation of two dimensional arrays


Quote:
>   strArray= (char**)malloc(rows*sizeof(char));//new char[50];

The above line allocates one byte per row.
I'm guessing you want to allocate space for one pointer per row.

strArray=(char**)malloc(rows*sizeof(char*)); // new char[][50]

Quote:
>   if(strArray==NULL)
>   {
>     cout << "Not enough memory"<<endl;
>     return nRetCode;
>   }

>   for(int i=0;i<rows;i++)
>   {
>     strArray[i]=(char*) malloc(sizeof(char)*cols);
>     if(strArray[i]==NULL)
>     {
>       cout << "Not enough memory"<<endl;
>       return nRetCode;
>     }
>   }
>   strcpy(strArray[0],"12345678901");
>   strcpy(strArray[1],"wrwefew");
>   strcpy(strArray[2],"ertetrt");

>   for(i=0;i<rows;i++)
>     free(strArray[i]);

>   free(strArray);
> ==>

> Any suggestions, am I missing something?

> Thanks & Regards,
> Satish



Wed, 10 Sep 2003 04:41:26 GMT  
 Dynamic memory allocation of two dimensional arrays
In addition to all the other suggestions, might I suggest using the STL/C++
Standard Library classes to achieve this effect instead?

For example, a dynamic two dimensional array of characters would look like
this:

std::vector<std::vector<char> > strArray;

or, since it's really an array of strings you're after:

std::vector<std::string> strArray;

use:
#include <vector>

for the vector template and

#include <string>

for the string template.

You can then dispense entirely with "new" and "delete" or "malloc" and
"free".

HTH

--
Reginald Blue                   | Opinions expressed here do not
Natural Language Understanding  | necessarily represent those of
Unisys Corporation              | my employer.
--------------------------------+-------------------------------

NL technology,speech application| My email address is wrong, you
development training, see:      | need to remove the obvious.
http://www.speechdepot.com/     +-------------------------------


Quote:
> Hi,
> I am allocating memory for a two dimension array as in KB article Q104639.
> But while doing free, it is giving an exception. It stops in _free_dbg_lk
> while checking no-mans-land gaps! The code fragment is as follows:
> ==>
>   strArray= (char**)malloc(rows*sizeof(char));//new char[50];
>   if(strArray==NULL)
>   {
>     cout << "Not enough memory"<<endl;
>     return nRetCode;
>   }

>   for(int i=0;i<rows;i++)
>   {
>     strArray[i]=(char*) malloc(sizeof(char)*cols);
>     if(strArray[i]==NULL)
>     {
>       cout << "Not enough memory"<<endl;
>       return nRetCode;
>     }
>   }
>   strcpy(strArray[0],"12345678901");
>   strcpy(strArray[1],"wrwefew");
>   strcpy(strArray[2],"ertetrt");

>   for(i=0;i<rows;i++)
>     free(strArray[i]);

>   free(strArray);
> ==>

> Any suggestions, am I missing something?

> Thanks & Regards,
> Satish



Wed, 10 Sep 2003 06:24:19 GMT  
 Dynamic memory allocation of two dimensional arrays
Thanks Doug. If I can use a single block of memory, I can avoid much of
exception handling portion (like the one you have indicated for memory
leak). But, as my cols is not a constant (string length varies), it will be
complicated to get the row index.

Thanks & Regards,
Satish



Quote:

> >Hi,
> >I am allocating memory for a two dimension array as in KB article
Q104639.
> >But while doing free, it is giving an exception. It stops in _free_dbg_lk
> >while checking no-mans-land gaps! The code fragment is as follows:
> >==>
> >  strArray= (char**)malloc(rows*sizeof(char));//new char[50];

> Should be sizeof(char*).

> >  if(strArray==NULL)
> >  {
> >    cout << "Not enough memory"<<endl;
> >    return nRetCode;
> >  }

> >  for(int i=0;i<rows;i++)
> >  {
> >    strArray[i]=(char*) malloc(sizeof(char)*cols);

> Can be just cols. sizeof(char) == 1 by definition.

> >    if(strArray[i]==NULL)
> >    {
> >      cout << "Not enough memory"<<endl;
> >      return nRetCode;

> You'll get memory leaks here.

> >    }
> >  }
> >  strcpy(strArray[0],"12345678901");
> >  strcpy(strArray[1],"wrwefew");
> >  strcpy(strArray[2],"ertetrt");

> >  for(i=0;i<rows;i++)
> >    free(strArray[i]);

> >  free(strArray);
> >==>

> >Any suggestions, am I missing something?

> There are more efficient ways to do this. An easy alternative is to
> allocate one big block of size rows*cols*sizeof(your_type) and make
> the array of pointers point to the proper rows inside it. You can go
> all the way and put the array of pointers at the front of this block,
> but then you have to worry about alignment of the data following the
> pointer array. Of course, you really don't need the pointer array at
> all; it just makes the notation a bit more natural. Assuming you have
> that big block of data just mentioned, you can calculate the 1D index
> of the object at (i,j) with i*cols+j. If you write a 2D array class
> template, then you can have:

> T* Matrix::operator[](int row)
> {
>    // Return address of the row
>    return p_data+row*n_cols;
> }

> Matrix<int> m(10,20);
> m[4][5] = 0;

> The first subscript calls Matrix::operator[], while the second applies
> to the pointer returned by this function.

> --
> Doug Harrison [VC++ MVP]
> Eluent Software, LLC
> http://www.eluent.com
> Tools for Visual C++ and Windows



Wed, 10 Sep 2003 08:57:49 GMT  
 Dynamic memory allocation of two dimensional arrays
Thanks Reginald! I would like to avoid the char*** ! I will take a look into
the STL.

Thanks & Regards,
Satish


Quote:
> In addition to all the other suggestions, might I suggest using the
STL/C++
> Standard Library classes to achieve this effect instead?

> For example, a dynamic two dimensional array of characters would look like
> this:

> std::vector<std::vector<char> > strArray;

> or, since it's really an array of strings you're after:

> std::vector<std::string> strArray;

> use:
> #include <vector>

> for the vector template and

> #include <string>

> for the string template.

> You can then dispense entirely with "new" and "delete" or "malloc" and
> "free".

> HTH

> --
> Reginald Blue                   | Opinions expressed here do not
> Natural Language Understanding  | necessarily represent those of
> Unisys Corporation              | my employer.
> --------------------------------+-------------------------------

> NL technology,speech application| My email address is wrong, you
> development training, see:      | need to remove the obvious.
> http://www.speechdepot.com/     +-------------------------------



> > Hi,
> > I am allocating memory for a two dimension array as in KB article
Q104639.
> > But while doing free, it is giving an exception. It stops in
_free_dbg_lk
> > while checking no-mans-land gaps! The code fragment is as follows:
> > ==>
> >   strArray= (char**)malloc(rows*sizeof(char));//new char[50];
> >   if(strArray==NULL)
> >   {
> >     cout << "Not enough memory"<<endl;
> >     return nRetCode;
> >   }

> >   for(int i=0;i<rows;i++)
> >   {
> >     strArray[i]=(char*) malloc(sizeof(char)*cols);
> >     if(strArray[i]==NULL)
> >     {
> >       cout << "Not enough memory"<<endl;
> >       return nRetCode;
> >     }
> >   }
> >   strcpy(strArray[0],"12345678901");
> >   strcpy(strArray[1],"wrwefew");
> >   strcpy(strArray[2],"ertetrt");

> >   for(i=0;i<rows;i++)
> >     free(strArray[i]);

> >   free(strArray);
> > ==>

> > Any suggestions, am I missing something?

> > Thanks & Regards,
> > Satish



Wed, 10 Sep 2003 08:59:44 GMT  
 Dynamic memory allocation of two dimensional arrays

Quote:

>Thanks Doug. If I can use a single block of memory, I can avoid much of
>exception handling portion (like the one you have indicated for memory
>leak). But, as my cols is not a constant (string length varies), it will be
>complicated to get the row index.

Right, the techniques I presented are for a two dimensional array, not
a 1D array of variable length arrays.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Thu, 11 Sep 2003 05:04:46 GMT  
 Dynamic memory allocation of two dimensional arrays

Quote:

> Hi,
> I am allocating memory for a two dimension array as in KB article Q104639.
> But while doing free, it is giving an exception. It stops in _free_dbg_lk
> while checking no-mans-land gaps! The code fragment is as follows:
> ==>
>   strArray= (char**)malloc(rows*sizeof(char));//new char[50];*************
> [snip]
> Thanks & Regards,
> Satish

The rows store a pointer to array of columns as allocated by
strArray[i]=(char*) malloc(sizeof(char)*cols);
and must be allocated with:
strArray= (char**)malloc(rows*sizeof(char *));//new char[50];
                                     ------
HTH


Wed, 17 Sep 2003 01:27:47 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Dynamic allocation and reallocation of two dimensional arrays

2. Memory allocation of two-dimensional array

3. Memory allocation in two dimensional array

4. Multi-dimensional array using dynamic memory allocation

5. Dynamic Memory Allocation For Multi-Dimensional Arrays

6. Multi-dimensional array using dynamic memory allocation

7. Two dimensional dynamic allocation using malloc

8. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

9. Dynamic allocation of 2-dimensional array

10. Dynamic allocation 2 dimensional array

11. Dynamic Allocation of 2-Dimensional Arrays

12. help with function to create dynamic two-dimensional char array

 

 
Powered by phpBB® Forum Software