IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

problème de pointeur


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Inscrit en
    Octobre 2008
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Octobre 2008
    Messages : 3
    Par défaut problème de pointeur
    voila mon programme, le probleme vient du pointeur Mess. Quand je lui demande d'afficher les valeurs du tableau Mess[] il maffiche nimporte koi. Ca fait longtemps que je n'ai plus programmé (surtt avec des pointeurs), ca doit etre une bete erreur donc si vous pouvez m'aider ce serait vrmt cool.
    Les pointeurs Mess se trouvent dans les fonctions task1, task2 et task3.
    Mon prog devrait m'afficher H-1 et L-1.

    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
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    #include <stdio.h>
    #include "includes.h"
    #include <string.h>
     
    #define DEBUG 1
     
    /* Definition of Task Stacks */
    /* Stack grows from HIGH to LOW memory */
    #define   TASK_STACKSIZE       2048
    OS_STK    task1_stk[TASK_STACKSIZE];
    OS_STK    task2_stk[TASK_STACKSIZE];
    OS_STK    task3_stk[TASK_STACKSIZE];
     
    #define QUEUELENGTH     10
    OS_EVENT *CommQ;
    OS_MEM *CommMem;
    void *CommMsg[QUEUELENGTH];
    INT32U CommBuf[16][32];
    //struct Message *Mess;
     
    /* Definition of Task Priorities */
    #define TASK1_PRIORITY      6 // highest priority
    #define TASK2_PRIORITY      7
    #define TASK3_PRIORITY      12
     
     
    struct Message{
        char prio;
        char id;
    };
     
    int changeState(int state)
    {
        if (state==0) 
        {
            return 1;
        }
        else
        {
            return 0;    
        }
    }
     
    void print(int state, int task)
    {
        printf("Task: %d State: %d\n", task, state);
    }
    /* Prints a message and sleeps for given time interval */
    void task1(void* pdata)
    {
        struct Message *Mess;
        INT8U *pmsg;
        INT8U err;
        while (1)
        {
            pmsg = OSMemGet(CommMem, &err);
            if (pmsg != (INT8U *)0) { /* Memory block allocated, use it. */
                Mess[0].prio='H';
                Mess[0].id='1';
                /* add pointer to the new message to the queue */
                OSQPost (CommQ, (void *) Mess);
                OSTimeDlyHMSM(0, 0, 0, 10);
            }
        }
    }
     
    /* Prints a message and sleeps for given time interval */
    void task2(void* pdata)
    {
         struct Message *Mess;
         INT8U *pmsg;
         INT8U err;
         struct Message test;
         while(1)
         {
            pmsg = OSMemGet(CommMem, &err);
            if (pmsg != (INT8U *)0) { /* Memory block allocated, use it. */
                Mess[1].prio='L';
                Mess[1].id='1';
                /* add pointer to the new message to the queue */
                OSQPost (CommQ, (void *) Mess);
                OSTimeDlyHMSM(0, 0, 0, 10);
            }
         }
    }
     
    void task3(void* pdata)
    {
        struct Message *Mess;
        INT8U err;
         while(1)
         {
            Mess = (struct Message *) OSQPend(CommQ, 0, &err);
            int i;
            for(i=0; i<=1; i++)
            {
                printf("%c - %c\n",Mess[i].prio, Mess[i].id);
            }
         }
    }
     
     
     
    /* The main function creates three task and starts multi-tasking */
    int main(void)
    {
      printf("Lab 3 - Step1\n");
      INT8U err; 
      OSTaskCreateExt
        (task1,                        // Pointer to task code
         NULL,                         // Pointer to argument that is
                                       // passed to task
         &task1_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
         TASK1_PRIORITY,               // Desired Task priority
         TASK1_PRIORITY,               // Task ID
         &task1_stk[0],                // Pointer to bottom of task stack
         TASK_STACKSIZE,               // Stacksize
         NULL,                         // Pointer to user supplied memory
                                       // (not needed here)
         OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
         OS_TASK_OPT_STK_CLR           // Stack Cleared                                 
        );
     
      OSTaskCreateExt
        (task2,                        // Pointer to task code
         NULL,                         // Pointer to argument that is
                                       // passed to task
         &task2_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
         TASK2_PRIORITY,               // Desired Task priority
         TASK2_PRIORITY,               // Task ID
         &task2_stk[0],                // Pointer to bottom of task stack
         TASK_STACKSIZE,               // Stacksize
         NULL,                         // Pointer to user supplied memory
                                       // (not needed here)
         OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
         OS_TASK_OPT_STK_CLR           // Stack Cleared                       
        );  
     
     
      OSTaskCreateExt
        (task3,                        // Pointer to task code
         NULL,                         // Pointer to argument that is
                                       // passed to task
         &task3_stk[TASK_STACKSIZE-1], // Pointer to top of task stack
         TASK3_PRIORITY,               // Desired Task priority
         TASK3_PRIORITY,               // Task ID
         &task3_stk[0],                // Pointer to bottom of task stack
         TASK_STACKSIZE,               // Stacksize
         NULL,                         // Pointer to user supplied memory
                                       // (not needed here)
         OS_TASK_OPT_STK_CHK |         // Stack Checking enabled 
         OS_TASK_OPT_STK_CLR           // Stack Cleared                                 
        );  
     
      CommQ=OSQCreate(&CommMsg[0],QUEUELENGTH); 
      CommMem = OSMemCreate(&CommBuf[0][0], 16, 32 * sizeof(INT32U), &err);
     
      OSStart();
     
      return 0;
    }

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Novembre 2006
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 52
    Par défaut
    Ne manquerait t'il pas un appel à la fonction OSInit() comme indiqué dans la doc ?.

    http://www.rabbit.com/documentation/.../ModUcos45.htm

  3. #3
    Candidat au Club
    Inscrit en
    Octobre 2008
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Octobre 2008
    Messages : 3
    Par défaut
    Non, je ne pense pas que ca soit une erreur car le multitasking marche sans problème dans le programme. Tout marche correctement je pense à part le pointeur Mess qui ne fait pas ce que je voudrais.

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 104
    Par défaut
    Y'a un truc qui ne va pas dans ton code. Dans les fonctions task1 & 2, tu déclares un pointeur sur une structure, mais tu n'alloues pas de mémoire :
    Et ce code ne peut donc pas fonctionner correctement :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Mess[1].prio='L';
    Mess[1].id='1';
    ... puisque le pointeur Mess a une valeur indéfinie (par conséquent Mess[x] accède également n'importe où en mémoire).

  5. #5
    Candidat au Club
    Inscrit en
    Octobre 2008
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Octobre 2008
    Messages : 3
    Par défaut
    effectivement tu as raison j'ai modifié ca et ca marche correctement maintenant. Mais je l'ai pas fait vraiment proprement j'ai déclaré un vecteur de grande taille sur lequel le pointeur pointe. Y a moyen d'allouer dynamiquement de la mémoire mais jsais plus commt faire exactement.

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    1 104
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 1 104
    Par défaut
    Pour l'allocation dynamique, il faut utiliser la fonction malloc.

Discussions similaires

  1. Problème de pointeurs..embrouillé
    Par Frenchy dans le forum C++
    Réponses: 11
    Dernier message: 10/03/2005, 16h33
  2. Problème de pointeur avec un TQuery
    Par Oluha dans le forum Bases de données
    Réponses: 3
    Dernier message: 25/01/2005, 13h57
  3. Problème de pointeur
    Par toma_lille dans le forum C++
    Réponses: 1
    Dernier message: 07/12/2004, 21h26
  4. [MFC] Problème de pointeur !!
    Par acastor dans le forum MFC
    Réponses: 7
    Dernier message: 19/03/2004, 15h50
  5. TBitmap et problèmes de pointeurs...
    Par benj63 dans le forum C++Builder
    Réponses: 8
    Dernier message: 28/07/2003, 13h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo