qsort clobbering内存位置 [英] qsort clobbering memory location

查看:71
本文介绍了qsort clobbering内存位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题。代码不是由我编写的,而是在stdlib中使用

qsort函数。总是,第四次通过,变量列表(即mem位置= 41813698)的内存变为11,

然后程序崩溃。显然qsort可能是我用b $ b覆盖了用过的内存位置。奇怪的是它是第四次运行程序它是

。下面是代码


内存位置OK在这里

for(i = 0; i< list-> in_use; i ++){

/ *

*设置排列表

* /

for(k = 0; k< ciCount; k ++ ){

ptab [k] = k;

}

if(!table [i])

{

printf(NULL Table call.\ n);

return;

}

linkTable = table [i];

// printf("%x \ n",linkTable);

// printf("%i \ n" ;,cmpPT);

// _ quicksort(ptab,ciCount,sizeof(int32),cmpPT);

qsort(ptab,ciCount,sizeof(int32),cmpPT) ;


printf("%x \ n",list);不行!!!!!!!!

// printf("%i \ n",cmp);

qsort(表[ i],ciCount,sizeof(int32),cmp);

for(k = 0,j = 0; j< ciCount; j ++){

if(table [i] [k]!= table [i] [j]){

k = k + 1;

table [i] [k] = table [i] [j];

}

/ *

*在排列表中镜像压缩

* /

permuTab [i] [ptab [j]] = k;

}


printf("%x \ n" ;,列表);不行!!!!!!!!!!!!!!

printf(压缩完成%i。\ n,k);

table [i] [k + 1] = -1; / *表格结束标记* /

sizeTab [i] = k + 1;

条目+ = k + 1;

printf( 在Blah之前;;


printf(" In use%x \ n",list);不行!!!!!!!!!!!!!!!!

printf(" Blah \ n");

}

我尝试将列表作为静态和非静态列表但总是全球化的。这可能是问题吗?任何帮助都会尽快受到青睐!

I have a strange problem. The code isn''t written by me, but uses the
qsort function in stdlib. ALWAYS, the fourth time through, the memory
location of variable list (i.e. mem location = 41813698) becomes 11,
then the program crashes. It is obviously qsort that may me
overwritten the used memory location. The weird thing is that it is
ALWAYS the fourth time running the program. Below is the code

MEMORY LOCATION OK HERE
for (i = 0; i < list->in_use; i++) {
/*
* Set up the permutation table
*/
for (k = 0; k < ciCount; k++) {
ptab[k] = k;
}
if(!table[i])
{
printf("NULL Table call.\n");
return;
}
linkTable = table[i];
//printf("%x\n", linkTable);
//printf("%i\n", cmpPT);
//_quicksort (ptab, ciCount, sizeof(int32), cmpPT);
qsort (ptab, ciCount, sizeof(int32), cmpPT);

printf("%x\n", list); NOT OK!!!!!!!!

//printf("%i\n", cmp);
qsort (table[i], ciCount, sizeof(int32), cmp);
for (k = 0, j = 0; j < ciCount; j++) {
if (table[i][k] != table[i][j]) {
k = k + 1;
table[i][k] = table[i][j];
}
/*
* Mirror the compression in the permutation table
*/
permuTab[i][ptab[j]] = k;
}

printf("%x\n", list); NOT OK!!!!!!!!!!!!!!

printf("Compression Complete %i.\n", k);
table[i][k+1] = -1; /* End of table Marker */
sizeTab[i] = k+1;
entries += k+1;
printf("Before Blah\n");

printf("In use %x\n", list); NOT OK!!!!!!!!!!!!!!!!

printf("Blah\n");
}
I tried list as static and "unstatic" but always global. Could that
be the problem? Any help would be greatly appreciated ASAP!

推荐答案

William Buch写道:
William Buch wrote:
我有一个奇怪的问题。代码不是由我编写的,而是在stdlib中使用
qsort函数。总是,第四次,变量列表的内存位置(即mem位置= 41813698)变为11,然后程序崩溃。显然qsort可能会覆盖已使用的内存位置。奇怪的是,它始终是第四次运行该程序。以下是代码

MEMORY LOCATION OK HERE
for(i = 0; i< list-> in_use; i ++){
/ *
* Set排列表
* /
for(k = 0; k< ciCount; k ++){
ptab [k] = k;
}
if( !table [i])
{
printf(" NULL Table call.\\\
);;
return;
}
linkTable = table [i] ;
// printf("%x \ n",linkTable);
// printf("%i \ n",cmpPT);
// _ quicksort(ptab ,ciCount,sizeof(int32),cmpPT);
qsort(ptab,ciCount,sizeof(int32),cmpPT);

printf("%x \ n",list) ;不行!!!!!!!!
// printf("%i \ n",cmp);
qsort(table [i],ciCount,sizeof(int32) ),cmp);
for(k = 0,j = 0; j< ciCount; j ++){
if(table [i] [k]!= table [i] [j]) {
k = k + 1;
table [i] [k] = table [i] [j];
}
/ *
*镜像压缩在排列表中
* /
permuTab [i] [ptab [j]] = k;
}

printf("%x \ n" ,列表);不行!!!!!!!!!!!!!!

printf(压缩完成%i。\ n,k);
表[i] [ k + 1] = -1; / *表格结束标记* /
sizeTab [i] = k + 1;
条目+ = k + 1;
printf(在Blah \ n之前);

printf(" In use%x \ n",list);不行!!!!!!!!!!!!!!!!

printf(" Blah \ nn);
}

我尝试将列表作为静态和非静态列表但总是全球化的。这可能是问题吗?任何帮助都会尽快受到青睐!
I have a strange problem. The code isn''t written by me, but uses the
qsort function in stdlib. ALWAYS, the fourth time through, the memory
location of variable list (i.e. mem location = 41813698) becomes 11,
then the program crashes. It is obviously qsort that may me
overwritten the used memory location. The weird thing is that it is
ALWAYS the fourth time running the program. Below is the code

MEMORY LOCATION OK HERE
for (i = 0; i < list->in_use; i++) {
/*
* Set up the permutation table
*/
for (k = 0; k < ciCount; k++) {
ptab[k] = k;
}
if(!table[i])
{
printf("NULL Table call.\n");
return;
}
linkTable = table[i];
//printf("%x\n", linkTable);
//printf("%i\n", cmpPT);
//_quicksort (ptab, ciCount, sizeof(int32), cmpPT);
qsort (ptab, ciCount, sizeof(int32), cmpPT);

printf("%x\n", list); NOT OK!!!!!!!!

//printf("%i\n", cmp);
qsort (table[i], ciCount, sizeof(int32), cmp);
for (k = 0, j = 0; j < ciCount; j++) {
if (table[i][k] != table[i][j]) {
k = k + 1;
table[i][k] = table[i][j];
}
/*
* Mirror the compression in the permutation table
*/
permuTab[i][ptab[j]] = k;
}

printf("%x\n", list); NOT OK!!!!!!!!!!!!!!

printf("Compression Complete %i.\n", k);
table[i][k+1] = -1; /* End of table Marker */
sizeTab[i] = k+1;
entries += k+1;
printf("Before Blah\n");

printf("In use %x\n", list); NOT OK!!!!!!!!!!!!!!!!

printf("Blah\n");
}
I tried list as static and "unstatic" but always global. Could that
be the problem? Any help would be greatly appreciated ASAP!




嗯,很明显,程序中有一个错误。我可以问,如果没有看到

相关声明,谁应该知道问题在哪里?


提供可编译和运行的代码;它大大增强了你获得适当帮助的机会。


[要找的是一个一个的错误 - 一个品种其中是
写完一个数组的结尾。]


HTH,

--ag
< br $> b $ b -

Artie Gold - 德克萨斯州奥斯汀



Well, obviously there''s a bug in the program. How, may I ask, is
*anyone* supposed to know where the problem is without seeing the
relevant declarations?

Present code that can be compiled and run; it greatly enhances your
chance of receiving appropriate help.

[One thing to look for is `off-by-one'' errors -- one variety of which is
writing past the end of an array.]

HTH,
--ag

--
Artie Gold -- Austin, Texas


2004年1月26日14:46:47 -0800 , wl****@earthlink.net (William Buch)

在comp.lang中写道。 c:
On 26 Jan 2004 14:46:47 -0800, wl****@earthlink.net (William Buch)
wrote in comp.lang.c:
我有一个奇怪的问题。代码不是由我编写的,而是在stdlib中使用
qsort函数。总是,第四次,变量列表的内存位置(即mem位置= 41813698)变为11,然后程序崩溃。显然qsort可能会覆盖已使用的内存位置。奇怪的是,它始终是第四次运行该程序。以下是代码

MEMORY LOCATION OK HERE
for(i = 0; i< list-> in_use; i ++){


[snip]

printf("%x \ n",list);不好!!!!!!!!


[snip]

printf("%x \ n",list);不好!!!!!!!!!!!!!!


[snip]

printf(" In use%x \ n",list);不行!!!!!!!!!!!!!!!!
I have a strange problem. The code isn''t written by me, but uses the
qsort function in stdlib. ALWAYS, the fourth time through, the memory
location of variable list (i.e. mem location = 41813698) becomes 11,
then the program crashes. It is obviously qsort that may me
overwritten the used memory location. The weird thing is that it is
ALWAYS the fourth time running the program. Below is the code

MEMORY LOCATION OK HERE
for (i = 0; i < list->in_use; i++) {
[snip]
printf("%x\n", list); NOT OK!!!!!!!!
[snip]
printf("%x\n", list); NOT OK!!!!!!!!!!!!!!
[snip]
printf("In use %x\n", list); NOT OK!!!!!!!!!!!!!!!!




除非您上面的代码段的第一行有严重错误,否则

''list''是指向结构或联合类型的指针。将它传递给带有%x转换说明符的
printf()会产生不确定的行为。


这可能不是您问题的原因,它是可能在

你没有发布的代码。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



Unless the first line of your snippet above has a serious error,
''list'' is a pointer to a structure or union type. Passing it to
printf() with a %x conversion specifier produces undefined behavior.

This is not likely to be the cause of your problem, it is probably in
the code you didn''t post.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


William Buch写道:
William Buch wrote:

我有一个奇怪的问题。代码不是由我编写的,而是在stdlib中使用
qsort函数。总是,第四次,变量列表的内存位置(即mem位置= 41813698)变为11,然后程序崩溃。显然qsort可能会覆盖已使用的内存位置。奇怪的是它总是第四次运行程序。

I have a strange problem. The code isn''t written by me, but uses the
qsort function in stdlib. ALWAYS, the fourth time through, the memory
location of variable list (i.e. mem location = 41813698) becomes 11,
then the program crashes. It is obviously qsort that may me
overwritten the used memory location. The weird thing is that it is
ALWAYS the fourth time running the program.




因为你只能运行程序四次,一次,

(然后它成为第五次和第六次等等)

你是什么意思总是第四次运行程序?


-

pete



Since you can only run the program four times, once,
(then it becomes the fifth time and the sixth time, etc.)
what do you mean by "ALWAYS the fourth time running the program"?

--
pete


这篇关于qsort clobbering内存位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