How to Store a Value at a Specific Location in the Memory

How to store a variable at a specific memory location?

In your IDE there will be a memory map available through some linker file. It will contain all addresses in the program. Read the MCU manual to see at which addresses there is valid memory for your purpose, then reserve some of that memory for your variable. You have to read the documentation of your specific development platform.

Next, please note that it doesn't make much sense to map variables at specific addresses unless they are either hardware registers or non-volatile variables residing in flash or EEPROM.

If the contents of such a memory location will change during execution, because it is a register, or because your program contains a bootloader/NVM programming algorithm changing NVM memory cells, then the variables must be declared as volatile. Otherwise the compiler will break your code completely upon optimization.

The particular compiler most likely has a non-standard way to allocate variables at specific addresses, such as a #pragma or sometimes the weird, non-standard @ operator. The only sensible way you can allocate a variable at a fixed location in standard C, is this:

#define MY_REGISTER (*(volatile uint8_t*)0x12345678u)

where 0x12345678 is the address where 1 byte of that is located. Once you have a macro declaration like this, you can use it as if it was a variable:

void func (void)
{
MY_REGISTER = 1; // write
int var = MY_REGISTER; // read
}

Most often you want these kind of variables to reside in the global namespace, hence the macro. But if you for some reason want the scope of the variable to be reduced, then skip the macro and access the address manually inside the code:

void func (void)
{
*(volatile uint8_t*)0x12345678u = 1; // write
int var = *(volatile uint8_t*)0x12345678u; // read
}

How to store a variable or object on desired memory location?

You wrote

 class newarray 
{
int arr[]; //size not declared

This is not allowed. Unfortunately, your compiler did not warn you when compilinhg. You only discovered that this was a problem when your code crashed.

You then wonder about "some allocated memory location" and picking one manually. That's not the problem. int arr[] without explicitly specifying bounds is allowed only in a few contexts, such as a function declaration. The function declaration doesn't need a size because the size in that case is determined by the caller.

Since this is C++, just use std::vector<int> for your dynamic arrays.

How to store values with same memory location in c?

In order to keep the content of each loaded field returned from strtok(), just add a strdup() before calling insertIntoList() after checking if not a null-pointer.

In your code, if you compare the value of both line and targetNum
are the same. If fact, the strtok() function returns a pointer to
the input string and keep the pointer for the next argument.

Replace the following code:

    char *targetNum = strtok(line, " \t\r\n");
printf("*****%s\n", targetNum);
insertIntoList(&head, targetNum);

By that one:

    char *targetNum = strtok(line, " \t\r\n");
if (targetNum != NULL) {
printf("*****%s\n", targetNum);
insertIntoList(&head, strdup(targetNum));
}

Assigning a variable to a specific memory location

In common general-purpose computers, you cannot assign addresses for objects. The addresses are managed for you by other software, and trying to set your own address for an object may interfere with other uses of that memory, may result in a trap if the memory is not mapped in the address space of your process, and may have other adverse consequences.

Assuming this code is for a hypothetical computer in a classroom study, and the computer and the C implementation have typical properties, such as eight-bit char (and that char is unsigned), then:

  • char *C,A=0,B=0x20 defines C to be a pointer to char. (This line should have a semicolon to mark its end.)
  • C=0x55 attempts to set C to point to the address 0x55. This is not the correct way to do this because C is a pointer, meaning its values are addresses, and 0x55 is a plain integer, not an address. The compiler should warn about this statement. A correct way to do this is to use a cast to convert the integer to a pointer: C = (char *) 0x55;.
  • After this, the loop starting with while(B) will execute as long as B is non-zero. Inside the loop, B=B+ *C; adds the contents of the memory C points to to B. Since you tell us the memory in that area is filled with 0x20, it adds 0x20 to B.
  • If we assume the computer and the C implementation have typical properties, this will continue with B being assigned the value 0x40, 0x60, 0x80, 0xa0, 0xc0, and 0xe0. Then the next addition will produce 0x100. 0x100 exceeds the eight bits in the char B, and it will wrap, putting 0x00 in B.
  • After B is set to 0x00, the while(B) loop will end.
  • So the loop will execute 7 times (once to set B to 0x40, then to 0x60, and so on, until it sets B to 0x00).
  • In each iteration, A is incremented by one. Since A starts at 0 and is incremented 7 times, its final value is 7.

Note: The C++; statement changes the value of C, changing where it points. In general, this would affect what value is added in the statement B=B+ *C;. However, since each byte in that area is filled with the same value, there is no effect; *C is always 0x20 in this situation. If the bytes of memory had different values, then changing C with C++; would change what value is obtained for *C.

How to store value into some specific memory address using c programming

This address space does not belong to this process

Blockquote
The term "segmentation" has various uses in computing; in the context of "segmentation fault", a term used since the 1950s, it refers to the address space of a program.[citation needed] With memory protection, only the program's address space is readable, and of this, only the stack and the read-write portion of the data segment of a program are writable, while read-only data and the code segment are not writable. Thus attempting to read outside of the program's address space, or writing to a read-only segment of the address space, results in a segmentation fault, hence the name.
Blockquote

http://en.wikipedia.org/wiki/Segmentation_fault

Can we assign a value to a given memory location?

The answer depends on some factors. Is your program running on an operating system? If yes, does the OS implement memory segmentation?

If you answered yes to both questions, trying to access a memory area that is not mapped or that it doesn't have permission to write will cause a memory access violation (SIGSEGV on POSIX based systems). To accomplish that, you have to use a system specific function to map the region of memory that contains this exact address before trying to access it.

How do I store the value of a register into a memory location pointed to by a pointer?

Are you sure you know what you really need? You requested the code that would store the register value into the memory allocated by malloc ("pointed to by a pointer"), i.e. *(int*) storage location, yet you accepted the answer that stores (or at least attempts to store) the value into the pointer itself, which is a completely different thing.

To store eax into the memory "pointed to by a pointer", i.e. into *(int*) storage as you requested, you'd have to do something like that

mov  edi, dword ptr storage
mov dword ptr [edi], eax

(I use the "Intel" right-to-left syntax for assembly instructions, i.e. mov copies from right operand to left operand. I don't know which syntax - right-to-left or left-to-right - your compiler is using.)

Note also that in mov edi, dword ptr storage the dword ptr part is completely optional and makes no difference whatsoever.



Related Topics



Leave a reply



Submit