pointer-arithmetic

Should this be called some special case of object slicing?

会有一股神秘感。 提交于 2019-12-02 23:46:22
Let's say I have a class Derived which derives from class Base whereas sizeof(Derived) > sizeof(Base) . Now, if one allocates an array of Derived like this: Base * myArray = new Derived[42]; and then attempts to access the n -th object using doSomethingWithBase(myArray[n]); Then this is might likely (but not always) cause undefined behaviour due to accessing Base from an invalid location. What is the correct term for such an programming error? Should it be considered a case of object slicing ? This is not object slicing. As noted, indexing myArray does not cause object slicing, but results in

Pointer arithmetics in C++ uses sizeof(type) incremention instead of byte incremention?

♀尐吖头ヾ 提交于 2019-12-02 17:11:18
问题 I am confused by the behavior of pointer arithmetics in C++. I have an array and I want to go N elements forward from the current one. Since in C++ pointer is memory address in BYTES, it seemed logical to me that the code would be newaddr = curaddr + N * sizeof(mytype) . It produced errors though; later I found that with newaddr = curaddr + N everything works correctly. Why so? Should it really be address + N instead of address + N * sizeof? Part of my code where I noticed it (2D array with

Changing to Pointer Arithmetic

旧城冷巷雨未停 提交于 2019-12-02 16:33:12
问题 I'm new to pointers in C and I'm having a lot of trouble figuring out how to work with them. I have this piece of code with some pointer arithmetic inside a void function: void function(int n, int *a, int *b){ int *p,*q; q = b; int i; *b = 0; for (i = 1; i<n; i++) if(*(a+i) == *(a+i-1)) *(b+i)=0; else *(b+i)=1; } I'm trying to change it completely to use only pointer arithmetic with no loop index variables. So far I've gotten this: void function(int n, int *a, int *b){ int *p,*q; q = b; *b =

C++ pointer arithmetic weirdness

痴心易碎 提交于 2019-12-02 13:52:17
问题 I found my bug (after a few hours) and isolated it in the following program. The problem is with the way in which the pst2 variable's value is calculated when using pointers to a struct. When using pointers to char, all works fine. Why is this? (Using gcc/g++ version: (Debian 4.4.5-8) 4.4.5) (For those who are wondering: I'm accessing a file-buffer containing data-groupings at regular offsets.) #include <iostream> #include "testpa.h" #pragma pack(push) #pragma pack(1) //----------------------

C++ pointer arithmetic weirdness

十年热恋 提交于 2019-12-02 07:50:56
I found my bug (after a few hours) and isolated it in the following program. The problem is with the way in which the pst2 variable's value is calculated when using pointers to a struct. When using pointers to char, all works fine. Why is this? (Using gcc/g++ version: (Debian 4.4.5-8) 4.4.5) (For those who are wondering: I'm accessing a file-buffer containing data-groupings at regular offsets.) #include <iostream> #include "testpa.h" #pragma pack(push) #pragma pack(1) //--------------------------- struct st_one { int i; char c; }; //--------------------------- struct st_two { long l; int i; };

Problems iterating through AddressOfNames member of IMAGE_EXPORT_DIRECTORY structure

匆匆过客 提交于 2019-12-02 06:25:54
问题 I'm having problems enumerating function names in kernel32.dll . I retrieved its IMAGE_EXPORT_DIRECTORY structure and stored an array of pointers to char arrays of each function name: char** name_table = (char**)(image+pExp_dir->AddressOfNames); //pExp_dir is a pointer to the IMAGE_EXPORT_DIRECTORY structure . I'm now trying to iterate through the function names and match them to a string containing the name of the function whom's RVA I need. for(int i=0;i<pExp_dir->NumberOfNames;i++) //until

Is pointer arithmetic possible with C++ string class?

前提是你 提交于 2019-12-02 03:00:37
After programming a little in C I decided to jump right into C++. At first I was pleased with the presence of the string class and being able to treat strings as whole units instead of arrays of characters. But I soon found that the C-style strings had the advantage of letting the program move through it character by character, using pointer arithmetic, and carry out a desired logical operation. I have now found myself in a situation that requires this but the compiler tells me it is unable to convert from type string to the C-style strings. So I was wondering, is there a way to use pointer

How to resolve pointer alias issues?

巧了我就是萌 提交于 2019-12-02 01:49:14
问题 Careless use of templates can cause bloat. One way to avoid that bloat is to have a thin typesafe template that wraps non-typesafe non-template code. To do this, the wrapper needs to provide some way for the non-template code to access things it knows nothing about. For example, in a data structure, the wrapper defines the node structs. The unsafe code needs to read and write to the nodes, but must do so indirectly, through some kind of interface which is specified by the wrapper. One way to

Why am I getting segfault when changing the signature of main?

微笑、不失礼 提交于 2019-12-01 23:48:18
问题 I am trying to get my feet into C, and wrote this program that displays a kb of my RAM in a random location. Here is the code, and it works fine: #include <stdio.h> int main(){ char *mem; for(int i =0; i < 1024; i++){ mem++; printf("%c", *mem); } return 0; } After that, I did the following change in my code, and I get segfaults every time I run my program: #include <stdio.h> // Just added this signature int main(int argc, char *argv[]){ char *mem; for(int i =0; i < 1024; i++){ mem++; printf("

Set shared_ptr with new_pointer that is old_pointer + offset

天大地大妈咪最大 提交于 2019-12-01 14:55:48
Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows. Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data . This address is still in alocated memory. But how to set without losing it? A question is (yes/no): Is it possible to set p to offset of prevous pointer,