printf使用%s直接输出string类型,Program received signal SIGILL, Illegal instruction

来源:互联网 发布:莱汀rei 知乎 编辑:程序博客网 时间:2024/05/18 09:03
 

 

 printf使用%s直接输出string类型,编译有警告,运行会报Program received signal SIGILL, Illegal instruction。

而且,运行时报错行,会是printf的上一行,致使查找起来可能非常耗时:上一行怎么看怎么没有错误。

 

附相关材料:

 

用printf输出string类型数据总结

分类: 学习心得 2008-04-14 22:26 1618人阅读 评论(1) 收藏 举报

#include <stdio.h>

#include <string.h>
using namespace std;
int main()
{
   string a;
   a[0]='a';
   a[1]='/0';
   printf("%s/n",a);
   system("pause");
}

出错: [Warning] cannot pass objects of non-POD type `struct std::string' through `...'; call will abort at runtime

printf只能输出C语言内置的数据,而string不是内置的,只是一个扩展的类,这样肯定是链接错误的。string不等于char*,&a代表的是这个字符串的存储地址,并不是指向字符串的首地址,aa    对象中包含一个指向"string"的指针, &aar得到的是这个对象的地址,不是"string"的地址。

printf输出string类型应如此操作!

#include<iostream>
#include<string>
using namespace std;


void main()
{
string aa="qqq";
printf("%s",aa.c_str()); //不推荐

//或者cout<<a;
}

由于string是C的一个 扩展类型,其数据赋值可以用其提供的方法:assign(const char *)或直接用其构造函数      
string str( "Now is the time..." );

 

 

 

illegal instruction 信号sigill

2010-10-19 22:53

ctags :illegal instruction SIGILL linux

error  "Illegal instruction" 是由信号:“SIGILL”产生。

WIKI SIGILL  From Wikipedia


不放过任何一个警告。
“warning: cannot pass objects of non-POD type 'const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' through '...'; call will abort at runtime”

问题解决过程说明:
1. 首先确定:在同一台机器上编译执行,不存在任何的交叉编译错误。
2. 再次编译,查看解决告警信息,发现将一个 “string” 类型的变量按”%s”的格式进行输出,导致告警。
3. 告警没解决导致执行时发生“Illegal instruction”的错误。

告警解决后,问题解决。


other:

SIGILL 信号是cpu在发现非法指令之后发出一个异常,然后由负责处理该异常的内核的ISR对含有这个非法指令的进程发出的。程序收到这个信号,一般就是报 告 illegal instruction 错误信息。

可执行程序含有非法指令的原因,一般也就是cpu架构不对,编译时指定的march和实际执行的机器的march不同。这种情况,因为工具链一样,连接脚 本一样,所以可执行程序可以执行,不会发生exec format error。但是会包含一些不兼容的指令。还有另外一种可能,就是程序的执行权限不够,比如在目态下运行的程序只能执行非特权指令,一旦CPU遇到特权指 令,将产生illegal instruction错误。

       在系统运行过程中,处理机状态是动态变化的。从目态转换为管态只能通过中断来实现。从管态到目态的转换可通过修改程序状态字PSW来实现。

SIGILL

Description

Illegal instruction

Default action

Abnormal termination of the process

SA_SIGINFO macros

ILL_ILLOPC

illegal opcode

ILL_ILLOPN

illegal operand

ILL_ILLADR

illegal addressing mode

ILL_ILLTRP

illegal trap

ILL_PRVOPC

privileged opcode

ILL_PRVREG

privileged register

ILL_COPROC

coprocessor error

ILL_BADSTK

internal stack error

other reference:

http://lists.busybox.net/pipermail/buildroot/2007-February/001516.html

http://blog.chinaunix.net/u3/109812/showart_2287811.html

http://youlvconglin.blog.163.com/blog/static/523204201021944216449/

http://www.lslnet.com/linux/dosc1/04/linux-127055.htm

http://blog.csdn.net/qiaoliang328/archive/2009/11/24/4866367.aspx

 

 

原创粉丝点击