Malloc Sizeof Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow
Malloc In C Example
the company Business Learn more about hiring developers or posting ads with us Stack c malloc array Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of
C Malloc Struct
6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up malloc: error checking and freeing memory up vote 0 down vote favorite I'm using malloc to make malloc in c++ an error check of whether memory can be allocated or not for the particular array z1. ARRAY_SIZE is a predefined with a numerical value. I use casting as I've read it's safe to do so. long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE); if(z1 == NULL){ printf("Out of memory\n"); exit(-1); } The above is just a snippet of my code, but when I add the error checking part (contained calloc in c in the if statement above), I get a lot of compile time errors with visual studio 2008. It is this error checking part that's generating all the errors. What am I doing wrong? On a related issue with malloc, I understand that the memory needs to be deallocated/freed after the variable/array z1 has been used. For the array z1, I use: free(z1); z1 = NULL; Is the second line z1 = NULL necessary? I get 102 errors...well MVS2008 stops the errors at 102. The errors are of type: error C2143: syntax error : missing ';' before 'type' error C2065: 'L' : undeclared identifier // this error repeats for all my identifiers and this points right after the closing } in the if statement. ARRAY_SIZE is quite large. I define it as #define ARRAY_SIZE 2500001 My full above code is too long. But I have a smaller code which is giving me the same behavior. Sorry for the formatting. I can't seem to get it right. #include stdio.h //note I have the actual < > in my code #include stdlib.h #include math.h #define ARRAY_SIZE 11 #define VECTOR_SIZE 5 main() { long double *z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE); if(z == NULL){ printf("Out of memory\n"); exit(-1); } long double *k = (long double*
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers
What Is Malloc
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x free in c Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it
Malloc Error Checking
only takes a minute: Sign up Why is it safer to use sizeof(*pointer) in malloc up vote 33 down vote favorite 17 Given struct node { int a; struct node * next; }; To malloc a new structure, struct http://stackoverflow.com/questions/2574642/malloc-error-checking-and-freeing-memory node *p = malloc(sizeof(*p)); is safer than struct node *p = malloc(sizeof(struct node)); Why? I thought they are the same. c malloc share|improve this question edited Oct 13 '13 at 16:04 asked Jun 23 '13 at 7:42 Yu Hao 84.4k18116177 7 FYI, when used with a variable, sizeof doesn't need parens: p = malloc(sizeof*p); –luser droog Jun 23 '13 at 7:46 46 It's more readable with parens –Tomer Arazy Jun 23 '13 at 7:48 2 @TomerArazy: I http://stackoverflow.com/questions/17258647/why-is-it-safer-to-use-sizeofpointer-in-malloc beg to differ; it is less readable with parens. Without parens, it cannot be a typename; the lack of parens helps the human reader to disambiguate. (minor issue: also, the parens could suggest that sizeof is a function, which it is not) –wildplasser Jun 23 '13 at 9:18 10 It is more readable without parentheses but with a space: sizeof *p. –Eric Postpischil Jun 23 '13 at 11:47 4 It is not more readable with parens, or without parens. These are opinions, not facts. But it is fact that the parens aren't necessary because sizeof applies as a unary operator to an expression. If you don't always use parentheses with every unary operators, why always use them with sizeof. –Kaz Jun 23 '13 at 15:27 | show 1 more comment 4 Answers 4 active oldest votes up vote 55 down vote accepted It is safer becuse you don't have to mention the type name twice and don't have to build the proper spelling for "dereferenced" version of the type. For example, you don't have to "count the stars" in int *****p = malloc(100 * sizeof *p); Compare that to the type-based sizeof in int *****p = malloc(100 * sizeof(int ****)); where you have too make sure you used the right number of * under sizeof. In order to switch to another type you only have to change one place (the declaration of p) instead of
sizeof operator returns the size in bytes of its operand. Whether the result of sizeof is unsigned int or unsigned long is implementation defined--which is http://publications.gbdirect.co.uk/c_book/chapter5/sizeof_and_malloc.html why the declaration of malloc above ducked the issue by omitting any parameter information; https://www.quora.com/In-a-C-program-with-the-function-int-*-malloc-sizeof-int-what-does-int-*-mean normally you would use the stdlib.h header file to declare malloc correctly. Here is the last example done portably: #include
C program with the function (int *) malloc (sizeof(int)), what does (int *) mean?UpdateCancelAnswer Wiki20 Answers Anders Kaseorg, MIT PhD student in CS; Cofounder of Ksplice, Inc.Written 103w agomalloc()
returns a pointer of type void *
, and (int *)
denotes a type cast that explicitly converts the pointer’s type to int *
. In C (but not C++), this conversion would happen implicitly even without the cast. So the next question to ask should be: is it good programming style to write this cast explicitly?Converting pointer types is in general an unsafe operation in C. Careless use of pointer casts can lead to either spectacular crashes if you get the type wrong, or subtle misbehavior if you try to “cleverly” use the same memory with multiple types and end up violating the strict aliasing rules. But a void *
pointer must be converted (implicitly or explicitly) in order for its memory to be used at all, so it’s important to have good conventions that make bugs easier to spot.Option 1: void *p = malloc(sizeof(int));
This is obviously a bad idea. There’s no type conversion at the initialization site, but now all the users of p
needs to convert it, so cast operators will need be scattered throughout large sections of your code. The compiler cannot check that all of the conversions are consistent with each other and with the definition. Don’t do this.Option 2: int *p = malloc(sizeof(int));
Here the conversion happens once implicitly at the initialization site. p
can be used without casting it, and most misuses of p
will be detected by the compiler. (You are compiling with warnings enabled, right? Right.) There’s still a potential mismatch between the two mentions of the type that would not be detected, and a programmer might change one while forgetting to change the other: long *p = malloc(sizeof(int))
. The programmer will probably notice if the declaration and initialization are on the same line, but it might be harder to notice if they’re far away from each other: long *p; /* lots of code; */ p = malloc(sizeof(int))
.Option 3: int *p = (int *)malloc(sizeof(int));
Some C programmers will complain that this looks redundant: we have to mention the type three times instead of two! However,