×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Help! A strange error in my code in Unix.

I use C in Unix OS. When I use "malloc" to allocate a space for a 2D array which is defined as **xx. It can't be allocated the size as I want. For example, if I want to give it 31*31 size, it is told "bus error". If I only give it 2*2, it passed. And if I allocate it in the beginning of the function, it passed. While if I allocate it where it should be, it appeared the error above.
I allocate it as this:
int **sub;
sub=(int**)malloc(31*sizeof(int *));
for(row=0;row<31;row++)
sub[row]=(int *)malloc(31*sizeof(int));


Could anybody tell me what's wrong in my code?

Any help would be greatly appreciated.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Help! A strange error in my code in Unix.
    I use C in Unix OS. When I use "malloc" to allocate a space for a 2D array which is defined as **xx. It can't be allocated the size as I want. For example, if I want to give it 31*31 size, it is told "bus error". If I only give it 2*2, it passed. And if I allocate it in the beginning of the function, it passed. While if I allocate it where it should be, it appeared the error above.
    I allocate it as this:
    int **sub;
    sub=(int**)malloc(31*sizeof(int *));
    for(row=0;row<31;row++)
    sub[row]=(int *)malloc(31*sizeof(int));


    Could anybody tell me what's wrong in my code?

    Any help would be greatly appreciated.
    • Try this
      int **CreateArray(int height, int width) {
      int **result = new int*[height];
      for (int i = 0; i<height;i++) {
      result[i]=new int[width];
      }
      return result;
      }


      void KillArray(int **Array, int height, int width) {
      for (int i = 0; i<height;i++) {
      delete[] Array[i];
      }
      delete[] Array;
      }
      • Sorry, change all the NEWs to malloc
    • 看了一下程序,没看出什么问题,在linux 上运行了一下,也没问题。 我估计你是其他代码有bug, 比如指针错误,内存越界等,当你使用 malloc 时这个bug 才暴露出来。
    • http://bbs.ee.ntu.edu.tw/boards/Programming/11/4.html
      你的机器应该不是intel 的机器。
    • Code looks ok, better check other staff. It runs well at SunOS 5.8 by gcc.
      Bus error is caused mainly due to the misaligned data members .
    • It's always better to check if malloc succeed before you try to use the pointer. I think it's because malloc failed. But we couldn't tell why malloc failed by just looking into this piece of code.
      • Thanks all! In fact, I used this section in other codes, and it runs well. And I run in a Sun OS. If there is possible that the code uses all memory? Because when I allocate little memory, it passed.
        • Well, it's not necessary to be the case that it uses all memory. Improper use of pointers might screw up the memory allocation.
          That's why I think first of all you have to check the pointer to see if malloc successfu or not. ( what if all memory used up?)

          Secondly, you have to look into other codes especially for pointers to see if other codes screwed up the whole thing. What you could do is that try to comment the other codes piece by piece see what happens and try to locate where this problem is caused.

          Hope it helps
    • 可以用下面的代码检测一下你机器的int 边界。 查查你的程序,有没有类似的转换: char *pch; int * pInt = (int *) pch;
      struct st1
      {
      char str_nouse[1];
      int nTest;
      }

      struct st2
      {
      char str_nouse[1];
      int *nTestPoint;
      }

      int nPointLen = sizeof( int *);
      int nIntLen = sizeof(int);
      int nIntBegin = sizeof(struct st1) - sizeof(int);
      int nPointBegin = sizeof(struct st2) - sizeof(int *);

      printf("len int:%d ip:%d \n",nIntLen,nPointLen );
      printf("begin int:%d ip:%d \n",nIntBegin,nPointBegin);