免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4520 | 回复: 16
打印 上一主题 下一主题

hp-unix中结构体指针问题--求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-14 14:57 |只看该作者 |倒序浏览
hp-unix服务器信息 :HP-UX  B.11.23 U ia64
如下是我写的例子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
  char s[10];
  long l;
}a;
typedef struct{
  char s[10];
  long l;
}t;
int main()
{
  char *str="Fgfgf";
  t *z;
  a *c;
  c=(a*)z;
  memset(c,'\0',sizeof(*c));
  memcpy(c->s, str, 4);
  printf("%s\n",c->s);
  return 0;
}
此程序在windows-VC,LINUX-GCC下运行失败,在unix下可以正常执行,没有给结构体指针动态分配内存空间怎莫会正常执行那。
问题处在此处 memcpy(c->s, str, 4);我用的编译命令是aCC +DD64 xxxx.c,生成了a.out,执行它有输出
请高手指教。

论坛徽章:
0
2 [报告]
发表于 2010-05-14 15:02 |只看该作者
碰巧而已,  正好c->s 指向有效空间.

论坛徽章:
0
3 [报告]
发表于 2010-05-14 15:26 |只看该作者
运行了2年的代码了,而且有别的程序也这么写的,难道都碰巧了?

论坛徽章:
0
4 [报告]
发表于 2010-05-14 15:35 |只看该作者
回复 3# kylinhaha

   程序肯定是错的.

    你把三个平台的c->s的值输出来就可以看到了, 从理论上讲, Linux 下和Windows 也可能运行不会出错.

论坛徽章:
0
5 [报告]
发表于 2010-05-14 18:01 |只看该作者
gcc下出现段错误,vc下程序崩溃,hp-unix下无问题,结果正确。
而且我的这个是简化的例子,原程序在服务器正常运行了2年多了,不知道有没有hp-unix环境的高手试验一下,看看是我的环境问题,还是unix本来就这样!
求大拿帮助!

论坛徽章:
0
6 [报告]
发表于 2010-05-15 06:41 |只看该作者
如果是这样,我只能说HP/UNIX太人性化了!

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
7 [报告]
发表于 2010-05-15 18:56 |只看该作者
hp-unix服务器信息 :HP-UX  B.11.23 U ia64
如下是我写的例子:
#include
#include
#include
type ...
kylinhaha 发表于 2010-05-14 14:57



    hp64   是64位机,指针都是64位的。
memset(c,'\0',sizeof(*c));//在HP清24字节,WIN清16字节
  memcpy(c->s, str, 4);

论坛徽章:
0
8 [报告]
发表于 2010-05-15 22:38 |只看该作者
楼上说的没有错,但是指针没有分配内存,却给赋值了,问题是这里。

论坛徽章:
0
9 [报告]
发表于 2010-05-17 02:41 |只看该作者
总之你的程序有问题是肯定的,你想知道为什么也没错。
你的问题光看C代码是不行的,你得看编译器生成的汇编指令。gcc 用-S可以编译时输出.s汇编文件,VC用/Fa。那个aCC你查一下手册看看用什么开关吧。我怀疑是栈被清空过,或者0x0000是在ia64上是有效地址。

论坛徽章:
0
10 [报告]
发表于 2010-05-17 13:20 |只看该作者
.file        "t.c"
        .psr        msb
        .radix        C
        .pred.safe_across_calls p1-p5,p16-p63
        .allow 0, "ao", "x1"
        .type        printf,@function
        .global        printf
        .type        memcpy,@function
        .global        memcpy
        .type        memset,@function
        .global        memset
        .type        main,@function
        .global        main
        .type        _memset,@function
        .global        _memset
        .type        _memcpy,@function
        .global        _memcpy
        .size        main, 272
// Routine [id=0006] ( main )

// ===
        .secalias .abe$3.text, ".text"
        .section .abe$3.text = "ax", "progbits"
        .align        16
        .proc        main
