Thread: Returning from function located in object file

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Poland
    Posts
    2

    Unhappy Returning from function located in object file

    Hi,
    I am writing program, which uses some functions located in sepereted object files. It imports headers of these functions from a header file. They are declared according to this pattern:
    Code:
    export type name(data);
    Source of the functions is located, as I said befor, in sepereted source files, which import some header files with structures if it is needed. I have already writen one of them. I have noticed one huge error. It can't be found by compiler, because it isn't gramatical error.
    Befor I describe the problem let's see the code
    1. code of the function
      Code:
      short int create(gr *start, gr *aktualna_gr, const char **argv, const int argc)
      {
      ...
      wsk = (gr *)malloc(...);
      ...
      start = wsk;
      ...
      return(0);
      }
    2. reference from the main()
      Code:
      x = create(root, current_gr, (const char **)argv, (const int)argc);
    3. additional information
      Code:
      typedef struct grupa gr;   //it is from header file with declaration of struct grupa
      gr* root = NULL;   //definition at main()


    Now my problem:
    In create() variable start at the begining is = (nil), after command
    Code:
    start = wsk;
    until end of the function start is equal variable wsk. However outside definition of start() exactly after the reference in main() root which is the pointer that should take over the value of variable start from the definition is still equal (nil).
    I have writen function similiar to crate() befor, but then I imported code of it using
    Code:
    #import "source.c"
    and it worked properly .

    I compile the program using makefile. It compiles source files to objects and then do
    gcc -o system main.o create.o ...some other objects
    I use gcc (GCC) 3.3.5 (Debian 1:3.3.5-13), GNU ld version 2.15 and GNU Make 3.80.

    What is wrong? I only expect the function to save new pointer in variable root. I'm totally confused and sad and it's all what I'd like to tell.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Maybe I'm missing this......

    But glancing over what you posted, I noticed a few things.

    1)export is a shell command. If you really want to use export in your c program, use something like getenv().

    2)To declare a function in the header file, go something like either

    extern int name(int);

    or

    int name(int);

    For the anal retentive programmer, use include guards.

    3)If include stdlib.h file, you don't need to typecast malloc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > until end of the function start is equal variable wsk. However outside definition of start() exactly after the reference in main...
    > short int create(gr *start
    You're not updating the start in main - everything in C is pass by value, including pointers. So if you want to update the value of a pointer, you need to pass a pointer to it.

    Code:
    #
    
    short int create(gr **start, gr *aktualna_gr, const char **argv, const int argc)
    {
    ...
    wsk = malloc(...);
    ...
    *start = wsk;
    ...
    return(0);
    }
    
    ...
    
    x = create( &root, current_gr, argv, argc);
    All your casts seemed entirely unnecessary.
    The cast of malloc is particularly poor - see the FAQ.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Poland
    Posts
    2
    Thanks. It's working now.
    In my post there is a little mistake. I have writen that I import header file with headers of some function which accord to pattern "export ... ...". Instead of export there should be extern

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM