VC与GCC代码编译差别(一)

Manacher算法,实现最长回文字符串检测的算法。算法实现在Gcc编译环境下运行出现错误,但是在VC环境下运行正常。

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

void PrintString(char* s);
char* longestPalindrome(char* s) {
    int index_s=0,index_f=1;
    int id=1,mx=0;
    int str_len = strlen(s);
    int* pos_table = (int*)malloc(str_len*2+3);
    char* full_str = (char*)malloc(str_len*2+3);/* abc --> $#a#b#c#'0' */

    printf("strlen = %d\n",str_len);
    *(full_str) = '$';
    while(*(s+index_s) != '\0'){
        *(full_str+index_f) = '#';
        index_f++;
        *(full_str+index_f) = *(s+index_s);
        index_f++;
        index_s++;
    }
    *(full_str+index_f) = '#';
    *(full_str+index_f+1) = '\0';
    printf("full string is ");PrintString(full_str);
    str_len = strlen(full_str);
    printf("sizeof new string is %d\n",str_len);
    *(pos_table) = 1;
    for (index_f = 1; index_f < str_len; index_f++){
        printf("index_f=%d,mx=%d,id=%d\n",index_f,mx,id);
        if (index_f < mx){
            if (*(pos_table+2*id-index_f) < (mx-index_f)){
                *(pos_table+index_f) = *(pos_table+2*id-index_f);
                continue;
            } else {
                *(pos_table+index_f) = *(pos_table+2*id-index_f);
                for (index_s = mx+1; index_s < str_len; index_s++){
                    printf("index_s=%d",index_s);
                    if (*(full_str+index_f+index_s) == (*(full_str+index_f-index_s))){
                        *(pos_table+index_f) += 1;
                    }else{
                        break;
                    }
                }
                id = index_f;
                mx = index_f+index_s-1;
                continue;
            }
        } else {
            *(pos_table+index_f) = 1;
            for (index_s = 1; index_s < str_len; index_s++){
          // 以下四句打印输出均不正常,类似于新开辟的字符串缓冲区被破坏了一样。 printf(
"\tindex_f=%d,index_s=%d,full_str=%d \n",index_f,index_s,(int)full_str); printf("\tfull_str[i]=%c, \t",*(full_str+index_f+index_s)); printf("\tfull_str[-i]=%c \n",*(full_str+index_f-index_s));

 

printf("full string is ");PrintString(full_str);

                if (*(full_str+index_f+index_s) == (*(full_str+index_f-index_s))){
                    *(pos_table+index_f) += 1;
                }else{
                    break;
                }
            }
            id = index_f;
            mx = index_f+index_s-1;
        }
        continue;
    }
    for (index_f = 0; index_f < str_len; index_f++){
        printf("%d",*(pos_table+index_f));
    }
    printf("\n");
    return full_str;
}

void PrintString(char* s)
{
    int index=0;
    while(*(s+index) != '\0'){
        printf("%c",*(s+index));
        index++;
    }
    printf("\n");
}

int main(void)
{
    char* s="aba";
    char* result;

    result = longestPalindrome(s);
    printf("This is question No.5 \n");
}

出错的情形如下图所示:

 

 

 Gcc环境下出错的情形 VC环境下正常的情形

转载于:https://www.cnblogs.com/MrLittleWhite/p/6838828.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值