..L0:
//      $start          CMid900 =               ;; // A

..L2:
main::
.prologue
//      $entry          CMid904 =                  // A [t.c: 13/1]
//file/line/col 13/1
.save ar.pfs, r36
        alloc           r36 = ar.pfs, 0, 6, 3, 0   // M [t.c: 13/1] [UVU: ]
.save rp, r37
        mov             r37 = rp                   // I
        add             r35 = 0, gp                // I
.body

//file/line/col 14/9
        add             r9 = @ltoffx(.abe$2.rodata), r0 ;; // M [t.c: 14/9]
        add             r9 = r9, gp                // M [t.c: 14/9]
        nop.i           0                       ;; // I
        ldxmov          r32 = [r9], .abe$2.rodata# // M [t.c: 14/9] [UVU: str]
//file/line/col 17/3
        add             r34 = 0, r33               // M [t.c: 17/3] [UVU: c] [UVuse]
//file/line/col 18/3
        add             out2 = 24, r0              // I [t.c: 18/3]
        add             out1 = 0, r0            ;; // M [t.c: 18/3]
        lfetch.nt1      [r34]                      // M [t.c: 18/3] [UVuse]
        add             out0 = 0, r34              // I [t.c: 18/3] [UVuse]
        nop.m           0                          // M
        nop.m           0                          // M
        br.call.dptk.few rp = _memset#          ;; // B [t.c: 18/3] [UVU]
        add             gp = 0, r35                // M [t.c: 18/3]
//file/line/col 19/3
        lfetch          [r32]                      // M [t.c: 19/3] [UVuse]
        add             out2 = 4, r0               // I [t.c: 19/3]
        lfetch          [r34]                      // M [t.c: 19/3] [UVuse]
        add             out1 = 0, r32              // M [t.c: 19/3] [UVuse]
        add             out0 = 0, r34           ;; // I [t.c: 19/3] [UVuse]
        nop.m           0                          // M
        nop.m           0                          // M
        br.call.dptk.few rp = _memcpy#          ;; // B [t.c: 19/3] [UVU]
        add             gp = 0, r35                // M [t.c: 19/3]
//file/line/col 20/3
        add             r9 = @ltoffx(.abe$2.rodata), r0 // M [t.c: 20/3]
        add             r10 = 0, r34               // I [t.c: 20/3] [UVuse]
        add             r11 = @pltoff(printf), r0 ;; // M [t.c: 20/3]
        add             r9 = r9, gp                // M [t.c: 20/3]
        add             r11 = r11, gp              // I [t.c: 20/3]
        add             r14 = r0, gp               // M [t.c: 20/3]
        add             out1 = 0, r10           ;; // I [t.c: 20/3]
        nop.i           0                          // I
        ldxmov          r9 = [r9], .abe$2.rodata#  // M [t.c: 20/3]
        ld8.acq         r10 = [r11]                // M [t.c: 20/3]
        add             r11 = 8, r11            ;; // I [t.c: 20/3]
        ld8             gp = [r11]                 // M [t.c: 20/3]
        mov             b6 = r10                   // I [t.c: 20/3]
        add             r9 = 8, r9              ;; // I [t.c: 20/3]
        add             r9 = 0, r9              ;; // M [t.c: 20/3]
        add             out0 = 0, r9               // M [t.c: 20/3]
        nop.i           0                          // I
        nop.m           0                          // M
        nop.m           0                          // M
        br.call.dptk.few rp = b6                ;; // B [t.c: 20/3] [UVU]
        add             gp = 0, r35                // M [t.c: 20/3]
        mov             rp = r37                   // I
//file/line/col 21/3
        add             r8 = 0, r0              ;; // I [t.c: 21/3]
        nop.m           0                          // M
        mov             ar.pfs = r36               // I
.restore sp
        br.ret.dptk.few rp                      ;; // B [t.c: 21/3]

