函数内的Seg Fault(C的新增功能) [英] Seg Fault within function (New to C)

查看:79
本文介绍了函数内的Seg Fault(C的新增功能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有以下标题为cleanSpace的函数,它接收了一个

字符串并清理它。我的程序通过

cleanSpace(c)将字符串传递给函数。该功能第一次通过,但

第二次通过我在第二行

最后一行strcpy上收到一个seg故障[core dump]。


我确定指针和我使用它的方式有问题。

有人可以确定我在那条线上做错了吗?


int cleanSpace(char * ptr)

{

char temp [300];

strcpy(temp,ptr) ;


int i = 0,

j = 0,

k = 0,

m = 0 ;


/ *最终这个循环将扫描新的行和标签并用空格替换



。 * /

for(i = 0; temp [i]; i ++)

{

if(temp [i] =='' \ n''|| temp [i] ==''\ t'')

temp [i] ='''';

}


// For循环找到角色起点。

for(i = 0; temp [i] ==''''; i ++)

{

temp [m] = temp [i + 1];

}


// For循环移动第一个找到的字符旁边的所有字符。

for(i ++; temp [i]; i ++)

{

temp [++ m] = temp [i];

}

temp [m + 1] =''\ 0'';


// for循环删除尾随空格。

for(i = strlen(temp) - 1; temp [i] ==''''; i--)

{

temp [i] =''\ 0'';

}


// For循环删除多余的空间。

for(i = 0; temp [i]; i ++)

{

if(temp [i] ==' '''&& temp [i + 1] =='''')

{

j = i;


while(te mp [j] =='''')

{

j ++;

}


代表(k = i + 1;温度[K]; k ++,j ++)

{

temp [k] = temp [j];

}

j = 0 ;

}

}

strcpy(ptr,temp); //将temp复制到ptr

return strlen(temp); //返回长度

}


感谢任何反馈


- Aaron T

Hello. I have the following function titled cleanSpace that recieves a
string and cleans it up. My program passes a string to the function via
"cleanSpace(c)". The function works the first time through, but the
second time through I recieve a seg fault [core dump] on the second
last line, strcpy.

I am certain something is wrong with the pointer and the way I use it.
Could somebody identify what I have done wrong at that line?

int cleanSpace(char* ptr)
{
char temp[300];
strcpy(temp, ptr);

int i=0,
j=0,
k=0,
m=0;

/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; temp[i]; i++)
{
if(temp[i] == ''\n'' || temp[i] == ''\t'')
temp[i] = '' '';
}

// For loop finds character starting point.
for(i=0; temp[i] == '' ''; i++)
{
temp[m] = temp[i+1];
}

// For loop moves all characters next to the first found character.
for(i++; temp[i]; i++)
{
temp[++m] = temp[i];
}
temp[m+1] = ''\0'';

// For loop removes trailing spaces.
for(i = strlen(temp) - 1; temp[i] == '' ''; i--)
{
temp[i] = ''\0'';
}

// For loop removes excess spaces.
for(i = 0; temp[i]; i++)
{
if(temp[i] == '' '' && temp[i+1] == '' '')
{
j = i;

while(temp[j] == '' '')
{
j++;
}

for(k = i + 1; temp[k]; k++, j++)
{
temp[k] = temp[j];
}
j=0;
}
}
strcpy(ptr,temp); // Copy temp to ptr
return strlen(temp); // Return the length
}

Appreciate any feedback

- Aaron T

推荐答案

AMT2K5于17/07/05写道:
AMT2K5 wrote on 17/07/05 :
你好。我有以下标题为cleanSpace的函数,它接收一个
字符串并清理它。我的程序通过
cleanSpace(c)将字符串传递给函数。该功能第一次通过,但第二次通过我在第二行
最后一行收到一个seg故障[core dump],strcpy。
Hello. I have the following function titled cleanSpace that recieves a
string and cleans it up. My program passes a string to the function via
"cleanSpace(c)". The function works the first time through, but the
second time through I recieve a seg fault [core dump] on the second
last line, strcpy.




代码是正确的,但这些副本没用。确保

原始字符串是可变的(字符串文字不是)。另外,尝试减少变量范围。
减少变量范围。它准备模块化......


这个有效:


#include< stdio.h>

#include< string.h>


int cleanSpace(char * ptr)

