By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,837 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,837 IT Pros & Developers. It's quick & easy.

Problem in accessing a 'memory address'

P: n/a
hi,

i need to store a value to a particular memory location without having
a variable.

So, how can i access a 'memory address' without using variables??

Is it possible in C???

Plz, help me..

Thanks in advance
Sethu

Nov 8 '06 #1
Share this Question
Share on Google+
20 Replies


P: n/a
se*****@gmail.com wrote:
i need to store a value to a particular memory location without having
a variable.
Why?

If you mean that you want to write to an address in your machine that
you only have the numerical form of, then you can't portably do
that in standard C; you'll have to use an implementation extension.
So, how can i access a 'memory address' without using variables??
That depends on what you mean by a "variable". (No, I'm being serious.
Do you count an expression like `*p` a variable?)
Is it possible in C???
Can't tell from your description.

--
Chris ".enable proofreading" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Nov 8 '06 #2

P: n/a
se*****@gmail.com said:
hi,

i need to store a value to a particular memory location without having
a variable.

So, how can i access a 'memory address' without using variables??

Is it possible in C???
That isn't really the question. The questions are:

1) is it possible on all your target platforms?
2) would this value have the same semantics on all those platforms?

If the answer to either of those questions is "no", C can't help you wave a
magic wand to make it so.

If you're using a protected-mode operating system such as Windows or Unix,
it's generally not possible (unless you're way down in the system, or
unless you're using a real-mode emulator of some kind).

If you're using a real-mode system such as MS-DOS or CP/M, it may be
possible.

As for semantics, that's unlikely to be portable across all your target
platforms unless they are extremely similar or unless you only have one
target platform.

Given all that, you can do this in C (but the outcome is not defined by the
C language - C gives you the power to do things like this, but on your own
head be it!):

*(unsigned char *)0xB8000000UL = 65;

On an appropriate system, this writes the letter 'A' on the screen in the
upper left hand corner. On a not-so-appropriate system, it may well crash
your machine. Caveat programmer!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 8 '06 #3

P: n/a
In article <11**********************@f16g2000cwb.googlegroups .com>,
<se*****@gmail.comwrote:
>i need to store a value to a particular memory location without having
a variable.
That sounds like a class assignment.
>So, how can i access a 'memory address' without using variables??
>Is it possible in C???
I'm not sure if you are asking about pointers, or if you are asking
how to store a value at a particular address location without having
to go through a lot of steps to create a variable that the linker will
happen to put at the desired location ?
--
Programming is what happens while you're busy making other plans.
Nov 8 '06 #4

P: n/a

Walter Roberson wrote:
In article <11**********************@f16g2000cwb.googlegroups .com>,
<se*****@gmail.comwrote:
i need to store a value to a particular memory location without having
a variable.

That sounds like a class assignment.
So, how can i access a 'memory address' without using variables??
Is it possible in C???

I'm not sure if you are asking about pointers, or if you are asking
how to store a value at a particular address location without having
to go through a lot of steps to create a variable that the linker will
happen to put at the desired location ?
no, the pointer itself pointing a memory location.

My question is,

char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.

but, Is it possible to assign an memory address in the C program
itself?? by the user itself??

Thanks and regards,
Sethu

Nov 8 '06 #5

P: n/a
My question is,
>
char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.
>
but, Is it possible to assign an memory address in the C program
itself?? by the user itself??

Thanks and regards,
Sethu
Still not clear.

Did you want to store some variable at Specific-(may be) Hardcoded
memory location. ???

--raxit sheth

Nov 8 '06 #6

P: n/a
se*****@gmail.com wrote:
Walter Roberson wrote:
>In article <11**********************@f16g2000cwb.googlegroups .com>,
<se*****@gmail.comwrote:
>i need to store a value to a particular memory location without having
a variable.

That sounds like a class assignment.
>So, how can i access a 'memory address' without using variables??
>Is it possible in C???

I'm not sure if you are asking about pointers, or if you are asking
how to store a value at a particular address location without having
to go through a lot of steps to create a variable that the linker will
happen to put at the desired location ?

no, the pointer itself pointing a memory location.
Unclear.
My question is,

char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.
(Depending what you mean by "dynamically" - no; some variables are
allocated addresses statically, before the program is run, in at
least some [1] implementations.)
but, Is it possible to assign an memory address in the C program
itself?? by the user itself??
This is a /different/ (related) question. Fortunately, it has a
simple answer: No.

Standard C provides no way to bind a variable to a specific
memory address.

[1] Probably "most", but my neck isn't stretchy today.

