473,785 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof([ALLOCATED MEMORY])

If I have malloc()'ed a pointer and want to read from it as if it were
an array, I need to know that I won't be reading past the last index.

If this is a pointer to a pointer, a common technique seems to be
setting a NULL pointer to the end of the list, and here we know that
the allocated memory has been exhausted. All good.

When this is a pointer to another type, say int, I could have a
variable that records how much memory is being allocated and use that
to track the size of the 'array'.
Alternatively, we could set the end of the 'array' to some kind of
error-code, such as 99 or MAX_INT.
I don't like either of these techniques.

So, what is a good way to stop a loop reading or writing past the
memory allocated to a pointer?
Or if possible, what is a good way of determining the size of memory
allocated to a pointer?

Cheers,
Matt

May 3 '06
74 4698
In article <44************ ***@yahoo.com>,
CBFalconer <cb********@yah oo.com> wrote:
Howard Hinnant wrote:
Keith Thompson <ks***@mib.or g> wrote:

... snip ...

You're assuming the information is there. I can imagine that it
might be either nonexistent or meaningless in some implementations .
(I don't know enough about actual implementations to know how
common this is.)


I know. I've written commercial malloc systems for both desktop and
embedded (even bareboard) systems. If you're going to implement the C
realloc interface, you have to know the size of a pointer passed to you.


Not necessarily. Consider the hypothetical "one time use"
allocater. realloc need only allocate a new chunk of the
appropriate size and copy the old over. It doesn't need to know
the size of the old, even for the copying if it can detect 'the
end' by some other means, analogous to encountering EOF.


I never said that sizeof_alloc(p) should have O(1) complexity. That
indeed would be over constraining. The "one time use" allocator still
must know the sizeof_alloc(p) in order to realloc; in the same way that
strlen(s) knows the size of the string s. One way or another, it must
know the size of the old memory to copy.

The smart interface would combine an allocation (e.g. malloc), with the
size query, so that an efficient implementation is more likely:

I'm requesting N bytes. Please give me the pointer (if able) and the
actual number of bytes it is pointing to.

-Howard
May 15 '06 #51
Howard Hinnant wrote:

In article <44************ ***@yahoo.com>,
CBFalconer <cb********@yah oo.com> wrote:
Howard Hinnant wrote:
Keith Thompson <ks***@mib.or g> wrote:

... snip ...
>
> You're assuming the information is there. I can imagine that it
> might be either nonexistent or meaningless in some implementations .
> (I don't know enough about actual implementations to know how
> common this is.)

I know. I've written commercial malloc systems for both desktop and
embedded (even bareboard) systems. If you're going to implement the C
realloc interface, you have to know the size of a pointer passed to you.


Not necessarily. Consider the hypothetical "one time use"
allocater. realloc need only allocate a new chunk of the
appropriate size and copy the old over. It doesn't need to know
the size of the old, even for the copying if it can detect 'the
end' by some other means, analogous to encountering EOF.


I never said that sizeof_alloc(p) should have O(1) complexity. That
indeed would be over constraining. The "one time use" allocator still
must know the sizeof_alloc(p) in order to realloc; in the same way that
strlen(s) knows the size of the string s. One way or another, it must
know the size of the old memory to copy.


Read again please. I postulated a copy mechanism that detected the
equivalent of EOF. Much like:

while (*dest++ = *source++) continue;

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
May 16 '06 #52
CBFalconer posted:
while (*dest++ = *source++) continue;

Redundant, yet nonetheless depictive, use of "continue".

I wonder, however, if it would hinder the compiler's optimization?
-Tomás

May 16 '06 #53
Tomás wrote:

CBFalconer posted:
while (*dest++ = *source++) continue;


Redundant, yet nonetheless depictive, use of "continue".


There are some style guidlines against writing code like this:
while (*dest++ = *source++);
because it looks like an accident.

Myself, I would write that this way:
do {
*dest = *source++;
} while (*dest++ != 0);

I'm not into cramming vertical space.

--
pete
May 16 '06 #54
pete posted:
There are some style guidlines against writing code like this:
while (*dest++ = *source++);
because it looks like an accident.

while (*dest++ = *source++); /* Not an accident */

;)

Myself, I would write that this way:
do {
*dest = *source++;
} while (*dest++ != 0);

More than one way to skin a cat. If I were looking for the most efficient
way, I'd use the "register" keyword for "dest" and "source", and I'd
probably do the following:

while ( *dest = *source ) ++dest, ++source;

This would remove the redundant last incrementation.

-Tomás
May 16 '06 #55
Tomás wrote:
More than one way to skin a cat.
If I were looking for the most efficient way,
I'd use the "register" keyword for "dest" and "source",
I don't use the register keyword.

"Smaller, faster programs can be expected if register
declarations are used appropriately, but future improvements
in code generation may render them unnecessary."
-- K&R, A 8.1, 1978
and I'd probably do the following:

while ( *dest = *source ) ++dest, ++source;

This would remove the redundant last incrementation.

http://www.prism.uvsq.fr/~cedb/local_copies/lee.html
Optimization is simply waste of programmer time if any of these
statements are true:
parts of the program haven't been written yet
the program is not fully tested and debugged
it seems to run fast enough already

