« first day (1327 days earlier)      last day (1517 days later) » 

12:46 AM
it applies "don't compute what you know the answer for" as a enforceable rule for the compiler
 
 
8 hours later…
8:51 AM
/* t_mmap.c

   Demonstrate the use of mmap() to create a shared file mapping.
*/
#include <sys/mman.h>
#include <fcntl.h>
#include "tlpi_hdr.h"

#define MEM_SIZE 10

int
main(int argc, char *argv[])
{
    char *addr;
    int fd;

    if (argc < 2 || strcmp(argv[1], "--help") == 0)
        usageErr("%s file [new-value]\n", argv[0]);

    fd = open(argv[1], O_RDWR);
    if (fd == -1)
        errExit("open");

    addr = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED)
What's the use of the memset(addr, 0, MEM_SIZE); /* Zero out region */ line of code? I think it's completely uncessary.
 
nwp
9:20 AM
As far as I can tell it makes a difference in case strlen(argv[2]) > MEM_SIZE because then strncpy will write MEM_SIZE - 1 bytes and the last byte in addr is left uninitialized, but you need it to be '\0'. That said there are better ways to set a byte to 0.
 
10:10 AM
void
cmdLineErr(const char *format, ...)
{
    va_list argList;

    fflush(stdout);           /* Flush any pending stdout */

    fprintf(stderr, "Command-line usage error: ");
    va_start(argList, format);
    vfprintf(stderr, format, argList);
    va_end(argList);

    fflush(stderr);           /* In case stderr is not line-buffered */
    exit(EXIT_FAILURE);
}
@nwp Within cmdLineErr it just exits.
Can I replace that 2 lines with strncpy(addr, argv[2], MEM_SIZE);?
        if (argc > 2) {                     /* Update contents of region */
            if (strlen(argv[2]) >= MEM_SIZE)
                cmdLineErr("'new-value' too large\n");

            // memset(addr, 0, MEM_SIZE);      // Zero out region
            // strncpy(addr, argv[2], MEM_SIZE - 1);

            strncpy(addr, argv[2], MEM_SIZE);

            if (msync(addr, MEM_SIZE, MS_SYNC) == -1)
                errExit("msync");

            printf("Copied \"%s\" to shared memory\n", argv[2]);
        }
 
nwp
I don't know what all this is. Try stepping through the program with a debugger and look at how the values change and where exactly it "just exits".
You can replace strncpy with strcpy now since you do a bounds check already.
 
ok
I remove some helper functions and here is
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define MEM_SIZE 10

