I im trying to declare a pointer and allocate some space for it
I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code? char *p; *p = malloc(10); The pointer you declared is p, not *p. When you're manipulating the pointer itself (for example when you're setting it to make it point somewhere), you just us... 2016-06-10, 1337👍, 0💬
What is wrong with this initialization?
What's wrong with this initialization? char *p = malloc(10); My compiler is complaining about an ``invalid initializer'', or something. Is the declaration of a static or non-local variable? Function calls are allowed in initializers only for automatic variables (that is, for local, non-static variab... 2016-06-03, 1373👍, 0💬
How can I shut off the warning: possible pointer alignment problem ...
How can I shut off the ``warning: possible pointer alignment problem'' message which lint gives me for each call to malloc? A modern lint shouldn't be complaining about this. Once upon a time, lint did not and could not know that malloc ``returns a pointer to space suitably aligned for storage of an... 2016-06-03, 1033👍, 0💬
How can I dynamically allocate arrays?
How can I dynamically allocate arrays? The equivalence between arrays and pointers allows a pointer to malloc'ed memory to simulate an array quite effectively. After executing #include <stdlib.h> int *dynarray; dynarray = malloc(10 * sizeof(int)); (and if the call to malloc succeeds), you can... 2016-04-23, 1121👍, 0💬
How can I find out how much memory is available?
How can I find out how much memory is available? Your operating system may provide a routine which returns this information, but it's quite system-dependent. (Also, the number may vary over time.) If you're trying to predict whether you'll be able to allocate a certain amount of memory, just try it-... 2016-04-23, 1216👍, 0💬
Return a null pointer or a pointer to 0 bytes? .....
What should malloc(0) do? Return a null pointer or a pointer to 0 bytes? The ANSI/ISO Standard says that it may do either; the behavior is implementation-defined ortable code must either take care not to call malloc(0), or be prepared for the possibility of a null return. 2016-04-21, 1076👍, 0💬
I have heard that some operating systems dont actually allocate...
I've heard that some operating systems don't actually allocate malloc'ed memory until the program tries to use it. Is this legal? It's hard to say. The Standard doesn't say that systems can act this way, but it doesn't explicitly say that they can't, either. (Such a ``deferred failure'' implementati... 2016-04-21, 1086👍, 0💬
malloc is returning crazy pointer values ...
malloc is returning crazy pointer values, I have included the line extern void *malloc(); before I call it. malloc accepts an argument of type size_t, and size_t may be defined as unsigned long. If you are passing ints (or even unsigned ints), malloc may be receiving garbage (or similarly if you are... 2016-04-15, 1143👍, 0💬
I am allocating a large array for some numeric work ...
I'm allocating a large array for some numeric work, using the line double *array = malloc(300 * 300 * sizeof(double)); malloc isn't returning null, but the program is acting strangely, as if it's overwriting memory, or malloc isn't allocating as much as I asked for, or something.. Notice that 300 x ... 2016-04-15, 748👍, 0💬
Why can I only seem to malloc 640K or so?. .....
I've got 8 meg of memory in my PC. Why can I only seem to malloc 640K or so?. Under the segmented architecture of PC compatibles, it can be difficult to use more than 640K with any degree of transparency, especially under MS-DOS. 2016-04-12, 749👍, 0💬
My application depends heavily on dynamic allocation of nodes for data structures ......
My application depends heavily on dynamic allocation of nodes for data structures, and malloc/free overhead is becoming a bottleneck. What can I do ? One improvement, which is particularly attractive if all nodes are the same size, is to place unused nodes on your own free list, rather than actually... 2016-04-12, 864👍, 0💬
y program is crashing, apparently somewhere down inside malloc ....
My program is crashing, apparently somewhere down inside malloc, but I can't see anything wrong with it. Is there a bug in malloc? It is unfortunately very easy to corrupt malloc's internal data structures, and the resulting problems can be stubborn. The most common source of problems is writing mor... 2016-04-06, 678👍, 0💬
I im dynamically allocating an array, like this ...
I'm dynamically allocating an array, like this: int *iarray = (int *)malloc(nints); malloc isn't returning NULL, but the code isn't working. A:malloc is a low-level, typeless allocator. It doesn't know how you're going to use the memory; all it does is to allocate as many bytes of memory as you ask ... 2016-04-06, 790👍, 0💬
You cant use dynamically-allocated memory after you free it?
You cant use dynamically-allocated memory after you free it? No. Some early documentation for malloc stated that the contents of freed memory were ``left undisturbed,'' but this ill-advised guarantee was never universal and is not required by the C Standard. Few programmers would use the contents of... 2016-04-04, 710👍, 0💬
Why isn't a pointer null after calling free? ...
Why isn't a pointer null after calling free? How unsafe is it to use (assign, compare) a pointer value after it's been freed? When you call free, the memory pointed to by the passed pointer is freed, but the value of the pointer in the caller probably remains unchanged, because C's pass-by-value sem... 2016-04-04, 629👍, 0💬
When I call malloc to allocate memory for a pointer which is local to a function ....
When I call malloc to allocate memory for a pointer which is local to a function, do I have to explicitly free it? Yes. Remember that a pointer is different from what it points to. Local variablesare deallocated when the function returns, but in the case of a pointer variable, this means that the po... 2016-03-31, 675👍, 0💬
I am allocating structures which contain pointers to other dynamically ....
I'm allocating structures which contain pointers to other dynamically-allocated objects. When I free a structure, do I also have to free each subsidiary pointer? Yes. malloc knows nothing about structure declarations or about the contents of allocated memory; it especially does not know whether allo... 2016-03-31, 713👍, 0💬
Must I free allocated memory before the program exits?
Must I free allocated memory before the program exits? You shouldn't have to. A real operating system definitively reclaims all memory and other resources when a program exits; the system cannot afford to have memory integrity depend on the whims of random programs. (Strictly speaking, it is not eve... 2016-03-28, 603👍, 0💬
I have a program which mallocs and later frees a lot of memory ...
I have a program which mallocs and later frees a lot of memory, but I can see from the operating system that memory usage doesn't actually go back down. Most implementations of malloc/free do not return freed memory to the operating system, but merely make it available for future malloc calls within... 2016-03-28, 689👍, 0💬
How does free know how many bytes to free?
How does free know how many bytes to free? The malloc/free implementation remembers the size of each block as it is allocated, so it is not necessary to remind it of the size when freeing. (Typically, the size is stored adjacent to the allocated block, which is why things usually break badly if the ... 2016-03-24, 629👍, 0💬
So can I query the malloc package to find out how big an allocated block is?
So can I query the malloc package to find out how big an allocated block is? Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself. 2016-03-24, 616👍, 0💬
Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?
Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer? sizeof tells you the size of the pointer. There is no portable way to find out the size of a malloc'ed block. (Remember, too, that sizeof operates at compile time 2016-03-21, 508👍, 0💬
Having dynamically allocated an array , can I change its size?
Having dynamically allocated an array , can I change its size? Yes. This is exactly what realloc is for. Given a region of malloced memory its size can be changed using code like: dynarray = realloc(dynarray, 20 * sizeof(int)); Note that realloc may not always be able to enlarge memory regions in-pl... 2016-03-21, 587👍, 0💬
Is it legal to pass a null pointer as the first argument to realloc? Why would you want to?
Is it legal to pass a null pointer as the first argument to realloc? Why would you want to? ANSI C sanctions this usage (and the related realloc(..., 0), which frees), although several earlier implementations do not support it, so it may not be fully portable. Passing an initially-null pointer to re... 2016-03-16, 582👍, 0💬
What does static variable mean? There are 3 main uses for static variables: If you declare within a ...
Write an equivalent expression for x%8? x&7
How To Enter Numeric Values as HEX Numbers? - MySQL FAQs - Introduction to SQL Basics If you want to...
If client side validation is enabled in your Web page, does that mean server side code is not run? W...
What is the version information in XML? “version” tag shows which version of XML is used.