--
Chris ".enable proofreading" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Nov 8 '06 #7

P: n/a

ra************@yahoo.co.in wrote:
My question is,

char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.

but, Is it possible to assign an memory address in the C program
itself?? by the user itself??

Thanks and regards,
Sethu

Still not clear.

Did you want to store some variable at Specific-(may be) Hardcoded
memory location. ???

--raxit sheth

absolutely. yes, i want to store some variable at a Hardcoded
memory location. ???.. is it possible??

Nov 8 '06 #8

P: n/a

ra************@yahoo.co.in wrote:
My question is,

char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.

but, Is it possible to assign an memory address in the C program
itself?? by the user itself??

Thanks and regards,
Sethu

Still not clear.

Did you want to store some variable at Specific-(may be) Hardcoded
memory location. ???

--raxit sheth

absolutely. yes, i want to store some variable at a Hardcoded
memory location. ???.. is it possible in C or C++??

Nov 8 '06 #9

P: n/a

ra************@yahoo.co.in wrote:
My question is,

char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.

but, Is it possible to assign an memory address in the C program
itself?? by the user itself??

Thanks and regards,
Sethu

Still not clear.

Did you want to store some variable at Specific-(may be) Hardcoded
memory location. ???

--raxit sheth

absolutely. yes, i want to store some variable at a Hardcoded
memory location. Is it possible in C or C++??

Nov 8 '06 #10

P: n/a
absolutely. yes, i want to store some variable at a Hardcoded
memory location. ???.. is it possible in C or C++??
It is not a standard in C.
But yes, it is possible. Your question should have been on a "embedded"
news group.

You can use "#pragma section" directive to tell compiler to store
variables at a specific address (memory section). Its support is
compiler and/or platform dependent.
Your compiler manual should provide more details.

Nov 8 '06 #11

P: n/a
Nagaraj L said:
You can use "#pragma section" directive to tell compiler to store
variables at a specific address (memory section). Its support is
compiler and/or platform dependent.
Your compiler manual should provide more details.
It is under no such obligation. But it *may* provide more details!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 8 '06 #12

P: n/a
In article <11**********************@i42g2000cwa.googlegroups .com>,
Nagaraj L <na******@gmail.comwrote:
>absolutely. yes, i want to store some variable at a Hardcoded
memory location. ???.. is it possible in C or C++??
>It is not a standard in C.
But yes, it is possible. Your question should have been on a "embedded"
news group.
It is possible on *some* implementations.

The compiler is allowed to support converting an integral value into
a pointer; the C standard does not define what the result is, and
the C standard does not promise that such a pointer is usable. But
the standard specifically allows implementations to define a meaning
(and appropriate restrictions) for it.

On most systems, if converting an integral value to a pointer is
supported at all, then the address pointed to would be in virtual
memory, not in physical memory. If virtual memory is in use and
it is absolute physical memory that must be addressed, then system-
specific mechanisms must be used (and it might not be possible without
resorting to linking in an assembler routine).
>You can use "#pragma section" directive to tell compiler to store
variables at a specific address (memory section). Its support is
compiler and/or platform dependent.
Your compiler manual should provide more details.
All #pragma are implementation defined, so #pragma section may simply
be completely ignored on implementations (not uncommon).

A section isn't a specific memory address: a section is a memory block,
and variables would get added on to the end of the block; the linker might
provide a mechanism to locate the section at a particular virtual
address... or it might not.

You should thus not say that 'You can use "#pragma section"': at best
you should say, 'You -might- be able to use "#pragma section",
together with other implementation-dependant instructions to the compiler
or linker, but this is not portable and you would need to check
your documentation for details'.

On the whole, converting an integral value to a pointer is usually
a lot easier than #pragma section, on any implementation that happens
to make the conversion to pointer meaningful and predictable.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 8 '06 #13

P: n/a
se*****@gmail.com wrote:
absolutely. yes, i want to store some variable at a Hardcoded
memory location.
Why?

--
Chris ".enable proofreading" Dollin
The "good old days" used to be much better.

Nov 8 '06 #14

P: n/a
On 8 Nov 2006 01:41:21 -0800, se*****@gmail.com wrote:
>So, how can i access a 'memory address' without using variables??
The following code compiles with gcc-mingw:

#include <stdio.h>
#include <stdlib.h>

int main()
{ int i; int* p;
p = (int*)(100);
i = *p;
printf("\nContent of address 100 is %d",i);
return(i);
}

However running the exe in XP gives a memory access exception, of
course.

Andreas
-------
1 + 1 = 3, for large values of 1.
Nov 8 '06 #15