{

int m = 0;


/ *最终这个循环将扫描新的线条和标签,并用空格替换

*它们。

* /

{

int i;

for(i = 0; ptr [i]; i ++)

{

if(ptr [i] ==''\ n''|| ptr [i] ==''\ t'')

{

ptr [i] ='''';

}

}

}


/ * For循环找到角色起点。 * /

{

int i;

for(i = 0; ptr [i] ==''''; i ++)

{

ptr [m] = ptr [i + 1];

}


/ * For循环移动第一个找到的

字符旁边的所有字符。 * /

for(i ++; ptr [i]; i ++)

{

ptr [++ m] = ptr [i];

}

ptr [m + 1] =''\ 0'';

}


/ * For循环删除尾随空格。 * /

{

int i;

for(i = strlen(ptr) - 1; ptr [i] =='''' ';我 - )

{

ptr [i] =''\ 0'';

}

}


/ * For循环删除多余的空格。 * /

{

int i;

for(i = 0; ptr [i]; i ++)

{

int j;

if(ptr [i] ==''''&& ptr [i + 1] =='''')

{

j = i;


while(ptr [j] =='''')

{

j ++;

}


{

int k = 0;

for(k = i + 1; ptr [k]; k ++,j ++)

{

ptr [k] = ptr [j] ;

}

j = 0;

}

}

}

}

/ *返回长度* /

返回strlen(ptr);

}


int main(无效)

{

char s [] ="清洁空间测试字符串;

int n = cleanSpace(s);


printf(" n =%ds =''%s'' \ n",n,s);


返回0;

}


-

Emmanuel

C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


显然你的代码不符合原始规格。

你用湿面条判处30睫毛。

- Jerry Coffin in alcc ++



The code is correct, but these copies are useless. Be sure that the
original string is mutable (A string literal is not). Also, try to
reduce the variables scope. It prepares to modularization...

This works :

#include <stdio.h>
#include <string.h>

int cleanSpace (char *ptr)
{
int m = 0;

/* Ultimately this loop will scan for new lines and tabs and replace
* them with spaces.
*/
{
int i;
for (i = 0; ptr[i]; i++)
{
if (ptr[i] == ''\n'' || ptr[i] == ''\t'')
{
ptr[i] = '' '';
}
}
}

/* For loop finds character starting point. */
{
int i;
for (i = 0; ptr[i] == '' ''; i++)
{
ptr[m] = ptr[i + 1];
}

/* For loop moves all characters next to the first found
character. */
for (i++; ptr[i]; i++)
{
ptr[++m] = ptr[i];
}
ptr[m + 1] = ''\0'';
}

/* For loop removes trailing spaces. */
{
int i;
for (i = strlen (ptr) - 1; ptr[i] == '' ''; i--)
{
ptr[i] = ''\0'';
}
}

/* For loop removes excess spaces. */
{
int i;
for (i = 0; ptr[i]; i++)
{
int j;
if (ptr[i] == '' '' && ptr[i + 1] == '' '')
{
j = i;

while (ptr[j] == '' '')
{
j++;
}

{
int k = 0;
for (k = i + 1; ptr[k]; k++, j++)
{
ptr[k] = ptr[j];
}
j = 0;
}
}
}
}
/* Return the length */
return strlen (ptr);
}

int main (void)
{
char s[] = " Clean space test string ";
int n = cleanSpace (s);

printf ("n=%d s=''%s''\n", n, s);

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++





On Sun,17 Jul 2005 10:02:06 -0700,AMT2K5写道:
Hi,

On Sun, 17 Jul 2005 10:02:06 -0700, AMT2K5 wrote:
strcpy(temp,ptr);
strcpy(temp, ptr);




这是没用的,因为Emmanuel Delahaye已经告诉过你了,另外,strcpy < br $> b $ b shou永远不会被使用。作为一个很好的安全实践,你应该总是使用strncpy来防止愚蠢的崩溃和潜在的安全性。

瑕疵。

-

问候,

AH



This is useless as Emmanuel Delahaye already told you, besides, strcpy
should never be used. As a good security practice, you should always
be using strncpy which will prevent silly crashes and potential security
flaws.
--
Greetings,
A.H.


感谢Arthur和Emannuel的帮助。


我可以在函数中放置一个条件来测试

incomming字符串是否需要更改或者是一个常量字符串

literal?我认为这也是为什么我的程序也会导致上面的代码出现段错误的原因。

它应该在字符串上作用不是。

Thanks for the help Arthur and Emannuel.

Is there a condition I can place in the function to test if the
incomming string does not need changing or is a constant string
literal? I think this is also why my program causes a seg fault with
the above code as well.

It''s acting on the string when it should not be.


这篇关于函数内的Seg Fault(C的新增功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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