如何给二维数组分配内存空间

兔子0204 2013-07-16 05:44:27
#include <iostream>   
using namespace std;
//分配内存空间
int **a;
a=(int **)malloc(1000*sizeof(int*));
for(int iw=0;iw<1000;iw++)
a[iw]=(int*)malloc(1000*sizeof(int));

void f(int *p, int row, int col);

int main(void)
{
int a[1000][1000];
//....给数组a[][]赋值...
f(&a[0][0], 20, 30);
return (0);
}
void f(int *p, int row, int col)
{
for (int i=0; i!=row; ++i)
{
p=p+i+1;
for (int j=0; j!=col; ++j)
{
if(*p!=0)
num++;
p=p+1;
}

}
}

给二维数组a[1000][1000]分配内存空间部分有问题,应如何分配?
...全文
418 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2013-07-18
  • 打赏
  • 举报
回复
引用 14 楼 wangyaninglm 的回复:
看起来,挺麻烦的。。。
权衡一下,选一种就是了。
hlyces 2013-07-17
  • 打赏
  • 举报
回复
malloc(1000*1000*sizeof(int));//分配一次就好了
橡木疙瘩 2013-07-17
  • 打赏
  • 举报
回复
更正:使用int**的方法,需要分配(行数+1)次,释放(行数+1)次。
橡木疙瘩 2013-07-17
  • 打赏
  • 举报
回复
如果行数可变,可以用int(*)[1000]这种方法。 如果要列数可变,只能用int**,然后分配(列数+1)次,释放(列数+1)次,但这样你的f函数就必须修改,第一个参数要改为int**,然后用下标去访问,不能再假设a是连续存储。 如果又要列数可变,又要连续存储,则可以用一个int*,一次分配row_count * col_count * sizeof(int)大小的空间,然后访问时用a[ i * col_count + j ]代替a[i][j],也就是用一维数组模拟二维数组。
shiter 2013-07-17
  • 打赏
  • 举报
回复
看起来,挺麻烦的。。。
lm_whales 2013-07-17
  • 打赏
  • 举报
回复
引用 11 楼 u010936098 的回复:
更正:使用int**的方法,需要分配(行数+1)次,释放(行数+1)次。
也可以分配2 次,释放2次;这种方式对于小块内存利用率低; 但是更像C,C++二维数组,可以用一级指针自增的方式,遍历所有元素。 分配(行数+1)次,释放(行数+1),这种对于小块内存利用率比较高; 而用一级指针自增的方式,遍历所有元素就不可能了,所以和C,C++二维数组的相似度不高。
兔子0204 2013-07-16
  • 打赏
  • 举报
回复
引用 7 楼 u010936098 的回复:
其实f有两种更简单的实现:

#include <iostream>   
using namespace std; 

typedef int a1000_i_t[1000];

void f(a1000_i_t *p,  int row, int col);   

int main(void)   
{   
    a1000_i_t * a = (a1000_i_t*)malloc( 1000 * sizeof(a1000_i_t) );
    //....给数组a[][]赋值...
    f(&a[0][0], 20, 30); 
    free(a);  
    return (0);   
}   
void f(a1000_i_t *p,  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if(p[i][j]!=0) 
               num++;
        }  
    }   
}  


#include <iostream>   
using namespace std; 
  
void f1(int (*p)[1000],  int row, int col);   
void f2(int (*p)[1000][1000],  int row, int col);   
  
int main(void)   
{   
    typedef int a_1000_1000_i_t[1000][1000];
    a_1000_1000_i_t * a = (a_1000_1000_i_t*)malloc( 1000 * sizeof(a1000_i_t) );
    //....给数组(*a)[][]赋值...
    f1(*a, 20, 30); 
    f2(a, 20, 30);
    free(a);
    return (0);   
}   
void f1(int (*p)[1000],  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if(p[i][j]!=0) 
               num++;
        }  
    }   
}  

void f2(int (*p)[1000][1000],  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if((*p)[i][j]!=0) 
               num++;
        }  
    }   
}  
之所以想用 int **a; a=(int **)malloc(1000*sizeof(int*)); for(int iw=0;iw<1000;iw++) a[iw]=(int*)malloc(1000*sizeof(int)); 是因为a数组的维数1000可以用变量m,n代替。如果用您的方法,可以用于维数是变量的数组吗?
buyong 2013-07-16
  • 打赏
  • 举报
回复
用stl的话,还不如vector<vector<int> >
橡木疙瘩 2013-07-16
  • 打赏
  • 举报
回复
其实f有两种更简单的实现:

#include <iostream>   
using namespace std; 

typedef int a1000_i_t[1000];

void f(a1000_i_t *p,  int row, int col);   

int main(void)   
{   
    a1000_i_t * a = (a1000_i_t*)malloc( 1000 * sizeof(a1000_i_t) );
    //....给数组a[][]赋值...
    f(&a[0][0], 20, 30); 
    free(a);  
    return (0);   
}   
void f(a1000_i_t *p,  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if(p[i][j]!=0) 
               num++;
        }  
    }   
}  