int
main(int argc, char *argv[])
{
	char *addr;
	int fd;
	if (argc < 2 || strcmp(argv[1], "--help") == 0)
		exit(0);

	fd = open(argv[1], O_RDWR);
	if (fd == -1)
		exit(1);

	addr = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	if (addr == MAP_FAILED)
		exit(2);
	if (close(fd) == -1) /* No longer need 'fd' */
		exit(3);

	printf("Current string=%.*s\n", MEM_SIZE, addr);
I didn't see any differences by replacing those 2 lines with strncpy(addr, argv[2], MEM_SIZE);.
 
nwp
10:31 AM
Wait, so you're writing to a file, you don't actually handle a C string. So you don't actually want a null terminator. Which would be the only difference.
 
I don't get it. Since the check is strlen(argv[2]) >= MEM_SIZE, so at most I have a MEM_SIZE - 1 string without null terminator. Then with strncpy(addr, argv[2], MEM_SIZE);, I always end up with a null terminator.
I mean, still, I see no differences QAQ
 
nwp
strncpy adds a null terminator unless the string has maximum size.
Maybe you should use memcpy instead.
 
@nwp Yes
Never mind. Forget this
 
With type mpz_class from GMP, how do you get the last n digits from the binary expression? I know the usual way would be to create a bit mask by doing (1 << n)-1 but I can't find the left shift operator for GMP
I'm guessing mpn_lshift()can be used but I find it's documentation confusing gmplib.org/manual/Low_002dlevel-Functions.html
"Function: mp_limb_t mpn_lshift (mp_limb_t *rp, const mp_limb_t *sp, mp_size_t n, unsigned int count)

Shift {sp, n} left by count bits, and write the result to {rp, n}. The bits shifted out at the left are returned in the least significant count bits of the return value (the rest of the return value is zero).

count must be in the range 1 to mp_bits_per_limb-1. The regions {sp, n} and {rp, n} may overlap, provided rp >= sp. "
 
11:01 AM
Why is it shifting two things and not just one?
 
nwp
@northerner What are those 2 things? As far as I can see only sp is being shifted.
 
@nwp {sp,n}
 
nwp
@dhS That is not a C or C++ question. Maybe ask in a math forum or something. But really, you should just write it down and you'll figure it out.
 
oh n is size
 
nwp
4 messages moved to Trash can
 
11:09 AM
@nwp also what's with the mp_limp_t type? How do you go from mpz_class to it and back?
 
nwp
> A limb means the part of a multi-precision number that fits in a single machine word. (We chose this word because a limb of the human body is analogous to a digit, only larger, and containing several digits.) Normally a limb is 32 or 64 bits. The C data type for a limb is mp_limb_t.
Looks like you're using the C++ wrapper version and looking at the C implementation. They are probably not meant to be mixed. Have you tried to just write mpz_class n; n << 42;? I would expect the C++ wrapper to support that and map it to the correct C function.
 
11:42 AM
@nwp I don't think GMP has << overloaded
Compiler gives...
error: no match for 'operator<<' (operand types are 'mpz_class' {aka '__gmp_expr<__mpz_struct [1], __mpz_struct [1]>'} and 'mpz_class' {aka '__gmp_expr<__mpz_struct [1], __mpz_struct [1]>'})
10 | mpz_class b = (mpz_class(1) << a);
| ~~~~~~~~~~~~ ^~ ~
| | |
| | __gmp_expr<[...],[...]>
| __gmp_expr<[...],[...]>
TestBitShift.cpp:10:30: note: candidate: 'operator<<(int, int)' <built-in>
 
nwp
I don't know. This looks kinda promising.
 
 
6 hours later…
5:20 PM
I'm a bit confused about something with std::future and std::promise. What if I have a case where I have many more calls to a function than I do threads, like say 1000. I don't want to create 1000 threads accidentally, but I need to make sure all 1000 are handled. With threads I would do something like (i = threadID, i < container.size(); i += numthreads) but I am not sure how to do something similar with std::future.
I think maybe this just a case where making some kind of thread pool is inevitable?
 
6:00 PM
I don't understand what that "i=threadID ...." stuff is supposed to do
 
The idea is that (regardless of how many threads) each one ends up taking on an equal share of work. So if there are 1000 elements in the container to be processed, and 4 threads, thread 0 does work on element 0, then 4, then 8, etc, while thread 1 does element 1, 5, 9, etc
I have a feeling there is a better way, it's something I picked up from older pre-C++11 code that used pthreads.
 
You can do a simple work-queue, you're on a pre-emptively multithreaded system. You can create a packaged_task for each work-item. Or if a single computation is small enough then manually partitioning, like you mentioned is still among the best options. Maybe partitioned by hardware_concurrency
 
so you mean like a dispatch to a threadpool with a max on number of live threads
 
What does a simple work-queue look like? And what is the advantage/purpose of a packaged task?
And no, each task is not small in my case.
 
6:56 PM
is there a better way for changing the sign of a number than x=-x? would x*=-1 be slower?
 
@SAJW do you have a reason to care about performance to that extend? x=-x; seems more readable
 
@maestromusica no, I was just wondering, that's all.
 
7:11 PM
@TylerShellberg with a simple work-queue I meant just a container with the necessary context for each computation and a lock/mutex to avoid contention. So for your case, essentially just a `std::vector<int>` with all the numbers to be processed, a mutex for adding/removing items and however many threads you want just removing items to work on, one-by-one.

A packaged_task is just what the name says a wrapper for a task (with potentially a result). That you can get a future from and execute elsewhere. You could create 1000 of those and put the futures for them in a collection. Then wait for
 
Hello :)
Recently I had to help a coworker program an arduino unit with some very basic code. It was reading from two sensors and an rtc. The issue I came across was that the values read from the stream seemed to change at different times. In other words, the sensor read, if it worked, was 0 on request, and then when printed would be 900. However, at read if the sensor was 2 on request, then it was a failed read. Very strange.
Is anyone aware of Wire or stream based events that fire when the data is available in arduino's reduced instruction set?
 
user6461957
8:11 PM
Why should I consider disabling the copy ctor and copy assignment operator of a base class say A. If we have a derived class struct B : A and class A with members char a, and char b (2 bytes); class B with one additional char member c (3 bytes), then doing this A x{B::y} would copy char a and char b (char c will be excluded). That is slicing. So far so good.
 
user6461957
But let's say I don't consider this to be an issue, then why should I still consider deleting the copy ctor and copy assignment op of the base class A?
 
user6461957
Second question, consider a base class A with a virtual function virtual void A::foo(), if A has nothing else but the virtual function, a vptr to a vtbl will be A's member (taking up 8 bytes). Consider a derived class B : A that has another virtual function virtual void B::bar(), now will there be another vptr with yet another vtbl or will the same vptr and vtbl be used that got inherited by A?
 
user6461957
Third question, if I remember correctly, if I have a class A that has a virtual function virtual void A::foo() and B inherits from A, then if I do another virtual declaration inside B, i.e., virtual void B::foo(), then what will happen? Is that even valid?
 
