sizeof(char) is always 1. Code is simpler by removing the multiplication.
Code:char *p = malloc(10); if (p == NULL) { perror("malloc"); return 1; }
sizeof(char) is always 1. Code is simpler by removing the multiplication.
Code:char *p = malloc(10); if (p == NULL) { perror("malloc"); return 1; }
Hello,
Whenever you allocate memory, you must free it. To keep this simple the ANSI-C function free() deallocates dynamically allocated memory. A compatible way to free memory allocated by malloc() is free().
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
and leaves the OP to perhaps think all he needs to give malloc as an argument is a number? no thanks,Originally Posted by [Ren]
if nothing else my code gets you into the habit of specify the sizeof of each storage area you need. Its assumed the OP didn't know this, based on his origianl entry.a little implicit, but i'm sure you understand.
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun
a, so. When using sizeof, is it not better to avoid using the type directly?Originally Posted by caroundw5h
Code:char *p = malloc(10 * sizeof *p);
It's best written as
On the principle that if there is a way to do things which is always correct, then that is the preferred way to do it. Then you don't have to think about it since that will be the way it is done out of habit, and that bit of code will be correct out of habit.Code:char *p = malloc( 10 * sizeof *p );
Imagine you started with
Now suppose you come along later and decide you want a wchar_t instead.Code:char *p = malloc ( 10 * sizeof(char) ); or char *p = malloc ( 10 );
In some hasty moment, you only change one thing instead of two.
Neither will draw a compiler error message, but pain and suffering can't be far away.Code:wchar_t *p = malloc ( 10 * sizeof(char) ); // oops, forgot to change the sizeof or wchar_t *p = malloc ( 10 ); // oops, forgot to add a sizeof
I bet now you think this is a reason to use a cast, so you get an error message when you change the type of the pointer. Whilst this is true (you will get an error message), it is not a reasonable tradeoff.
Sure you get a "initialization from incompatible pointer type" message if you change the type of p, but only at the expense of suppressing a rather more serious "initialization makes pointer from integer without a cast" which you would otherwise get if you failed to include stdlib.h in the first place.Code:char *p = (char*)malloc( 10 * sizeof(char) );
The other downside is that you've now got 3 things to change, and still no guarantee that you won't end up with equally broken code as before.
Except now having compiled it once, got a "initialization from incompatible pointer type" message and fixed it, you might be more tempted to believe that the code is correct because you've made the change. Several crashes later, you have a "DOH!!" moment and you're back on track.Code:wchar_t *p = (wchar_t*)malloc( 10 * sizeof(char) ); // oops, you still lose!
May it happens doesn't make it good practice. People do mistakes. I won't encourage them...Originally Posted by caroundw5h
There is no such a monster. C an C++ are separated and different languages.its actully so common there is a language called C/C++
Emmanuel Delahaye
"C is a sharp tool"
This string is expandable (e.g. with strcat())Originally Posted by thinhare
This one is not expandable (it occupies the whole room already). It can be shrinked.it still works even if 100 is omitted. and seems the string could be appended to any desired longer. like this:
char s[] = "Hello ";
Note that the size of the array (sizeof operator) will remain unchanged, only the length of the string (strlen()) will change.
Emmanuel Delahaye
"C is a sharp tool"
strcpy() and strcat() have no size info. strncpy() and strncat() have it, but their correct use is not trivial...Originally Posted by thinhare
No It's called simplicity. The C language is a sharp tool. Can be dangerous in beginner's hands...that might just be that so-called flexibility.
Emmanuel Delahaye
"C is a sharp tool"
That was actually a joke.There is no such a monster. C an C++ are separated and different languages.
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun
That may well hold truth, but I somehow doubt the original poster tries to learn C and C++ at the same time.Originally Posted by caroundw5h
Rather I'd say, malloc was casted because stdlib.h was not included - thereby causing the compiler to default to return-type int and complain about the type mismatch. At least I know I once did that :-)
main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }