1 2 3 4 5 6 > >>   Sort: Rank

In a call to malloc, what does an error like
In a call to malloc, what does an error like ``Cannot convert `void *' to `int *''' mean? It means you're using a C++ compiler instead of a C compiler.
2016-06-21, 330👍, 0💬

I see code like ...
I see code like char *p = malloc(strlen(s) + 1); strcpy(p, s); Shouldn't that be malloc((strlen(s) + 1) * sizeof(char))? It's never necessary to multiply by sizeof(char), since sizeof(char) is, by definition, exactly 1. (On the other hand, multiplying by sizeof(char) doesn't hurt, and in some circum...
2016-06-21, 293👍, 0💬

I wrote a little wrapper around malloc ...
I wrote a little wrapper around malloc, but it doesn't work: #include lt;stdio.h> #include lt;stdlib.h> mymalloc(void *retp, size_t size) { retp = malloc(size); if(retp == NULL) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); } } Are you sure the function initialized what you thought it di...
2016-06-10, 330👍, 0💬

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, 357👍, 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, 332👍, 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, 322👍, 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 ref...
2016-04-23, 431👍, 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, 378👍, 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, 408👍, 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, 393👍, 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, 414👍, 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, 373👍, 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, 351👍, 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, 432👍, 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, 333👍, 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, 328👍, 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, 304👍, 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, 309👍, 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, 299👍, 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, 272👍, 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, 265👍, 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, 276👍, 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, 278👍, 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, 269👍, 0💬

1 2 3 4 5 6 > >>   Sort: Rank