#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Core dumped on this program


    Why this program didn't work. I'm new in C programming.

    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <inttypes.h>
    
    //encrypt method for tea
    void encrypt(uint32_t* v, uint32_t*k)
    {
      uint32_t v0=v[0], v1=v[1], sum=0, i;           /* set up */
      uint32_t delta=0x9e3779b9;                     /* a key schedule constant */
      uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
      for (i=0; i < 32; i++) {                       /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);  
      }                                              /* end cycle */
      v[0]=v0; v[1]=v1;
    }//end encrypt
    
    
    //decrypt method for tea
    void decrypt (uint32_t* v, uint32_t* k) {
        uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i;  /* set up */
        uint32_t delta=0x9e3779b9;                     /* a key schedule constant */
        uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
        for (i=0; i<32; i++) {                         /* basic cycle start */
            v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
            v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
            sum -= delta;                                   
        }                                              /* end cycle */
        v[0]=v0; v[1]=v1;
    }//end decrypt
    
    
    
    int main(int argc, char** argv)
    {
    
      uint32_t* message;
      *message = 1011;
      uint32_t* key; 
      *key = 101;
    
      encrypt(message, key);
      printf("%" PRIu32 "\n", *message);
    
      return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Why do you think that it doesn't work? Explain that please.

    IOW, describe the symptoms to us! This isn't Psychic Programming Friends Network! We cannot read your mind!
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Code:
    $ gcc -Wall -Wextra baz.c
    baz.c: In function ‘main’:
    baz.c:35:14: warning: unused parameter ‘argc’ [-Wunused-parameter]
    baz.c:35:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
    baz.c:39:12: warning: ‘message’ is used uninitialized in this function [-Wuninitialized]
    baz.c:41:8: warning: ‘key’ is used uninitialized in this function [-Wuninitialized]
    Your pointers are pointing at garbage, which will almost certainly crash as soon as you try and dereference them.

    If you study the code you copy/pasted you'll notice this
    uint32_t v0=v[0], v1=v[1], sum=0, i; /* set up */
    uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */


    That v needs to be an array of 2 elements, and k needs to be an array of 4 elements.

    Adjust your input so you're actually providing input from two suitably sized arrays (and not naked pointers to nowhere). You have to do more than simply declare variables of the same type as the parameters, you have to think about how those variables are going to be used (this is especially important wherever pointers are involved).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo