i am having a problem while populating the structure members with address reference but when it is done using the member it self then its fine.
with structure memmber
memcpy(&(AVPFieldStructureObj->resource_value),data_start,actual_data_length);
With Memory Address
memcpy((&AVPFieldStructureObj+fieldOffset),data_start,actual_data_length);
where actual_data_length is the size of varibale and data_start is pointer pointing to the data buffer.
with memory its giving garbage value when i print the field and after executing the whole i am getting a segmentation fault but when debug with GDB the program exited normally.there was no segmentation fault
please suggest
Thanks in advance
Regards,
Soheb
I believe fieldOffset is the offset in bytes?
If you're doing something like:
Type* pointer = somewhere;
pointer += 3;
then pointer gets moved forward by 3*sizeof(Type) bytes - so 3 Type objects forward.
So in your code, in the latter variant, you're not offsetting the address by fieldOffset bytes, but by fieldOffset*sizeof(AVPFieldStructureObj) bytes.
You can work around that by casting to the pointer to char* temporarily.
Related
I have a doubt in dynamic memory allocation (malloc)
Say
ivar=(int*)malloc(1*sizeof(int));
What the above code will do?
Will it create address for allocations?
Also which is the standard way to get values in malloc? (Say i as loop variable)
scanf("%d",&ivar[i]);
OR
scanf("%d",ivar+i);
int main()
{
int *ivar;
ivar=(int*)malloc(1*sizeof(int));
printf("%u",ivar); // outputs 2510
printf("%u",&ivar);// outputs 65524
} // please explain why it is…
Thanks in advance.
The memory allocated using malloc is created in heap section of RAM.
ivar=(int*)malloc(1*sizeof(int));
The syntax for malloc is
void *malloc(size_t size);
(1*sizeof(int)) gives 4 bytes, so 4 bytes is allocated in Heap.
You cannot directly access memory of heap, so ivar pointer is used to access it.
When you write
printf("%p",ivar); // outputs 2510
printf("%p",&ivar);// outputs 65524
Both of these gives address, first one gives address of the location pointer is pointing at, second one gives address of the pointer
scanf("%d",&ivar[i]);
and
scanf("%d",ivar+i);
both are equal, so you can use either one of them.
I'm getting the following output from gdb:
(gdb) print tid->rvm
$28 = (rvm_t) 0x605010
(gdb) step
306 rlog->entries[i].sizes[num - 1] = size;
(gdb) step
307 }
(gdb) print tid->rvm
$29 = (rvm_t) 0x64
(gdb) print tid
$30 = (trans_t) 0x607b50
This is the code for the tid structure:
struct _trans_t {
rvm_t rvm;
int numsegs;
segment_t* segments;
};
The rvm_t rvm is a struct containing metadata for a recoverable virtual memory pager.
I don't believe I am allowed to post the full function code where this is happening, because this is a university assignment, but you can see the single line of code where this change occurs, rlog->entries[i].sizes[num - 1] = size;. There is no reference to the value that get changed. I also have to use this ugly typedefing pointers to structs thing. The assignment submission server contains the header file as-is and I can't edit it.
My question is what the heck can cause this. tid is passed as a parameter to a function rvm_about_to_modify(trans_t tid, void *segbase, int offset, int size). The struct member rvm is never touched inside this function and you can see the step sequence where it has address 0x605010, which is the correct address, and one line later, without any reference
to either the tid struct or its rvm member, the pointer changes to address 0x64. The actual tid pointer has not changed. It keeps address 0x607b50 throughout.
I'm complete at a loss here and can't figure out what would cause tid->rvm to change value without ever being touched. From what I can tell, 0x64 is the address of the interrupt vector table entry for the keyboard status register. Any help is appreciated.
Edit:
Here's some requested updates. The value of i is 0, the value of num is 1, the value of rlog is 0x607b30. rlog->entries[0] is an entry for changes to a data segment with name "testseg", size 10000 bytes, updatesize 300 bytes, numupdates 1, and pointers to arrays of offsets, sizes, and data. rlog->entries[0].sizes[0] is 100. The data pointer is 0x6051f0.
Since you cannot post the full code, we cannot explain in any detail. However, your assertion that "tid->rvm [changes] value without ever being touched" is self-contradictory. What you can say is that structure member tid->rvm is not changed via structure pointer tid, by the thread being debugged.
Supposing that the structure does not live in shared memory and that the process is single-threaded, the only possible conclusion is that tid->rvm
is fully or partially aliased by rlog->entries[i].sizes[num - 1]. That is, the memory to which the left-hand side of the second expression refers overlaps that to which the first refers. There are several ways that could happen, among them:
the tid and/or rlog pointer points to the wrong place
the current value of i is outside the bounds of array rlog->entries
the expression num - 1 is outside the bounds of array rlog->entries[i].sizes
Those are by no means the only possibilities, but they are the most likely ones among those that can be guessed from the limited amount of code presented.
Thanks for the help. valgrind helped. Turns out I wasn't initializing some of the pointers in the rlog->entries elements, so when I wrote to them later, it spilled into addresses allocated previously to tid->rvm and overwrote it. Still not sure why an invalidated pointer is redirected to the keyboard status register.
i am just started learning pointers in c. I have following few doubts. If i find the answers for the below questions. It Will be really useful for me to understand the concept of pointers in c. Thanks in advance.
i)
char *cptr;
int value = 2345;
cptr = (char *)value;
whats the use of (char *) and what it mean in the above code snippet.
ii)
char *cptr;
int value = 2345;
cptr = value;
This also compiles without any error .then whats the difference between i & ii code snippet
iii) &value is returning address of the variable. Is it a virtual memory address in RAM? Suppose another c program running in parallel, will that program can have same memory address as &value. Will each process can have duplicate memory address same as in other process and it is independent of each other?
iv)
#define MY_REGISTER (*(volatile unsigned char*)0x1234)
void main()
{
MY_REGISTER=12;
printf("value in the address tamil is %d",(MY_REGISTER));
}
The above snippet compiled successfully. But it outputs segmentation fault error. I don't know what's the mistake I am doing. I want to know how to access the value of random address, using pointers. Is there any way? Will program have the address 0x1234 for real?
v) printf("value at the address %d",*(236632));//consider the address 236632 available in
//stack
why does the above printf statement showing error?
That's a type cast, it tells the compiler to treat one type as some other (possibly unrelated) type. As for the result, see point 2 below.
That makes cptr point to the address 2345.
Modern operating systems isolate the processes. The address of one variable in one process is not valid in another process, even if started with the same program. In fact, the second process may have a completely different memory map due to Address Space Layout Randomisation (ASLR).
It's because you try to write to address 0x1234 which might be a valid address on some systems, but not on most, and almost never on a PC running e.g. Windows or Linux.
i)
(char *) means, that you cast the data stored in value to a pointer ptr, which points to a char. Which means, that ptr points to the memory location 2345. In your code snipet ptr is undefined though. I guess there is more in that program.
ii)
The difference is, that you now write to cptr, which is (as you defined) a pointer pointing to a char. There is not much of a difference as in i) except, that you write to a different variable, and that you use a implicit cast, which gets resolved by the compiler. Again, cptr points now to the location 2345 and expects there to be a char
iii)
Yes you can say it is a virtual address. Also segmentation plays some parts in this game, but at your stage you don't need to worry about it at all. The OS will resolve that for you and makes sure, that you only overwrite variables in the memory space dedicated to your program. So if you run a program twice at the same time, and you print a pointer, it is most likely the same value, but they won't point at the same value in memory.
iv)
Didn't see the write instruction at first. You can't just write anywhere into memory, as you could overwrite another program's value.
v)
Similar issue as above. You cannot just dereference any number you want to, you first need to cast it to a pointer, otherwise neither the compiler, your OS nor your CPU will have a clue, to what exactely it is pointing to
Hope I could help you, but I recommend, that you dive again in some books about pointers in C.
i.) Type cast, you cast the integer to a char
ii.) You point to the address of 2345.
iii.) Refer to answer from Joachim Pileborg. ^ ASLR
iv.) You can't directly write into an address without knowing if there's already something in / if it even exists.
v.) Because you're actually using a pointer to print a normal integer out, which should throw the error C2100: illegal indirection.
You may think pointers like numbers on mailboxes. When you set a value to a pointer, e.g cptr = 2345 is like you move in front of mailbox 2345. That's ok, no actual interaction with the memory, hence no crash. When you state something like *cptr, this refers to the actual "content of the mailbox". Setting a value for *cptr is like trying to put something in the mailbox in front of you (memory location). If you don't know who it belongs to (how the application uses that memory), it's probably a bad idea. You could use "malloc" to initialize a pointer / allocate memory, and "free" to cleanup after you finish the job.
How would i free the memory that is leaking fromt he following code.
struct object_one{
int value;
}*object,object_node;
struct node_one {
void **pointers;
}*node, node_node;
node sample(){
object number;
node node123;
node123 = malloc(sizeof(node_node));
number = malloc(sizeof(object_node));
number->valu = malloc(sizeof(int));
number->value = 9;
node123->pointers[0]=number;
free(number);
return node123;
}
Am I doing the correct way to deallocate the memory referenced by number.Once i do the above one, i get the error;
Invalid read of size 4
==15957== at 0x403804: main (abc.c:1255)
==15957== Address 0x540cb50 is 0 bytes inside a block of size 4 free'd
Please suggest me how can I prevent the memory leak in this condition? Thanks in advance.
[EDIT]
Hi actually the above mentioned is not the real code I have. But I have tried my best to refelect the sturcutre and semantics of my code. Actually I am implementing a data strcture. number is some temproary storage and node123 is my real data-base. I would like to assign the value of number in the database and dereference it.number type is a component of database.
[EDIT2]
The code corresponding to line 1255 is printing for the value that is holded by the object i'e value.It looks like:
object tempObject;
tempObject = search_object(root,50);
[1255] printf("Key is %d ------>value is %d\n",50,tempObject->value);
Here the functionsearch_object searches the value corresponding to the key 50. The function is returning the proper value associated with the key and still it is showing such error.
You also need to free(node123).
But this is not what the error message is telling you.
You are also dereferencing a pointer that doesn't point at valid memory (on the line node123->pointers[0] = number). You have not allocated any memory for pointers to point at, so dereferencing it like this will write to a random area in memory.
You forgot to free(node123), it seems.
For each malloc() call you must have a free() call.
The valgrind error you show is not a memory leak, it's an invalid read in a memory region that has been free'd.
It happens in abc.c at line 1255.
You forgot a free(node123);
And also this line: node123->pointers[0]=number; is a trouble.
You didn't initialized pointers inside node123.
int **F;
int **dev_pF;
size_t *pitchF;
void init_cuda_mem(int mF,int mT,int nF,int nT){
cudaMallocPitch((void **)dev_pF,pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2);
cudaMemcpy2D((void *)dev_pF,*pitchF,(void *)pF,*pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2,cudaMemcpyHostToDevice);
}
Well hello everyone
in the above snippet i am trying to allocate a 2D array using cudaMallocPitch
and then copying that array using cudaMemcpy2D from the host to the device
unfortunately it crashes and i think the error is (i think) at the cudaMemcpy2D
can someone help me locate it please
I think the issue is that you are mistaken with regards to pointers and pointers to pointers.
You should probably do something on the lines of:
int *dev_pF;
size_t pitchF;
void init_cuda_mem(int mF,int mT,int nF,int nT) {
cudaMallocPitch((void **)&dev_pF, &pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2);
cudaMemcpy2D((void *)dev_pF,pitchF,(void *)pF, pitchF,(nF + 2*nT -2)*sizeof(int),mF + 2*mT -2,cudaMemcpyHostToDevice);
}
Note the difference that you are now taking the address of the variables in the cudaMallocPitch call and then just using them directly in the second call.
In your original code, you were first asking cudaMalloc to store a pointer in whatever memory dec_pF happened to point to and store the size in whatever memory pitchF was pointing to. Both of these where unitialized, so disaster could occur there. In the second call you were converting dev_pF from a pointer to pointer to a regular pointer, so you are telling the memcpy to copy memory starting where the pointer was stored rather than where the allocated memory was stored. And since both the pointer to pointer and the size where unitialized at first, pretty much anything could happen.
Also, you are making use of a pF pointer that I cannot see in the original code, make sure it is initialized properly.