..L1:
//      $end                                    ;; // A

        .endp        main

// ===


// ===
        .secalias .abe$4.IA_64.unwind, ".IA_64.unwind"
        .section .abe$4.IA_64.unwind = "a", "unwind"
        .align 8
        data8.ua @segrel(.abe$3.text)
        data8.ua @segrel(.abe$3.text+272)
        data8.ua @segrel(.abe$unwind_info_block00000)

// ===
        .secalias .abe$5.IA_64.unwind_info, ".IA_64.unwind_info"
        .section .abe$5.IA_64.unwind_info = "a", "progbits"
        .align 8
// unwind_info_block: notype local temp
        data8.ua @segrel(.llo$annot_info_block0000)
.abe$unwind_info_block00000:        data1        0x00, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x02
        data1        0x03, 0xe4, 0x01, 0xb0, 0xa5, 0xe6, 0x00, 0xb1
        data1        0x24, 0x61, 0x30, 0xc0, 0x00, 0x00, 0x00, 0x00

// ===
        .secalias .abe$2.rodata, ".rodata"
        .section .abe$2.rodata = "a", "progbits"
        .align 8
// .rodata: section local
// __undef: notype local temp
// __undef: notype local temp

.abe$__undef00001:        stringz        "Fgfgf"
        data1        0x00, 0x00
.abe$__undef00002:        stringz        "%s\n"

// ===
        .secalias .abe$6.HP.opt_annot, ".HP.opt_annot"
        .section .abe$6.HP.opt_annot = "a", "annot"
        .align 8
// .llo$annot_info_block0000: object local temp
.llo$annot_info_block0000:        data1        0x00, 0xac, 0x01, 0xbe, 0xff, 0xfe, 0xff, 0xff
        data1        0xff, 0xfc, 0xff, 0xff, 0xb0, 0xfc, 0x01, 0x02
        data1        0x84, 0x00, 0x02, 0x03, 0x88, 0x02, 0x00, 0x1f
        data1        0x00

// ===
        .secalias .abe$7.debug_procs_info, ".debug_procs_info"
        .section .abe$7.debug_procs_info = "", "progbits"
        .align 1
        data1        0x00, 0x00, 0x00, 0x8d, 0x00, 0x02
        data4.ua @secrel(.abe$8.debug_procs_abbrev)
        data1        0x08, 0x03
        data8.ua @secrel(.abe$0.debug_line)
        data8.ua @secrel(.abe$1.debug_actual)
        stringz        "t.c"
        data1        0x04, 0x01, 0x47, 0x00
        stringz        "/home/flx05/fleksbat/exec/test/GEX/busi/GEXBAP01002/lm"
        data1        0x00, 0x00, 0x00, 0x00
        stringz        "\tt.c"
        data1        0x00, 0x00, 0x00, 0x69, 0x00, 0x07, 0x01, 0x00
        data1        0x00, 0x00, 0x00
        data8.ua .abe$3.text
        data8.ua .abe$3.text+272
        data1        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        data1        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        data1        0x00, 0x00

// ===
        .secalias .abe$8.debug_procs_abbrev, ".debug_procs_abbrev"
        .section .abe$8.debug_procs_abbrev = "", "progbits"
        .align 1
// .debug_procs_abbrev: section local
        data1        0x03, 0x11, 0x01, 0x10, 0x07, 0x90, 0x40, 0x07
        data1        0x03, 0x08, 0x13, 0x0b, 0x95, 0x40, 0x0b, 0x25
        data1        0x08, 0x1b, 0x08, 0x01, 0x13, 0x00, 0x00, 0x07
        data1        0x2e, 0x01, 0x94, 0x40, 0x0b, 0x96, 0x40, 0x06
        data1        0x11, 0x01, 0x12, 0x01, 0x97, 0x40, 0x01, 0x98
        data1        0x40, 0x01, 0x00, 0x00, 0x09, 0x1e, 0x01, 0x03
        data1        0x08, 0x01, 0x13, 0x00, 0x00, 0x00

