Hi,
I have a program that writes to a specific memory location. I can currently read the value doing this:

#define address1 0x000F0000 //this is the memory location
unsigned long *val1 = (void *) address1;

So * val1 contains what I need from the other program.
Now I want to organize these values in a struct, something like this:

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
} pt;

struct my_struct *org;
org = &pt;

But somehow, I can't assign the value in address1 to val1.
e.g.

org->val1 = (void *) address1; //or something like that.

Any ideas what is it that I'm missing?


Many thanks,

ibug

Hi,
I have a program that writes to a specific memory location. I can currently read the value doing this:

#define address1 0x000F0000 //this is the memory location
unsigned long *val1 = (void *) address1;

So * val1 contains what I need from the other program.
Now I want to organize these values in a struct, something like this:

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
} pt;

struct my_struct *org;
org = &pt;

But somehow, I can't assign the value in address1 to val1.
e.g.

org->val1 = (void *) address1; //or something like that.

Any ideas what is it that I'm missing?


Many thanks,

ibug

You can't just assign memory in a user process, you have to allocate it first.

Plus this line

So * val1 contains what I need from the other program.

Are you creating a program that shares memory with another program?

>>Any ideas what is it that I'm missing?

Yes -- if you are using MS-Windows or *nix operating systems than that will not work because your program can not change the memory values of memory it does not own. You can't just plug some random memory location into your program.

You can't just assign memory in a user process, you have to allocate it first.

Plus this line

So * val1 contains what I need from the other program.

Are you creating a program that shares memory with another program?

The memory location is already allocated by the "other" program. It is not shared in that I don't write to it. It is shared in that programA writes and programB (mine) reads from this location.

But why do I need to allocate memory only when I use the STRUCT?
This line:

unsigned long *val1 = (void *) address1;

is perfectly capable of reading the shared memory location.

The memory location is already allocated by the "other" program. It is not shared in that I don't write to it. It is shared in that programA writes and programB (mine) reads from this location.

But why do I need to allocate memory only when I use the STRUCT?
This line:

unsigned long *val1 = (void *) address1;

is perfectly capable of reading the shared memory location.

Unless your using some form of memory sharing then user processes can't shared data...Well I'm assuming a modern operating system with virtual memory..

>>Any ideas what is it that I'm missing?

Yes -- if you are using MS-Windows or *nix operating systems than that will not work because your program can not change the memory values of memory it does not own. You can't just plug some random memory location into your program.

I'm using neither. And I did mention that assigning an integer pointer to the memory location does work. So the question is not whether this is possible or not (it is possible) but what is it that I'm missing regarding the STRUCT.

Thanks.

I'm using neither. And I did mention that assigning an integer pointer to the memory location does work. So the question is not whether this is possible or not (it is possible) but what is it that I'm missing regarding the STRUCT.

Thanks.

Just curious, what are you developing this on?

For your struct:

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

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
};

int main(int argc, char**argv)
{

	struct my_struct *org = (struct my_struct*)malloc(sizeof(struct my_struct));
	org->val1 = (unsigned long*)NULL;//assign your values here
	org->val2 = (unsigned long*)NULL;//assign your values here
	exit(EXIT_SUCCESS);
}

Do you want the struct elements to be pointers? Or do you really mean for them to be back-to-back long integers packaged in a struct?

/**
 * 0x000F0000  +--------------+ ============#
 * 0x000F0001  |              |             #
 * 0x000F0002  |    value1    |             #
 * 0x000F0003  +--------------+   struct T  #
 * 0x000F0004  +--------------+             #
 * 0x000F0005  |              |             #
 * 0x000F0006  |    value2    |             #
 * 0x000F0007  +--------------+ ============#
 * 0x000F0008         .
 * 0x000F0009         .
 * 0x000F000A         .
 */
struct T
{
   unsigned long value1;
   unsigned long value2;
};

#define address1 0x000F0000
struct T *ptr = (struct T *)address1;

void foo(void)
{
   /* read the data via the absolutely-located struct */
   unsigned long v1 = ptr->value1;
   unsigned long v2 = ptr->value2;
   /* write the data via the absolutely-located struct */
   ptr->value1 = v1 + 1;
   ptr->value2 = v2 + 1;
}

Just curious, what are you developing this on?

Embedded System on an FPGA. I've written programA that writes to the memory location in VHDL. Now I need to read it in C and because I have lots of these registers, I thought I'd arrange them nicely in a STRUCT but as you've noticed, it wasn't that easy.

Thanks for your suggestion. I'll give it a try.

Do you want the struct elements to be pointers? Or do you really mean for them to be back-to-back long integers packaged in a struct?

Not sure really but I think the struct elements must be pointers.

Thanks for your post. I'll try that as well.

ibug.

Do you want the struct elements to be pointers? Or do you really mean for them to be back-to-back long integers packaged in a struct?

Not sure really but I think the struct elements must be pointers.

Thanks for your post. I'll try that as well.

ibug.

Not really, if the memory your interested in is continuous then Dave Sinkula's method is more practical..

struct my_struct *org = (struct my_struct*)malloc(sizeof(struct my_struct));

A silly question, but why do I need malloc here? I am only reading from this address.

The memory at the following 2 addresses are "written to" by programA (assume this is possible):

#define addressA 0x000A0000
#define addressB 0x000B0000

And I want the elements in my_struct to have pointers to these 2 addresses:

struct T
{
   unsigned long value1;
   unsigned long value2;
};

//something like this:
T->value1 = (unsigned long *) addressA;
T->value2 = (unsigned long *) addressB;

Is this not possible?

struct my_struct *org = (struct my_struct*)malloc(sizeof(struct my_struct));

A silly question, but why do I need malloc here? I am only reading from this address.

This was before I realized what your specs were

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

#define SOMEADDRESS 0x12345678

struct my_struct{
	unsigned long *val1;
	unsigned long *val2;
};

int main(int argc, char**argv)
{

	struct my_struct *org = (struct my_struct*)SOMEADDRESS;
	org->val1 = /*assign your values here*/
	org->val2 = /*assign your values here*/
	exit(EXIT_SUCCESS);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.