473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Memory Manipulation

Tom
I am seeking an efficient method to "clip" the front-end off a large
block of memory. For example, let's say there is a block of memory
holding 100,000 structures of "data" and you want to drop the first
20,000 structures and free that memory.

Must the program overwrite the first 80,000 structures?

for(int i = 0; i < 80000; ++i) data[i] = data[i+20000];

Followed by a realloc() call. Thus effectively clipping off the
back-end and not the front-end?

If the above is correct ... I understand realloc() can physically move
memory too. Thus moving once to overwrite and possible moving again to
free up a little memory. This seems very inefficient. Although I
suspect realloc() seldom moves memory when reducing the block size?
Probably occurs when increasing block size and only when a contiguous
block is available?

Any better ways to skin the cat?

Thanks.
Jan 13 '07 #1
4 3615
"Tom" <Th********@earthlink.netwrote in message
news:df********************************@4ax.com...
>I am seeking an efficient method to "clip" the front-end off a large
block of memory. For example, let's say there is a block of memory
holding 100,000 structures of "data" and you want to drop the first
20,000 structures and free that memory.

Must the program overwrite the first 80,000 structures?

for(int i = 0; i < 80000; ++i) data[i] = data[i+20000];

Followed by a realloc() call. Thus effectively clipping off the
back-end and not the front-end?

If the above is correct ... I understand realloc() can physically move
memory too. Thus moving once to overwrite and possible moving again to
free up a little memory. This seems very inefficient. Although I
suspect realloc() seldom moves memory when reducing the block size?
Probably occurs when increasing block size and only when a contiguous
block is available?
There are two cats to skin (and gosh, I hesitate to use that idiom, because
I'm a cat owner):

CAT #1: MOVING LOTS OF MEMORY

The memmove() function will probably copy the memory more efficiently than
the array iteration above. It should be self-evident how to obtain the
pointers to pass to this function in 'C'.

CAT #2: DATA STRUCTURE

As best I know there is no standard 'C' function to effectively modify the
start address of a memory block in the way you wish.

An obvious solution is to store the array elements in the reverse order as
you've assumed above, then you can simply realloc() away the last 20,000
elements.

The most common approach in 'C' for this problem is to store an array of
pointers to the objects (and for this to be effective, a pointer has to be
much smaller than a data object). Then, the pointers are manipulated rather
than the actual data.

However, 1 realloc() and 20,000 free()s is going to take far more time than
a copy.
------------------------------------------------------------
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 13 '07 #2
"Tom" <Th********@earthlink.netwrote in message
news:df********************************@4ax.com...
>
Although I
suspect realloc() seldom moves memory when reducing the block size?
Probably occurs when increasing block size and only when a contiguous
block is available?
It isn't safe to make any assumptions about the way realloc() will behave
except as documented.

Your assumptions are probably reasonable, especially the decreasing block
size assumption, but ... the documented interface is the documented
interface.

------------------------------------------------------------
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 13 '07 #3
>
CAT #1: MOVING LOTS OF MEMORY

The memmove() function will probably copy the memory more efficiently than
the array iteration above. It should be self-evident how to obtain the
pointers to pass to this function in 'C'.
memmove() just moves the contents of the memory pointed by the
pointeers, however it doesn't free anything, which should be taken care
explicitly...

Jan 13 '07 #4
"Tom" <Th********@earthlink.netwrote in message
>I am seeking an efficient method to "clip" the front-end off a large
block of memory. For example, let's say there is a block of memory
holding 100,000 structures of "data" and you want to drop the first
20,000 structures and free that memory.

Must the program overwrite the first 80,000 structures?

for(int i = 0; i < 80000; ++i) data[i] = data[i+20000];

Followed by a realloc() call. Thus effectively clipping off the
back-end and not the front-end?

If the above is correct ... I understand realloc() can physically move
memory too. Thus moving once to overwrite and possible moving again to
free up a little memory. This seems very inefficient. Although I
suspect realloc() seldom moves memory when reducing the block size?
Probably occurs when increasing block size and only when a contiguous
block is available?

Any better ways to skin the cat?
Realloc clips off the back. There is no standard library function to
reallocate whilst clipping off the front, so you have to to a memmove()
first followed by realloc(). Potentially you are copying the data twice.

There are three routes.

1) Use a different data structure. eg linked list rather than a big array.
This is probbaly the best answer.

2) Write your own malloc(). This isn't necessarily as bad as it seems. You
can then implent an efficient realloc_clip_front() routine.

3) Hack into the system malloc() to write realloc_clip_front(). This might
be easy but will probably be rather difficult, depending on your system. You
will have to rewrite the function to port the program.
Jan 13 '07 #5

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

Similar topics

5
by: Lionel | last post by:
Hi all, Just wondering exactly how memory is allocated using the new operator? More specifically I am interested in how the memory is calculated for globable variables? I recently stumbled into...
20
by: iouswuoibev | last post by:
When writing a function that manipulates data (for example, a string), is it in principle a good or bad idea to allocate/deallocate memory within that function? For example, I have the following...
5
by: Nadav | last post by:
Hi, I am trying to get a direct pointer to the address of the screen ( something like 0x0b00 in the happy DOS days ), how can I do that? Should this be done in the kernel or it can also be done...
4
by: Chris | last post by:
Hi, I have an asp.net site that receives a medium amount of traffic (500-1k users per day). I'm having massive trouble at the moment with a memory leak that I just cannot isolate. The server is...
2
by: Robert | last post by:
Hello javascript group readers, I have a question regarding how to prevent memory leaks in Internet Explorer when using closures. I already knew about the circular reference problem, and until...
12
by: Simon Berube | last post by:
Hi, I have a rather simple question for which I couldn't find an answer. I noticed how a significant number of objects in Python return a __repr__() string along the lines of : < Object at...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
4
by: FFMG | last post by:
Hi, My Hosting company is saying that I am using too much memory on the VPS and that I should upgrade. I am currently running at 65%-75%, (according to WHM). It does seem a bit high, but I...
3
by: Mad Hatter | last post by:
Hi folks I have a script which reads the mysql database,does a lot of string manipulation then writes its output to a file. The manipulation is all done in a separate function which is called...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
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...

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.