This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / Help! A strange error in my code in Unix.
-annabella(anna);
2003-2-6
{637}
(#1023968@0)
-
Try thisint **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;
}
-benii(自私且不爱国的搬运工);
2003-2-6
{303}
(#1024251@0)
-
Sorry, change all the NEWs to malloc
-benii(自私且不爱国的搬运工);
2003-2-6
(#1024253@0)
-
看了一下程序,没看出什么问题,在linux 上运行了一下,也没问题。
我估计你是其他代码有bug, 比如指针错误,内存越界等,当你使用
malloc 时这个bug 才暴露出来。
-holdon(try again);
2003-2-6
(#1024259@0)
-
http://bbs.ee.ntu.edu.tw/boards/Programming/11/4.html你的机器应该不是intel 的机器。
-holdon(try again);
2003-2-6
{30}
(#1024276@0)
-
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 .
-yuanzidan(原子弹);
2003-2-6
{63}
(#1024326@0)
-
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.
-freshair(Happy pig);
2003-2-6
(#1024448@0)
-
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.
-annabella(anna);
2003-2-6
(#1024634@0)
-
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
-freshair(Happy pig);
2003-2-6
{404}
(#1024735@0)
-
可以用下面的代码检测一下你机器的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);
-holdon(try again);
2003-2-6
{406}
(#1024837@0)