Jackson's Rules of Optimisation:
Rule 1: Don't do it.
Rule 2: (for experts only) Don't do it yet - that is, not until you
have a perfectly clear and unoptimized solution.
- Michael Jackson

--
pete
May 16 '06 #56
pete posted:
Jackson's Rules of Optimisation:
Rule 1: Don't do it.
Rule 2: (for experts only) Don't do it yet - that is, not until you
have a perfectly clear and unoptimized solution.
- Michael Jackson

I prefer to think for myself -- I tend to be more open-minded, more
creative, more inventive, more intuitive, and more intelligent than the
person who's trying to spoon-feed me guidelines.

The following code is A-OK by me:

void Strcpy( register char* dest, register const char* source )
{
while ( *dest = *source ) ++dest, ++source;
}
You have your way of doing things, and I have mine. I'm sure we both get
the job done... but I get that extra sprinkle of satisfaction from knowing
I perfected the code to the best of my ability.

-Tomás
May 16 '06 #57
On Tue, 16 May 2006 04:21:29 GMT, "Tomás" <NU**@NULL.NULL > wrote:
pete posted:
Jackson's Rules of Optimisation:
Rule 1: Don't do it.
Rule 2: (for experts only) Don't do it yet - that is, not until you
have a perfectly clear and unoptimized solution.
- Michael Jackson
Rule 2.5: It's much easier to make correct code fast than to make fast
code correct.

I prefer to think for myself -- I tend to be more open-minded, more
creative, more inventive, more intuitive, and more intelligent than the
person who's trying to spoon-feed me guidelines.

The following code is A-OK by me:

void Strcpy( register char* dest, register const char* source )
{
while ( *dest = *source ) ++dest, ++source;
}
You have your way of doing things, and I have mine. I'm sure we both get
the job done... but I get that extra sprinkle of satisfaction from knowing
I perfected the code to the best of my ability.

It's even possible that the compiler can optimize this code in spite
of your attempt to interfere ;-)

--
Al Balmer
Sun City, AZ
May 16 '06 #58
On Tue, 16 May 2006 04:21:29 GMT, "Tomás" <NU**@NULL.NULL > wrote:
pete posted:
Jackson's Rules of Optimisation:
Rule 1: Don't do it.
Rule 2: (for experts only) Don't do it yet - that is, not until you
have a perfectly clear and unoptimized solution.
- Michael Jackson

I prefer to think for myself -- I tend to be more open-minded, more
creative, more inventive, more intuitive, and more intelligent than the
person who's trying to spoon-feed me guidelines.

The following code is A-OK by me:

void Strcpy( register char* dest, register const char* source )
{
while ( *dest = *source ) ++dest, ++source;
}
You have your way of doing things, and I have mine. I'm sure we both get
the job done... but I get that extra sprinkle of satisfaction from knowing
I perfected the code to the best of my ability.

-Tomás


Well, you could be surprised to see the assembler code geenrated by
the compiler. Many timed, the followin lines give all of them the same
final code:

while ( *dest = *source ) ++dest, ++source;

for(;*dest = *source;++dest, ++source);

while ( *dest++ = *source++ );

for(;*dest++ = *source++;);

None of them is the most optimal until proven woth some profiler. You
may choose your preferred style, but don't take for grnated it is the
more optimized solution. Optimum solutions may be optimum in code size
but not in speed, or the contrary, or optimum in local stack size but
not in code size, or whatever.

Profile the results of your compiler. This is not a guideline, this is
the only recognized way to find where to optimize your code.

Best regards,

Zara
May 16 '06 #59
while ( *dest = *source ) ++dest, ++source;
This one doesn't do a redundant final incrementation.

for(;*dest = *source;++dest, ++source);

Nor does this one.

while ( *dest++ = *source++ );

This one does however.

for(;*dest++ = *source++;);

As does this one.
-Tomás
May 16 '06 #60

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2048
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
4
13012
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage reported through task manager peaks between 41-42MB. I've stopped and restarted the MSSQLserver service and checked that the running values are what I set them to be. Does anybody have any ideas as to why the sqlservr.exe would be utilizing more...
0
1053
by: Bill Burwell | last post by:
Which memory properties, or what combinations of memory properties, provide useful information about a program's memory usage when that program has just started leaking memory? While I have a VB bias, it seems to me the answer to this question should be generic - that is language independent.
4
2591
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
9
2354
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When the OS is asking for it is usually too late, tons of swapping and slow performance.
22
3484
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from application server, will each connection take the memory 800M (200000 x 4k = 800 M), so the memory took will be 800M times number of connections, or the total memory get from bufferpool will be 800M?
14
20786
by: Alessandro Monopoli | last post by:
Hi all, I'm searching a PORTABLE way to get the available and total physical memory. Something like "getTotalMemory" and it returns the memory installed on my PC in bytes, and "getAvailableMemory" and it returns the available memory in bytes. Do you know is there's a C function, a c++ Object or anything else that compiles in Linux and Windows to get these data?
5
24811
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
1
2047
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after the objects for which that memory was allocated are no longer live in the process. This is only a leak if peak memory goes up again each time you create any new objects. Try repeated allocations of a large dictionary and observe how memory...
5
505
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new" and push back into the vector,this is done inside a for loop for
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.