Bjr, j'ai une methode en C qui attribue une adresse memoire à une structure de donnees qu'elle a reçu en parametre. et a la sortie de cette methode, je dois nourrir les champs de cette stucture. Je recois une erreur:

Program received signal SIGSEGV, Segmentation fault.
0x000000000041519d in SC_ShmLoadBankInfo () at SCPubFunc.pc:102
SC_ShmGetStru(g_stConf.iShmID, MODE_BANK_ARRAY, i, (void *)&pstBank);
102 pstBank->iBankID = iBankID;
voici la methode qui attribue l'adresse memoire:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
int SC_ShmGetStru(int iShmID, int iMode, int iID, void **pAddr)
{
    int i, iOffset, iStruSize;
    ST_SC_BankInfo *pstBank = NULL;
    ST_SC_SvcInfo *pstSvc = NULL;
    void *pBase;
 
    /* get shm base addr (attach) */
    if ((int)(*pBase = (void*)shmat(iShmID, (void*)0, 0)) == -1)
    {
        sprintf(g_caMsg, "[Fail] ShmGetStru, attach shm");
        ErrLog(TRACE_LEVEL5, g_caMsg, RPT_TO_LOG, 0, 0);
        return -1;
    }
 
    switch (iMode)
    {
    case MODE_CONFIG:
        *pAddr = (ST_SC_Config *) pBase;
        return 0;
    case MODE_BANK_ARRAY:
        iOffset = sizeof(ST_SC_Config);
        iStruSize = sizeof(ST_SC_BankInfo);
        if (iID > BANK_NUM)
        {
            sprintf(g_caMsg, "[Fail] ShmGetStru, illegal bank array %d", iID);
            ErrLog(TRACE_LEVEL5, g_caMsg, RPT_TO_LOG, 0, 0);
            shmdt(pBase);
            return -1;
        }
        *pAddr = (ST_SC_BankInfo *) (pBase + iOffset + sizeof(ST_SC_BankInfo) * iID);
        return 0;
    case MODE_BANK_VALUE:
        iOffset = sizeof(ST_SC_Config);
        iStruSize = sizeof(ST_SC_BankInfo);
        for (i=0; i<BANK_NUM; i++)
        {
            pstBank = (ST_SC_BankInfo *) (pBase + iOffset + iStruSize * i);
            if (pstBank->iBankID == iID)
            {
                *pAddr = pstBank;
                return 0;
            }
        }
        sprintf(g_caMsg, "[Fail] ShmGetStru, bank value not found:%d", iID);
        ErrLog(TRACE_LEVEL5, g_caMsg, RPT_TO_LOG, 0, 0);
        shmdt(pBase);
        return -1;
    case MODE_SVC_ARRAY:
        if (iID > g_stConf.iSvcNum)
        {
            sprintf(g_caMsg, "[Fail] ShmGetStru, illegal svc array %d", iID);
            ErrLog(TRACE_LEVEL5, g_caMsg, RPT_TO_LOG, 0, 0);
            shmdt(pBase);
            return -1;
        }
        iOffset = sizeof(ST_SC_Config) + sizeof(ST_SC_BankInfo) * BANK_NUM;
        iStruSize = sizeof(ST_SC_SvcInfo);
        pstSvc = (ST_SC_SvcInfo *) (pBase + iOffset + iStruSize * iID);
        *pAddr = pstSvc;
        break;
    case MODE_SVC_VALUE:
        iOffset = sizeof(ST_SC_Config) + sizeof(ST_SC_BankInfo) * BANK_NUM;
        iStruSize = sizeof(ST_SC_SvcInfo);
        for (i=0; i<g_stConf.iSvcNum; i++)
        {
            pstSvc = (ST_SC_SvcInfo *) (pBase + iOffset + iStruSize * i);
            if (pstSvc->iSvcID == iID)
            {
                *pAddr = pstSvc;
                return 0;
            }
        }
        sprintf(g_caMsg, "[Fail] ShmGetStru, svc value not found:%d", iID);
        ErrLog(TRACE_LEVEL5, g_caMsg, RPT_TO_LOG, 0, 0);
        shmdt(pBase);
        return -1;
    default:
        sprintf(g_caMsg, "[Fail] ShmGetStru, invalide Mode:%d", iMode);
        ErrLog(TRACE_LEVEL5, g_caMsg, RPT_TO_LOG, 0, 0);
        shmdt(pBase);
        return -1;
    }
 
    return 0;
}
La partie du code où le programme plante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
SC_ShmGetStru(g_stConf.iShmID, MODE_BANK_ARRAY, i, (void *)&pstBank);
pstBank->iBankID = iBankID;
strcpy(pstBank->caDBUsr, caDBUsr);
strcpy(pstBank->caBankName, caBankName);
strcpy(pstBank->caTax, caTax);
strcpy(pstBank->caBin, caBin);
SC_ShmDettach(pstBank);
Merci pour votre aide.