P: n/a
2006-11-08 <11**********************@h54g2000cwb.googlegroups .com>,
se*****@gmail.com wrote:
>
Walter Roberson wrote:
>In article <11**********************@f16g2000cwb.googlegroups .com>,
<se*****@gmail.comwrote:
>i need to store a value to a particular memory location without having
a variable.

That sounds like a class assignment.
>So, how can i access a 'memory address' without using variables??
>Is it possible in C???

I'm not sure if you are asking about pointers, or if you are asking
how to store a value at a particular address location without having
to go through a lot of steps to create a variable that the linker will
happen to put at the desired location ?

no, the pointer itself pointing a memory location.

My question is,

char c;

when the compiler reads the above instruction, it will allocates a
memory location dynamically.

but, Is it possible to assign an memory address in the C program
itself?? by the user itself??
On systems where this makes sense for user code[1], you can typically
assign some sort of integer expression to a pointer with an appropriate
cast, and use that pointer to access the memory at that address.

[1]such systems are becoming fewer with each passing year.
Nov 8 '06 #16

P: n/a
sethukr :
So, how can i access a 'memory address' without using variables??

*(int*)6892 = 5;

--

Frederick Gotham
Nov 8 '06 #17

P: n/a

se*****@gmail.com wrote:
hi,

i need to store a value to a particular memory location without having
a variable.
Not a C thing per se, but here goes,

That's only going to work on a dwindling set of systems, that have
interesting things at fixed addresses, and let you peek or poke
anywhere in the address range.

About all that's left in that area are MSDOS, in real mode. or the
original Macintoshes, running system 7 or before.

On those you can often jam an absolte address into a pointer variable
and somethimes get the desired result. For instance, in MSDOS there
might be the number of ticks since bootup at address $0040:$006C. You
can access it with:

unsigned long int * TickPtr; unsigned long int Ticks;

TickPtr = Ptr( 0x0040, 0x006C );

Ticks = * TickPtr;

Note that any access like this is discouraged, and you're likely to get
unstable results. Also the code is not portable to any other system.
Every other system I can think of has memory protection and API's for
accessing system info.

Nov 8 '06 #18

P: n/a
Frederick Gotham wrote:
sethukr :
>So, how can i access a 'memory address' without using variables??

*(int*)6892 = 5;
That's allowed to not work. The OP has two choices: (a) abandon hope,
because what they want isn't portably allowed by standard C; (b)
abandon portability, for the same reason; (c) explain /why/ they
want to do this, in case there's a better approach that's either
portable or has an obvious place to ask about; or (d) meditate
on Monty Python [1].

[1] Not Monty Hall, which is probably a different issue, or Python,
which is a different fang entirely.

--
Chris "comfy chair" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Nov 9 '06 #19

P: n/a
Chris Dollin <ch**********@hp.comwrote:
Frederick Gotham wrote:
sethukr :
So, how can i access a 'memory address' without using variables??
*(int*)6892 = 5;

That's allowed to not work. The OP has two choices: (a) abandon hope,
because what they want isn't portably allowed by standard C; (b)
abandon portability, for the same reason; (c) explain /why/ they
want to do this, in case there's a better approach that's either
portable or has an obvious place to ask about; or (d) meditate
on Monty Python [1].
Or (e) use the indicated solution, which is as portable as any solution
to this problem is, because it may not be guaranteed that it works, it
_is_ at least normal ISO C. _If_ any implementation allows you to change
a direct memory address, I'd expect it to allow you to do it like this.

Richard
Nov 9 '06 #20

P: n/a
Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:
>Frederick Gotham wrote:
sethukr :

So, how can i access a 'memory address' without using variables??

*(int*)6892 = 5;

That's allowed to not work. The OP has two choices: (a) abandon hope,
because what they want isn't portably allowed by standard C; (b)
abandon portability, for the same reason; (c) explain /why/ they
want to do this, in case there's a better approach that's either
portable or has an obvious place to ask about; or (d) meditate
on Monty Python [1].

Or (e) use the indicated solution, which is as portable as any solution
to this problem is, because it may not be guaranteed that it works, it
_is_ at least normal ISO C. _If_ any implementation allows you to change
a direct memory address, I'd expect it to allow you to do it like this.
Which I'd subsume under (b).

I think (c) is the dominant choice, actually. (a) or (b) may follow,
but none of us actually knows why the OP wants to do this thing.

--
Chris ".enable proofreading" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Nov 9 '06 #21

This discussion thread is closed

Replies have been disabled for this discussion.