0

I have a struct defined like this:

typedef struct foo {
    void *bar;
    int memory;
}

I'm trying to assign a value to the void-pointer like so:

int *mem = (int *)proc->memory;
proc->bar = mem;

However, the second line gives a segmentation fault. Why? I thought I knew how to use pointers but I don't.

after the assignment the mem pointer contains the address of proc->memory. But then when I try to assign the void *bar to that address, it says segmentationfault.

I've tried numerous stuff, introducing ampersands and pointercasts wherever, I have no clue what I'm doing.

yesyes
  • 1
  • 3
    Show how you allocate `proc` (I think you just don't) – Alex Lop. Apr 29 '19 at 17:56
  • I don't indeed. I'm working with a framework for an assignment. The struct is initialized for me. – yesyes Apr 29 '19 at 18:16
  • The only plausible reason for the statement `proc->bar = mem;` to cause a segmentation fault is if `proc` is not a valid pointer. You seem to have confirmed that that is indeed the case, so I'm not seeing what else there is to say. – John Bollinger Apr 29 '19 at 18:31
  • I personally didn't allocate any struct. But the assignment has a framework in which it is allocated (at least I hope so, the framework is too big to read through every line). However if proc isn't a valid pointer than proc->memory also will give a segmentation fault, or not? Because I can assign proc-> memory to an int value just fine. – yesyes Apr 29 '19 at 18:37

1 Answers1

0

If you want a pointer you need to take the address of the int.

int *mem = &(proc->memory);

you were creating a pointer to whatever the value of int was -- this probably pointed to protected memory not something inside our process space.


If you want to "make a copy" then you need to allocate space

int *mem = malloc(sizeof(int));

*mem = proc->memory;

This should all be covered in a simple introduction to pointers in C.

eg http://www.ontko.com/pub/rayo/cs35/pointers.html

You can also google search for malloc or dynamic memory allocation in C.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • I have tried this before and it didn't work. It tells me the pointer is invalid. All I want to do is to put the value of memory in the void *bar so I can adjust it there as needed. – yesyes Apr 29 '19 at 18:18
  • In this case, this `proc->bar = mem` wouldn't cause a segfault. here is no dereferencing, just assignment. – Alex Lop. Apr 29 '19 at 18:25
  • @yesyes what do you mean by "put"? You want to make a copy? – Hogan Apr 29 '19 at 18:26
  • Well, proc->bar itself gives a segmentationfault. If I try to do proc->userdata = 10; for instance, it also gives me a segmentationfault. If I comment out the line of code, it doesn't and the code runs (given that I commented out all the other blocks of code that use the value). – yesyes Apr 29 '19 at 18:28
  • @Hogan I want to initially have the exact same value as proc->memory in the void *bar. After which I will adjust the value of it. – yesyes Apr 29 '19 at 18:29
  • @yesyes this you need to allocate memory first before you put a value there. – Hogan Apr 29 '19 at 18:30
  • @Hogan to make it clear for myself: I first allocate memory with malloc(sizeof(int)), malloc returns a pointer to the address of the malloced memory. Then with *mem = proc->memory; I set the value of the address mem is pointing to equal to proc->memory? – yesyes Apr 29 '19 at 18:52
  • @yesyes, no -- *mem = proc->memory; sets the value stored in that location in memory to the value stored in proc->memory – Hogan Apr 29 '19 at 19:04