is byte allocated

S

sinbad

hi,
Is there any way to know if a byte of memory is allocated.
Does C allows me to write such a function, or Is there any other way
for
doing this. btw i am using gcc in linux; Here, the byte may not be
allocated
to this process but might be used by another process, even in that
case
i should get the result as alloced; Conversely is there a way to tell
malloc ()
that i need memory to be allocated at this memory location if
possible?

int main ()
{
if (is_alloced(0x3434453E))
do_something;
else
do_something_else;
return 0;
}
 
K

Keith Thompson

sinbad said:
hi, Is there any way to know if a byte of memory is allocated. Does C
allows me to write such a function, or Is there any other way for
doing this. btw i am using gcc in linux; Here, the byte may not be
allocated to this process but might be used by another process, even
in that case i should get the result as alloced; Conversely is there a
way to tell malloc () that i need memory to be allocated at this
memory location if possible?

By "allocated", I presume you mean allocated by malloc (or calloc, or
realloc).

The answer to both your questions is no. At least, there's no
portable way to do what you're asking. (I don't know of any
non-portable way either.)
 
T

Tomás Ó hÉilidhe

By "allocated", I presume you mean allocated by malloc (or calloc, or
realloc).

The answer to both your questions is no.  At least, there's no
portable way to do what you're asking.  (I don't know of any
non-portable way either.)


One solution would be to create a wrapper for malloc that contains
info about allocations, maybe something like as follows:

#include <stdint.h>

typedef struct AllocInfo {
char unsigned const *pfirst;
char unsigned const *pend;
} AllocInfo;

AllocInfo allocs[50];

uint_fast8_t IsAddressAllocated(void const *const addr)
{
AllocInfo const *p = allocs;

while (p++->p_first) /* Array is null-terminated */
{
if ( (char unsigned const*)addr >= p->pfirst
&& (char unsigned const*)addr < p->pend
)
return 1;
}

return 0;
}

Actually I just realised that the behaviour of those comparisons is
undefined because they don't (necessarily) refer to the same object.
It could be rewritten to take only the starting address of the
allocation into account.

Anyway, then you'd wrap malloc:

#include <stddef.h>

void *mymalloc(size_t const val)
{
static AllocInfo const *p = allocs;

if (p == (allocs + (sizeof allocs / sizeof *allocs) -1) )
return 0;

p->pfirst = malloc(val);

if (!p->pfirst)
return 0;
else
{
p->pend = p->pfirst + val;

return (p++)->pfirst;
}
}

Then you could call "mymalloc" instead of "malloc", which can be
achieved quite easily with a "Find and Replace in Files", or perhaps
you might be bold enough to edit your "stdlib.h" file to:

#define malloc mymalloc

By the way, have you ever heard of mudflap? It's a memory access
debugging tool built into the GCC compiler. It commandeers the
compiler's memory access routines; for instance instead of having the
normal dereference operator, it calls a function that checks the
validity of the operation. It's pretty cool.
 
K

Kenny McCormack

Gordon Burditt said:
Unless you are dealing with shared memory segments, there is no way
to form a pointer that points at memory in another process on a
UNIX-like system. Each process has its own address space. Even

Not true. DOS is "Unix-like" and it is easy there for one process to
point at and access memory in another process.

In the above, the word "process" should be read to include "TSR"
processes.
 
S

sinbad

the os on which the process is running should be able to provide
such system call. since memory is managed by os, It should definitely
be able to tell if a byte is already in use or ready to use
(unalloced);

offtopic: Does linux provides anything of this sort;

thanks
 
F

Flash Gordon

sinbad wrote, On 22/12/08 03:53:
the os on which the process is running should be able to provide
such system call. since memory is managed by os,

Not on all Operating Systems and not all the time.
It should definitely
be able to tell if a byte is already in use or ready to use
(unalloced);

Just because it is freed by a program does not mean it is returned to
the OS. Just because it is allocated by the OS does not mean it is
available for normal use (it might be in use as part of a control
structure used by the C library for managing a heap).
offtopic: Does linux provides anything of this sort;

As you know it is off topic try asking where it is topical instead.
 
C

CBFalconer

Gordon said:
I don't agree with this at all. You don't have multiple processes
on MS-DOS. You don't have the possibility of multiple users.
There is no implementation of fork(). There are no user ids or
file ownership.


DOS doesn't support multiple processes.


A TSR is not a process, it's a function or several that gets a
callback. Some TSRs might qualify as device drivers.

I guess you weren't around in the old days, when there were
multiple methods of turning MSDOS into a multi-tasking system.
They usually included a TSR that detected all DOS calls and
inhibited interrupts. The quality of the result was a great
variable. Being too young is not a crime.

Since you have deleted all attributions, we have no idea who you
are agreeing or disagreeing with. You are also quoting them
without proper identification of the authors.
 
I

Ian Collins

CBFalconer said:
I guess you weren't around in the old days, when there were
multiple methods of turning MSDOS into a multi-tasking system.
They usually included a TSR that detected all DOS calls and
inhibited interrupts. The quality of the result was a great
variable. Being too young is not a crime.
Old days? The last multi-tasking implementation I wrote on DOS was in 1993!
Since you have deleted all attributions, we have no idea who you
are agreeing or disagreeing with. You are also quoting them
without proper identification of the authors.

He's an arrogant prat who thinks he's above Usenet conventions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top