![]() |
|
#1
|
|||
|
|||
Memory Block SizeI am writing a program trying to return the size of a memory block in C-Free on windows. I have been using the malloc_size() method and including the stdlib.h header file. When I try to run my program in minix-3 (which I have to do) malloc_size is undefined.
Isn't malloc_size in the standard minix library? If not does anyone know any way around this? Is there another way to get the size of a memory block? Thanks. |
|
#2
|
|||
|
|||
Re: Memory Block SizeBoy, malloc() must be topic of the week!
There are 2 or 3 threads showing which have given me a much better understanding of malloc that you might want to give a read. Especially '2D arrays:dynamic allocation and freeing'. I googled malloc_size and a man page comes up from Apple Developement (which interestingly is a BSD man(3) page) which is for malloc() and has malloc_size as well . (Also a lot of other people seem to be asking the same q as you!) The definition there is: Quote:
So if it is not available in your minix environment, I would think you could get this value from an object after it has been allocated memory by malloc(). I am new at this but I've attempted an example: CPP / C++ / C Code:
Well, I don't know if that's any help. I seem to be asking twice as many questions as I might hope to answer... Now, I read that in my nifty new old K&R (p186) that: Quote:
At any rate, you might try comparing the size of a malloc 'd object with what you can get using the malloc_size function you have available on your Windows machine. See what you get... Sorry. ++Howard; |
|
#3
|
|||
|
|||
Re: Memory Block SizeQuote:
Here's the thing: malloc() is a standard library function. That is to say: the C Language Standard defines its existence and its required behavior. Here is the entire description of malloc() from the C99 standard: Quote:
That's it! Little details like a variable named malloc_size and big details like how malloc() is supposed to do its thing are left to implementations (that is: to the compiler and library writers). In the appendix on "unspecified behavior", the Standard specifically mentions portability issues concerning: Quote:
In other words, successive calls may or may not give contiguous blocks of memory, and later calls may have lower addresses than earlier (or vice-versa --- the behavior is explicitly unspecified). Compiler and library implementers are free to do it any way that they bloomin' well please. (And they can change implementation details any time they want to.) Bottom line: anything that you discover (or guess) by experimenting with a particular compiler won't necessarily be valid with other compillers. Not even with other versions of the same compiler unless it is specifically documented for that particular version of that particular compiler. If it is open-source, than you may be able to look at the source code (lots of luck on that!!!) to find answers. Sometimes there is specific documentation (other than source code) about compiler internals, and, maybe even library internals. Good hunting! Regards, Dave Footnote: One other little detail: Not all compilers try equally hard to comply with any of the Standards. So, even if a Standard gives some requirement, any particular compiler may not do it that way (and may or may not bother to document any variances). However, if the Standard specifically mentions that certain behavior is "unspecified" or "undefined" or otherwise "unguaranteeed", then all compiler writers are off the hook. They can do just about anything (within the Standard's definition of "unspecified" or "undefined" behavior or results) and still claim compliance. |
|
#4
|
|||
|
|||
Re: Memory Block SizeQuote:
Quote:
It would be interesting to hear what the malloc_size was supposed to be used for in the original post and what his DOES return compared to what the size(mallocated object) is. And yes, I'm havin fun, thanks, ++Howard; |
|
#5
|
|||
|
|||
Re: Memory Block SizeGoing over the program I posted earlier in this thread I found that I hadn't really created what I thought I did. The idea was to allocate an array of ints or chars and look at the sizeof(array). I think I created an array of pointers intead! So my printout makes sense in that respect since a pointer is stored in size int:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
I'm thinking I need to declare it differently. Perhaps (*)array1 ? ...no success thusfar... Or, I guess just have to do the math since I don't have a malloc_size handy... On another matter with the program I previously posted: On my old k6-2 laptop in 98 with a bunch of stuff open using method2, malloc would max out at about 225000000 - 225500000 chars , , and I noticed that when I increase ints, I have to reduce chars accordingly. My ints don't seem to get freed. So, I don't think my: free(array1); ...is working correctly. Along with that, using method 1, interestingly, I max out at 259800 - 259900 (259826+-) but it's not my malloc error message, instead it's an alert window. I was thinking I was hitting a limit on the number of pointers available. The number is mysteryously close to a 256xxx or an ffff ffff kind of thing. But the alert says : (only included the top section) "MALLOC_SIZE1 caused a stack fault in module MALLOC_SIZE1.EXE at 0167:004095c2." It's not a deal, I was just curious about it. Anyhow if anyone might have any thoughts on these issues, especially the free() at this point, I'd be interested to hear. Thanks, ++Howard; |
|
#6
|
|||
|
|||
Re: Memory Block SizeQuote:
CPP / C++ / C Code:
Output on my 32-bit Linux system with GNU gcc: Code:
Output on my 64-bit Linux system with GNU gcc: Code:
Note that the size of a pointer is eight bytes, but an int is still four bytes. (The exact sizes of variables, including pointers, are not specified in any C or C++ Standard.) Also note that values of the expressions involving the "sizeof" operator in the examples are constants. The values of the expressions are known at compile time. Now if you create an "array" with malloc, there is no way (no way) that a standard C program can know the size of the block of memory that was actually created for its use. Your program knows how many bytes it requested and that's how many it was granted, and that is the number that it can legally use. Period. Full stop. Consider the following: CPP / C++ / C Code:
Output from the 32-bit system: Code:
The first sizeof() value tells the size of a pointer to double. The second one tells the size of a double. Again, the sizeof() operator in this example is something whose result is constant and is known at compile time (and in this case has absolutely nothing to do with the amount of memory that is in the block whose address is the value of the pointer). Quote:
If you free them, they are freed. That means that you can't legally use that particular block of memory any more. Depending on what else is going on at the time, if you have allocated everything that your program will be allowed (by the operating system) to allocated, it is just possible that if you free them you might not be able to allocate the same number of bytes again. 1. There is a certain amount of overhead associated with blocks of memory obtained from malloc. Allocating many small blocks may actually cause more bytes of memory to be reserved for your program than allocating one large block. This is one of those "implementation" details, and not all malloc() library functions behave the same way, but is "typical", for cases that I have personally investigated. (In other words Your Mileage May Vary.) 2. There is no inherent requirement for garbage collection in the standard library function malloc() and it is quite possible that if you allocate and free a number of blocks that you might end up with an allocatable pool of memory that has enough bytes for your next call to malloc(), but no single block has enough contiguous bytes to satisfy the request. The standard absolutely requires that the bytes be contiguous, since that is a requirement to be able to treat the contents of the block as an array. Due to this shortcoming (lack of specified garbage collection in C), there have been a number of "garbage collection" functions (and C++ classes) that have been published to do the deed. 3. If you have returned your bytes to the operating system's memory pool (by using free()), some other process may have claimed them in the interval between the time that you freed them and the time that you requested them again. This behavior depends on the operating system, and is even more beyond the control or knowledge of the lowly C programmer than any of the implementation-dependent characteristics of the library function. In other words: There is an absolute limit on the size of the virtual address space for any given CPU. This represents an upper bound on the total memory for all processes that are running at a given time. A particular compiler (and linker and loader) might have some lower limit for a program. Operating system settings (we used to call it Job Control Language) may set limits on the amount of memory that the operating system will make available to a given class of program. In other words: A general-purpose operating system (Windows, Linux) actually represents a non-deterministic system! Programmers can develop a sense of reasonable expectations and, with good programming practices, can protect against lots of stuff, but if you really have a need, in today's program, for several gigabytes of array storage to accomplish a mission-critical task, you probably need a little more control over the operating environment than stock versions of commercial operating systems are likely to provide. In fewer words: Your Mileage May Vary. A final note: malloc() is a standard library function, and we are discussing the Standards-imposed requirements of malloc(). There aren't any, other than the couple of sentences that I quoted earlier. Library function writers for popular compiler distributions seem to favor speed first, memory overhead second, and garbage collection never. I, personally, would not argue with those decisions. If I have special needs, I will take care of them with special functions. Regards, Dave |
|
#7
|
|||
|
|||
Re: Memory Block SizeWhew,, that's a print... Thanks for taking the time.
re: sizeof(mallocated array) So we use the math method on known values... re: free() I guess in situations like this I just need to be aware of the behavior and what I might expect to find (and look out for) the next time I approach the limits. Gosh,, and I'm supposed to be working on console manipulation..... sidetracked AGAIN! Thanks, ++Howard; |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 20:23 |
| Strange C++ code memory leakage problem | gaoanyu | C++ Forum | 7 | 04-Nov-2005 09:09 |
| Pointer Usage in C++: Beginner to Advanced | varunhome | C++ Forum | 0 | 19-Aug-2005 10:25 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 19:35 |
| Having a problem | Chuckles | Computer Hardware Forum | 19 | 13-Sep-2004 13:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The