-1

I'm learning c not really fully understand everything, i have this example code i gat from a book. The Topic is the usage of calloc() and realloc().I'm using Ubuntu 14.04 and the gcc compiler. I checked on the syntax in the code and is just the same as in the book didn't make any mistakes. So what i think the problem is, is how to write the code so that the compiler understands it the way it needs it. For that i do not have the experience to figure that out. I have more error outputs if needed.

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

void main(){
    double * memptr;
    memptr = (double *) calloc(100, sizeof(double));
    if (memptr == NULL){
        printf("\nNicht genuegend Speicherplatz!");
        exit(1);
    }
    printf("\nSpeicher fuer 100 double-Variable Ok!");
    memptr = (double*) realloc(memptr,125);
    if(memptr ==NULL){
        printf("\nNicht genuegend Speicherplatz!");
        exit(1);
    }
    printf("\nSpeicherplatz auf 125 Variable vergroessert!");
    free(memptr);
    printf("\nSpeicher wieder freigegeben!");
}

Error messages:

*bei84.c: In function ‘main’:
bei84.c:7:22: warning: incompatible implicit declaration of built-in function ‘calloc’
[enabled by default]

memptr = (double *) calloc(100, sizeof(double));
                  ^

bei84.c:13:21: warning: incompatible implicit declaration of built-in function ‘realloc’
  [enabled by default]
  memptr = (double*) realloc(memptr,*125);
codias
  • 1
  • 2
  • 1
    That's odd... `calloc()` and `realloc()` are defined in `stdlib.h` which you have included. – Bill Lynch Dec 08 '14 at 15:45
  • Could you show us how you are compiling the source code as well? – Bill Lynch Dec 08 '14 at 15:47
  • This is not the answer but you have logical error: `realloc(memptr,125);` must be `realloc(memptr,125 * sizeof (double));` – i486 Dec 08 '14 at 15:48
  • 1
    I would guess the problem comes from the actual code not including stdlib at all, and the code shown here isn't a 100% copy paste. Otherwise, wouldn't gcc whine about line 6 rather than 7? – Lundin Dec 08 '14 at 15:50
  • 1
    Just an FYI an earlier answer explains why you generally get this warning.. http://stackoverflow.com/questions/977233/warning-incompatible-implicit-declaration-of-built-in-function-xyz.. however in your case you seem to have included the correct header file. – tsar2512 Dec 08 '14 at 15:51
  • 1
    btw. i tried compiling and executing your code.. just a copy paste, and it worked.. so nothing wrong with the code itself. I think it could be more with linking or the way your platform is setup – tsar2512 Dec 08 '14 at 15:53
  • 1
    gcc bei84.c -o bei84 @ Bill – codias Dec 08 '14 at 15:53
  • @codias: Could you upload to gist.github.com (or anywhere really) the output of `gcc -E bei84.c`? – Bill Lynch Dec 08 '14 at 15:54
  • 1
    I can compile and run this code precisely as shown here without any warning or error. Thus I believe it to be an fault in the OPs system and not a code problem at all, therefore off topic. – Vality Dec 08 '14 at 15:54
  • When you program in C (not C++) you should never cast the return value of `malloc`, `realloc`, or `calloc`. – harper Dec 08 '14 at 15:56
  • my problem is not the code but how to rewrite so that the compiler gets it – codias Dec 08 '14 at 15:56
  • 2
    @codias: The problem is that we can't find any issues with your code. It should compile and run successfully. – Bill Lynch Dec 08 '14 at 15:57
  • 2
    Check that you include the intended header files. Do you have any `stdlib.h` other than the compiler's in your search path? You get the answer with `gcc -E myprogram.c`. BTW: The output *must* show the prototype if you have included the intended file. Else `#include `. – harper Dec 08 '14 at 16:00
  • 1
    I am able to compile and run the code in Ubuntu 14.04. how are you compiling it ? – vpillai Dec 08 '14 at 16:15
  • Somehow your compilation is not finding the prototypes, even though this post has correctly used `#include`. Suspect you prototype exists in some other header file. Check your compiler documentation about its location. – chux - Reinstate Monica Dec 08 '14 at 16:15
  • let me try the things you have said we be back – codias Dec 08 '14 at 16:16
  • @Bill https://github.com/codias/calloc-and-realloc-compiling-issue/issues/1 – codias Dec 08 '14 at 16:40
  • @codias: Yeah. That's not super helpful because it's being parsed by markdown. If you use [gist.github.com](http://gist.github.com) instead, it won't do a markdown pass over it. – Bill Lynch Dec 08 '14 at 16:42
  • sry @Bill https://gist.github.com/codias/705aa59cb42f4ac8c9e9 – codias Dec 08 '14 at 16:50
  • That's not the entire output. There is more at the beginning. – Bill Lynch Dec 08 '14 at 16:53
  • To be more explicit, assuming you are using gcc 4.8.2 (I know you're using gcc 4.8.x), you've skipped 1402 lines. – Bill Lynch Dec 08 '14 at 17:02
  • `void main()` is non-standard. You should not cast the result of `malloc`. If you want 125 doubles, `realloc(memptr,125)` is wrong. You can check where your includes come frome by adding `-H` to the gcc command line. – n. m. could be an AI Dec 08 '14 at 17:17
  • 1
    Your program [compiles and runs](http://ideone.com/HQoS3p) despite the small problems I pointed out in another comment. It is safe to assunlme that either your gcc installation is broken, or you are not compiling the program you think you are compiling. – n. m. could be an AI Dec 08 '14 at 18:39
  • hi ppl thanks for the advices, it still ain't working. I have included the malloc.h header files and removed all the casts infront of the function and compiled it with codeblocks still getting errors coming from my . https://gist.github.com/codias/77bf488056c6098db074 – codias Dec 11 '14 at 12:04
  • codeblock is on a wiindows 7 machine – codias Dec 11 '14 at 12:22
  • I have reinstalled Ubuntu no changes still not working. gist.github.com/codias/10b7d526ce3340cfdfa3 this is what i get after gcc -E bei84.c – codias Dec 11 '14 at 13:29

1 Answers1

0

In the beginning, say #include <malloc.h>, that may clear up some issues. Also, you can put spaces between lines of C code; the compiler doesn't care, and it makes it SO MUCH EASIER TO READ!

And when you use realloc, you do not need the (double *), memptr is already a double * and it is understood how the return is cast into a double *.

Gophyr
  • 406
  • 2
  • 11