user6461957
8:26 PM
To 3: the size won't increase if I declare another virtual inside the derived class: https://godbolt.org/z/BL9wVG
But why?
 
9:54 PM
#include <stdio.h>

int main()
{
    int rev_num=0,x=0,n,a;
    printf("Enter a number");
    scanf("%d",&n);
    for(int i=n+1;i<i+1;i++)
    {
        a=i;
        while(a>0)
        {
            rev_num=rev_num*10+a%10;
            a/=10;
        }
        for(int j=2;j<i;j++)
        {
            x=0;
            if(i%j==0)
            {
                x=1;
                break;
            }
        }

        if(x==0 && i==rev_num)
        {
            printf("%d is the next prime palindrome",i);
the above code is not producing any o/p neither showing any errors, it keeps on taking inputs indefinitely.
Here's the question- Take any number as input and print the next prime palindrome on the screen. Example: if input is 7, o/p is 11 and if i/p is 113, o/p is 131. Please help, it would be appreciated!!!!!!
 
user6461957
You should consider initializing your variables to prevent erroneous behavior. Okay, a palindrome is a word that is read the same forward or backward. Your question is not clear to me; what you intend the program to do exactly? Please be more clear. A more or less accurate description would help.
 
user6461957
What do you mean by o/p?
 
user6461957
Okay so apparently for each prime number you put in, you would like to have the next prime number which also happens to be a palindrome.
 
user6461957
Oh wait it is "any" number
 
10:16 PM
how would I store the value of a pointer (i.e. the address the pointer is pointed at) in say int?
int x = ptr;
 
user6461957
@chewbapoclypse that won't work generally unless you cast it to an int.
 
so like int x = (int)ptr;
 
user6461957
Well, but you will likely lose information on a 64 bit machine.
 
ok so you say store it in a long int?
what I am trying to do is store the address into an array
but not as a pointer, but the addresses raw bytes
 
user6461957
Okay, if I understand your question, why not have an array of void pointers?
 
user6461957
10:27 PM
or pointers of char, or int, doesn't matter. A pointer, points to another address. If you want the address of a pointer stored, then have a double pointer or a void pointer or any other pointer doesn't matter, but you need to cast then.
 
user6461957
Not sure if I got your question right.
 
user6461957
So, if you want to store the address of a pointer to another pointer, you can do one of the following:
void *p1 = &ptr;
char *p2 = (char *)(&ptr);
char **p3 = (char **)(&ptr);
int **p4 = &ptr;
...
If you want to store the address to another type like long int or uint64_t or whatever, do this:
uint64_t addr = (uint64_t)(&ptr);
 
char*
+--------------+-----------------------------------------------------------+
| | | |
| some bytes | address the pointer is looking at | some more bytes |
| | | |
+--------------+-----------------------------------+----------------------+
it ruined my ascii art :@
 
user6461957
Okay, to be clear: do you want to store the address of a pointer or do you want to store the value of a pointer?
 
not the address of the pointer the value of the pointer
 
user6461957
10:35 PM
Ah, okay
 
user6461957
Then forget what I stated earlier.
 
user6461957
Wait
 
user6461957
Well in that case you can store it to types like int64_t or long int (not portable), or even double, but you need to cast.
I would store it inside a void pointer:
int x = 42;
int *ptr = &x;
void *pp = ptr;
Or a char pointer (then it will be byte addressable, since a char is 1 byte):
char *pp = (char *)ptr;
 
user6461957
If you want to store it inside another type like int64_t, do this:
 
user6461957
int64_t addr = (int64_t)ptr;
 
10:39 PM
ok let me run that really quick
does int64_t exist on 32 bit machines?
 
user6461957
Yeah, it is guaranteed as far as I know by the compiler implementation.
 
user6461957
You need to include <inttypes.h>
 
user6461957
If I remember correctly
 
user6461957
Oh wait do you have a 32-bit machine? Or rather, do you compile for a 32-bit target?
 
user6461957
In that case you can probably just use int.
 
user6461957
10:43 PM
Background: on a 32-bit machine a pointer has byte size 4. On a 64-bit machine 8.
 
no I am just trying to be platform agnostic
 
user6461957
Then I wouldn't use int64_t addr = (int64_t)ptr;
 
user6461957
As the pointer sizes can change depending on the machine type
 
I am using this char *pp = (char *)ptr;
 
user6461957
I would use a char * (always addresses 1 byte) or better a int8_t *.
Okay, good.
 
user6461957
10:47 PM
Now, the value of the int pointer ptr is byte addressable
 
user6461957
Since a regular int has 4 bytes (usually), you can access each of those bytes via pp[0], pp[1], pp[2] or alternatively: *(pp + 0) which is just *pp, *(pp + 1) etc.
 
user6461957
But there is an issue with this: byte ordering so it will break portability
 
user6461957
Hmm..
 
user6461957
So then probably this will do the job:
 
I got this when printing to console
0x1f5e50
std::cout << std::hex << std::setfill('0');
int8_t *pp = (int8_t *)ptr;
std::cout << std::setw(2) << reinterpret_cast<unsigned*>(pp) << " ";
std::cout << std::dec << std::endl;
 
user6461957
10:53 PM
okay use int32_t
 
user6461957
try that
 
0x765e50
I think the reinterpret_cast<unsigned*>(pp)
is clamping it to 8 bytes
or for rather
four**
 
user6461957
reinterpret_cast<int32_t *>? ptr is of type int right?
 
yeah int of 4 bytes
pointer to int of 4 bytes pedantically
 
user6461957
std::cout << std::hex << std::setfill('0');
std::cout << std::setw(2) << reinterpret_cast<int32_t *>(ptr) << " ";
std::cout << std::dec << std::endl;

?
 
11:01 PM
0x10a5e50
 
user6461957
What is the goal here? Do you want the actual integer value?
 
user6461957
Didn't you want the pointer value?
 
let me create a picture
 
user6461957
k
 
user6461957
11:13 PM
Okay, so if you want to copy the bytes to that array, you have to use something like memcpy.
 
user6461957
Wait
 
user6461957
I will provide you an example
 
user6461957
uint8_t *arr = new uint_t[256];
std::memcpy(dst, ptr, sizeof(ptr));

dst should be the beginning address/location where you want to store that pointer.
 
user6461957
Clear?
 
yeah, I get what you are saying, does memcpy make syscalls?
 
user6461957
11:22 PM
Not that I know of, but std::memmove will rely on a syscall (since it will allocate some heap memory) as far as I know.
 
ok so this is not doing what I am trying to do
&(*ptr) is what I want
 
user6461957
"I want to write the address that the pointer is pointing to in this area of the array", which means you want to copy the contents (value of the pointer) to a (byte addressable) array.
 
user6461957
Er wait
 
address of the value that pointer is pointed at
 
user6461957
?

&(*ptr) you are dereferencing ptr and then you want the address of a value? I don't think you will get something decent out of that.
 
user6461957
11:32 PM
I guess I don't get your question. Maybe some others can help or understand what you want to achieve. Sorry. I tried to be helpful. :(
 
wait one more picture
it will be clear as day
 
user6461957
k
 
user6461957
Okay, let me be clear, if you do:

int x = 5;
int *p = &x;

p == &x; // true

So a pointer has the address stored as a value already.
So given the picture above, I would do something like this:

std::memcpy(your_array, &x, sizeof(&x));
 
user6461957
Or rather:
std::memcpy(your_array + byte_offset, &x, sizeof(&x));
 
user6461957
11:44 PM
std::memcpy(your_array + byte_offset, &x, sizeof(&x)); // address of x
std::memcpy(your_array + byte_offset + sizeof(&x), x, sizeof(x)); // value of x
 
I believe that worked
memcpy(ptr+4, &ptr, sizeof(&ptr));
yielded e1 03 00 00 50 5e e3 00 00 00 00 .....
yo can see that 50 5e e3 00 is there
 
user6461957
So could I help at all?
 
yeah that looks like it worked so I should be able to read it back out as a pointer
and get the starting position
ie .. e1 03 00 00
 
user6461957
Wait
 
user6461957
memcpy(ptr+4, &ptr, sizeof(&ptr));
 
11:50 PM
yea?
 
user6461957
Don't you want the address of x?
 
user6461957
namely 5
 
not in this case
the ptr is pointed at the beginning of the array
 
user6461957
Okay, I hope it is clear for you that &ptr will yield the address of the pointer itself
 
ohh
yeah thats not what I want
 
user6461957
11:52 PM
How about?:
memcpy(ptr+4, ptr, sizeof(ptr));
 
noo thats not it either
that duplicates
 
user6461957
ptr points to another int, right?
 
e1 03 00 00 e1 03 00 00 00 00
 
user6461957
Ah wait
 
user6461957
No, in that case, it needs to be &ptr
 
user6461957
11:53 PM
My bad
 
like so ... memcpy(ptr+4, &ptr, sizeof(ptr));
 
user6461957
No, no like before
 
user6461957
memcpy(ptr+4, &ptr, sizeof(&ptr));
 
user6461957
B/c for copying you need to provide an address to memcpy, so you are providing the address of the pointer itself, and then sizeof(&ptr) will be copied from &ptr to ptr + 4
 
but isn't that address of the pointer someplace else?
the pointer has address for itself and then it points to another address right?
 

« first day (1327 days earlier)      last day (1517 days later) »