malloc geschichte
-
guten morgen,
und hier eine anfänger frage:
warum tut folgendes nicht:
main.c:#include"malloc.h" void check_malloc(size_t bytes); double test; int main() { test = (double *)check_malloc(5*sizeof( double )); }
malloc.h:
#include <stdio.h> #include <math.h> #include <float.h> #include <string.h> void check_malloc(size_t bytes); void check_malloc(size_t bytes) { void *tmp; tmp= (void*) malloc(bytes); if(tmp==NULL) { fprintf(stderr, "ERROR : Out of memory ?!\n" " Trying to (m)allocate %i Bytes failed !\n" " File: %s Line: %d\n", (int) bytes); exit(1); } return tmp; }
Fehlermeldung:
In file included from malloc.c:5:
../malloc.h: In functioncheck_malloc': ../malloc.h:19: warning:
return' with a value, in function returning void
malloc.c: In function `main':
malloc.c:11: error: void value not ignored as it ought to beGrüße,
Nakaa
-
Du willst einen "void*****" (Zeiger auf unstrukturierte Daten) zurückgeben, keinen "void" (kein Rückgabetyp).
-
hm, das void eigentlich keinen Rückgabewert hat weiß ich. komme ich um das problem irgendewie herum? denn ich möchte nicht bei jedem malloc dieselbe if abfrage mit errorout programmieren...
-
Nakaaa schrieb:
hm, das void eigentlich keinen Rückgabewert hat weiß ich. komme ich um das problem irgendewie herum? denn ich möchte nicht bei jedem malloc dieselbe if abfrage mit errorout programmieren...
//malloc.h #ifndef MALLOC_H #define MALLOC_H void * check_malloc(size_t bytes); //man beachte das Sternchen^^ void * check_malloc(size_t bytes) { void *mem; mem = malloc(bytes); if(mem == NULL) { fprintf(stderr, "ERROR : Out of memory ?!\n" " Trying to (m)allocate %i Bytes failed !\n" " File: %s Line: %d\n", (int) bytes); exit(1); } return mem; } #endif //MALLOC_H
//main.c #include "malloc.h" int main(void) { double d = check_malloc(5 * sizeof( d )); free ( d ); return 0; }
Btw. Die Funktion muss nur ein mal in der Header Datei deklariert werden, nicht noch mal in der main.c zusätzlich.
MfG
GPC
-
bzw: wie gebe ich einen void* zurück?
sorry für die dummheit...
-
wie alles andere mit return.
-
Ja, du mußt der Funktion sagen, daß sie einen Zeiger zurückgeben will:
void/*->*/ * /**/ check_malloc(size_t bytes);
(btw, wenn du schon den Header für deine Funktion einbindest, brauchst du keine zusätzliche Forward-Deklaration)
(und noch ein btw: In den Header gehört nur der Prototyp - die eigentliche Implementation solltest du in eine malloc.c auslagern. Spätestens wenn du das in einem größeren Projekt verwendest, wird dir dein Linker danken ;))
-
also erst mal ein kniefall für CStoll und GPC.
so schaut's jetzt aus und klemmt noch etwas...
//malloc.h #include <stdio.h> #include <string.h> #ifndef MALLOC_H #define MALLOC_H void * check_malloc(size_t bytes); #endif //MALLOC_H
//main.c #include "malloc1.h" int main(void) { double d = check_malloc(5 * sizeof( d )); free ( d ); void * check_malloc(size_t bytes) { void *mem; mem = malloc(bytes); if(mem == NULL) { fprintf(stderr, "ERROR : Out of memory ?!\n" " Trying to (m)allocate %i Bytes failed !\n" " File: %s Line: %d\n", (int) bytes); exit(1); } return mem; } return 0; }
wozu brauch man MALLOC_H?
Nakaa
-
Fehlermeldung:
malloc1.c: In functionmain': malloc1.c:6: error: incompatible types in initialization malloc1.c: In function
check_malloc':
malloc1.c:13: warning: assignment makes pointer from integer without a cast
-
Woop, was hast du denn jetzt gemacht? Bringen wir mal Ordnung rein:
//malloc.h #ifndef MALLOC_H #define MALLOC_H //Die includes müssen hier rein #include <stdio.h> #include <string.h> void * check_malloc(size_t bytes); #endif //MALLOC_H
//malloc.c #include "malloc.h" void * check_malloc(size_t bytes) { void *mem = malloc(bytes); if(mem == NULL) { fprintf(stderr, "ERROR : Out of memory ?!\n" " Trying to (m)allocate %d Bytes failed !\n" " File: %s Line: %d\n", bytes, __FILE__, __LINE__ ); exit(1); } return mem; }
//main.c #include "malloc.h" int main(void) { double *d = check_malloc(5 * sizeof( *d )); free ( d ); return 0; }
Okay, jetzt haben wir drei Dateien: malloc.h, malloc.c und main.c Die kompilierst und linkst du und dann läuft das.
wozu brauch man MALLOC_H?
Das ist ein so genannter Include-Guard: http://tutorial.schornboeck.net/pp_if.htm
MFG
GPC
-
GPC schrieb:
fprintf(stderr, "ERROR : Out of memory ?!\n" " Trying to (m)allocate %i Bytes failed !\n" " File: %s Line: %d\n", (int) bytes);
3 platzhalter aber nur 1 argument für 'fprintf' kann auch für seltsames verhalten sorgen
-
^^ Jo, da hab ich gepennt beim Copy & Paste. Ich nahm an, dass der Threadersteller fprintf im Griff hat...
so, jetzt passt's.
-
also leute,
double d = check_malloc(5 * sizeof( d ));
mag mein compile einfach nich.
main.c:5: error: incompatible types in initialization
und auch das warning wird ausgegeben.....
/* um das gesicht zu wahren, der fprint war auch gecopied und gepastet ...*/
-
Du hast es heute mit den Sternchen, oder
check_malloc() gibt einen Pointer zurück, also mußt du die Ausgabe auch einer Pointer-Variablen ("double * d = ...;") zuweisen.
-
...und weil d ein pointer sein sollte nimm '5*sizeof(double)' für 5 doubles, nicht '5*sizeof(d)'
-
verdammte axt!!!
also malloc gibt nen void pointer zurück, deshalb muss ich ne void * funktion haben und den rückgabewert auch nem pointer zuweisen.
demnächst besser denken vorm posten.
molte grazieeee!!!!!!!!!!!!!!!!!