// ===
        .secalias .abe$0.debug_line, ".debug_line"
        .section .abe$0.debug_line = "", "progbits"
        .align 8
// .debug_line: section local
        data1        0x00, 0x00, 0x00, 0x41, 0x00, 0x02, 0x00, 0x00
        data1        0x00, 0x10, 0x04, 0x01, 0x00, 0x05, 0x0a, 0x00
        data1        0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01
        data1        0x00, 0x00, 0x00, 0x08, 0x03
        stringz        "t.c"
        data1        0x00, 0x00, 0x00, 0x00, 0x09, 0x02
        data8.ua .abe$3.text
        data1        0x04, 0x01, 0x05, 0x01, 0x03, 0x0c, 0x01, 0x05
        data1        0x09, 0x1f, 0x05, 0x03, 0x26, 0x10, 0x42, 0x47
        data1        0x9c, 0x02, 0x04, 0x00, 0x01, 0x01

// ===
        .secalias .abe$1.debug_actual, ".debug_actual"
        .section .abe$1.debug_actual = "", "progbits"
        .align 8
// .debug_actual: section local
        data1        0x00, 0x00, 0x00, 0x45, 0x00, 0x02, 0x00, 0x00
        data1        0x00, 0x0e, 0x04, 0x01, 0x00, 0x05, 0x0a, 0x00
        data1        0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01
        data1        0x00, 0x09, 0x02
        data8.ua .abe$3.text
        data1        0x00, 0x01, 0x11, 0x0b, 0x1f, 0x00, 0x01, 0x11
        data1        0x1e, 0x00, 0x01, 0x11, 0x10, 0x10, 0x00, 0x01
        data1        0x11, 0x32, 0x1a, 0x00, 0x01, 0x11, 0x37, 0x1a
        data1        0x00, 0x01, 0x11, 0x87, 0x1f, 0x00, 0x01, 0x18
        data1        0x1e, 0x02, 0x04, 0x00, 0x01, 0x01

// ===
        .secalias .abe$9.note, ".note"
        .section .abe$9.note = "", "note"
        .align 8
        data1        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02
        data1        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xbd
        data1        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
        stringz        "HP"
        data1        0x00, 0x00, 0x00, 0x00, 0x00
        stringz        "A.06.05 [Jul 25 2005] [Build N/A]\necom options = -assembly on -ia64abi all -architecture 64 -inst compiletime -sysdir /usr/include -test namespaces -koenig_lookup on -inline_power 1 -link_type dynamic -fpeval float -tls_dyn on -target_os 11.23 --sys_include /opt/aCC/include_std --sys_include /opt/aCC/include_std/iostream_compat --sys_include /usr/include --sys_include /usr -ucode hdriver=optlevel%1% -plusolistoption -Ol06all! -plusolistoption -Ol13moderate! -plusooption -Oq01,al,ag,cn,sz,ic,vo,Mf,Po,es,rs,Rf,Pr,sp,in,cl,om,vc,pi,fa,pe,rr,pa,pv,nf,cp,lx,Pg,ug,lu,lb,uj,dn,sg,pt,kt,em,np,ar,rp,dl,fs,bp,wp,pc,mp,lr,cx,cr,pi,so,Rc,fa,ft,fe,ap,st,lc,Bl,sr,ib,pl,sd,ll,rl,dl,Lt,ol,fl,lm,ts,rd,dp,If!"
        data1        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        data1        0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00
        data1        0x00, 0x00, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00
        data1        0x00, 0x00, 0x04
        stringz        "HP"
        data1        0x00, 0x00, 0x00, 0x00, 0x00
        stringz        "t.c\n/test/lm\nANSI C++ 64 bits"
        data1        0x00, 0x00, 0x00, 0x00
我对汇编一窍不通,此代码为上述程序的汇编代码,望高手指点
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP