dynamic memory allocating error

This is a discussion on dynamic memory allocating error within the C Programming forums, part of the General Programming Boards category; Hi! I'm trying to make an implementation of the datatype RATIONAL in ADT way. The probleme is when I try ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    Angry dynamic memory allocating error

    Hi!
    I'm trying to make an implementation of the datatype RATIONAL in ADT way. The probleme is when I try to allocate memory for a new struct that I create in main.c.
    I send the adress of the struct to rational_create, where I try to allocate memory to it and after that set values to the integers in the struct, but the way I try to allocate doesn't work.
    the problem is how to allocate the memory in rational_create(rational *r, int n, int t).

    The code looks like this:

    main.c
    ===========
    int main(void)
    {
    Rational a;
    rational_create(Rational a, 2, 2);

    system("PAUSE");
    return 0;
    }

    rational.c
    ===========
    #include <stdio.h>
    #include <stdlib.h>
    #include "rational.h"

    struct rat_
    {
    int a1, b1;
    }

    void rational_create(Rational *r, int n, int t)
    {
    *r = (Rational)malloc(sizeof(struct rat_));

    r->a1=n;
    r->b1=t;
    }

    rational.h
    ===========
    #include <stdio.h>
    #include <stdlib.h>

    struct rat_;

    typedef struct rat_ Rational;

    /*Allocate memory*/
    void rational_create(Rational *r, int n, int t);

    int gcd(int n, int t);
    Rational add(Rational r);
    Rational sub(Rational r);

    and s on...

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    There are a few problems with the code... but to answer your main question: there is no need to allocate memory when you use the declaration
    Code:
    Rational a;
    as it is done for you implicitly. You only really need to use malloc() / calloc() when you declare a pointer to some memory whose size will only be defined at run time.

    It might also be a good idea to do the typedef like this:
    Code:
    typedef struct {
       int a1, b1;
    } Rational;
    otherwise you may have problems when you inlcude rational.h in main.c (which you will have to do I think). To prevent rational.h being compiled more than once, use:
    Code:
    #ifndef RATIONAL_H
    #define RATIONAL_H
    /*...... header declarations here ...... */
    #endif /* RATIONAL_H */
    There are still a few other things for you to find as well... !!!

    DavT

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    the purpose is that someone else can't see the struct and funktions. He can only view the header file.
    I can make changes in the functions without him knowing about it.
    That's why the declarations of the struct has to be as it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21