0

I have a school project that I made in C (It's actually college level, but nvm). It is quite big and it works fine....in debug mode. When I build it however, it crashes. I managed to locate the problem and it looks like this(the names are in my native language so I changed them):



    typedef struct
    {
       int a, b;
       another_struct **another_struct_handle;
    } my_struct;

    void some_function(const int n, my_struct **my_struct_handle)
    {
        //some code
        *my_struct_handle=(my_struct *)malloc(n*sizeof(my_struct)); // this is where it crashes
        //some code
    }

    int main()
    {
        my_struct *my_struct_handle;
        some_function(10, &my_struct_handle);
    }

Few notes:

  1. I already do exactly the same thing with another_struct in other function and it doesn't crash. This "other function" is called before some_function, of course;
  2. This crashes ONLY when i build the project, not on debug;
  3. I compile the project with GNU GCC in code::blocks (if it have any importance);
  4. The compiler don't give me any warnings or errors anywhere in my code;
  5. I am not allowed to use global variables, that's why I need to pass a pointer to a pointer.
    Any ideas? Thanks in advance.
  • 2
    Can you please use `gdb` to locate the exact occurrence of erroneous instruction? I doubt the problem is inside `//some code` part. – Sourav Ghosh Jan 12 '15 at 09:09
  • 2
    "This crashes ONLY when i build the project, not on debug;" is an extremely vague statement. – barak manos Jan 12 '15 at 09:10
  • Did you compile with all warnings (`gcc -Wall -Wextra`) ? – Basile Starynkevitch Jan 12 '15 at 09:10
  • `malloc` may fail and on success returns an *uninitialized* memory block. You'll better clear that allocated memory zone (e.g. using `memset`) – Basile Starynkevitch Jan 12 '15 at 09:11
  • 2
    "I am not allowed to use global variables, that's why I need to pass a pointer to a pointer" - you know you can always initialize `my_struct_handle` in `main` and then pass it **by value** to `some_function`, right? – barak manos Jan 12 '15 at 09:12
  • Someone gave an answear but deleted it. If i do my_struct *my_struct_handle = (my_struct *)malloc(sizeof(my_struct)) it works. And I have no idea why. I mean, the pointer is allocated, not his value, but I don't use his value anyways, I only use his address. And As I said, I do the same thing multiple times, in this case it crashes and in the others it don't. Can someone explain please? My problem is solved and thanks for that. I commented where the error occurs (I found like a caveman using printfs where It is). – caution2toxic Jan 12 '15 at 09:15
  • [In C you should not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jan 12 '15 at 09:15
  • Yes I can initialize my_struct_handle in main, but this is not the point of the project, as stupid as it sounds. I need to make a function that initialize it. In fact this project is more or less around functions. There are many subproblems, each one with it's function, and initializing is one of them. Stupid I know... – caution2toxic Jan 12 '15 at 09:17
  • 1
    Also, do you get any warnings? Have you tried building with extra warnings enabled (e.g. `-Wall -Wextra`)? – Some programmer dude Jan 12 '15 at 09:18
  • @JoachimPileborg my problem is solved(see my first comment) and no, I don't got any warnings, and btw ty for the "not cast tip". It really makes the code nicer, but I first used malloc in C++ (bad practice) and the casting remained from there. xD But I don't use malloc anymore in C++ so, hope this tip will remain in my brain for when I use C xD – caution2toxic Jan 12 '15 at 09:21
  • I compiled your code with `gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2`, open `-Wall` and `-g` flag, I can't see any warnings even. I created an `another_struct` for test and it just deleted `another_struct **another_struct_handle;` in your `my_struct`, could show your compile result and detail content of `another_struct` – How Chen Jan 12 '15 at 09:56
  • Run the program with [`valgrind`](http://valgrind.org/docs/manual/quick-start.html) so you can see where and why it's failing. – mgarciaisaia Jan 12 '15 at 10:51
  • And does `printf("n=%d\n", n);` give you what you expect? – Morpfh Jan 12 '15 at 12:36

0 Answers0