#include <iostream>   
using namespace std; 
  
void f1(int (*p)[1000],  int row, int col);   
void f2(int (*p)[1000][1000],  int row, int col);   
  
int main(void)   
{   
    typedef int a_1000_1000_i_t[1000][1000];
    a_1000_1000_i_t * a = (a_1000_1000_i_t*)malloc( 1000 * sizeof(a1000_i_t) );
    //....给数组(*a)[][]赋值...
    f1(*a, 20, 30); 
    f2(a, 20, 30);
    free(a);
    return (0);   
}   
void f1(int (*p)[1000],  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if(p[i][j]!=0) 
               num++;
        }  
    }   
}  

void f2(int (*p)[1000][1000],  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if((*p)[i][j]!=0) 
               num++;
        }  
    }   
}  
橡木疙瘩 2013-07-16
  • 打赏
  • 举报
回复
其实还有一种方法,可能更好理解: 把int[1000][1000]定义为一种类型,然后从堆中分配一个这种类型的内存块,赋值给这种类型的指针:


#include <iostream>   
using namespace std; 
  
void f(int *p,  int row, int col);   
  
int main(void)   
{   
    typedef int a_1000_1000_i_t[1000][1000];
    a_1000_1000_i_t * a = (a_1000_1000_i_t*)malloc( 1000 * sizeof(a1000_i_t) );
    //....给数组a[][]赋值...
    //在C中访问该数组需要(*a)[i][j]
    //在C++中可以:
    a_1000_1000_i_t & ra = *a;
    //然后ra[i][j]
    f(&(*a)[0][0], 20, 30); 
    //在C++中可以f(&ra[0][0], 20, 30); 

    //上面的代码中忘了释放: 
    free(a);
    return (0);   
}   
void f(int *p,  int row, int col)   
{   
    //上面没注意到row和col都不是1000
    //应该象下面这样改:
    for (int i=0; i!=row; ++i)   
    {   
        int * temp = p + i * 1000;//row * col_count
        for (int j=0; j!=col; ++j)   
        {
            if(*temp!=0) 
               num++;
            temp=temp+1; 
        }  
         
    }   
}  

橡木疙瘩 2013-07-16
  • 打赏
  • 举报
回复
引用 4 楼 u011343898 的回复:
谢谢,假如我就想用malloc分配内存,那么二维数组作为参数的函数应当如何修改呢?
同样道理:

#include <iostream>   
using namespace std; 
  
void f(int *p,  int row, int col);   
  
int main(void)   
{   
    typedef int a1000_i_t[1000];
    a1000_i_t * a = (a1000_i_t*)malloc( 1000 * sizeof(a1000_i_t) );
    //....给数组a[][]赋值...
    f(&a[0][0], 20, 30);   
    return (0);   
}   
void f(int *p,  int row, int col)   
{   
    for (int i=0; i!=row; ++i)   
    {   
        for (int j=0; j!=col; ++j)   
        {
            if(*p!=0) 
               num++;
            p=p+1; 
        }  
         
    }   
}  
兔子0204 2013-07-16
  • 打赏
  • 举报
回复
引用 3 楼 u010936098 的回复:
看起来你的f函数是假设数组a是象标准二维数组那样连续存储的,那么就不能用“指针的指针”,而要用“数组的指针”:
typedef int a1000_i_t[1000];

a1000_i_t * a = new a1000_i_t[1000];
此外,f中应该有一处错误:
    for (int i=0; i!=row; ++i)   
    {   
        //p=p+i+1;  这一句应该去掉,否则会越界访问
        for (int j=0; j!=col; ++j)   
        {
            if(*p!=0) 
               num++;
            p=p+1; //这一句会被执行row * col次,足以遍历数组了。
        }  
谢谢,假如我就想用malloc分配内存,那么二维数组作为参数的函数应当如何修改呢?
橡木疙瘩 2013-07-16
  • 打赏
  • 举报
回复
看起来你的f函数是假设数组a是象标准二维数组那样连续存储的,那么就不能用“指针的指针”,而要用“数组的指针”:
typedef int a1000_i_t[1000];

a1000_i_t * a = new a1000_i_t[1000];
此外,f中应该有一处错误:
    for (int i=0; i!=row; ++i)   
    {   
        //p=p+i+1;  这一句应该去掉,否则会越界访问
        for (int j=0; j!=col; ++j)   
        {
            if(*p!=0) 
               num++;
            p=p+1; //这一句会被执行row * col次,足以遍历数组了。
        }  
兔子0204 2013-07-16
  • 打赏
  • 举报
回复
引用 1 楼 qzf362269994 的回复:
int * *a = new int*[1000]; for (int i = 0 ;i<1000 ; i++) { *(a+i) = new int[1000]; }
if(*p!=0)单步调试到此行,会出现expression cannot be evaluated。。。
qzf362269994 2013-07-16
  • 打赏
  • 举报
回复
int * *a = new int*[1000]; for (int i = 0 ;i<1000 ; i++) { *(a+i) = new int[1000]; }

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