New answers tagged

1

In addition to @Mark Bluemel good answer: Overlap A difference between memcpy() and memmove() is the ability to handle overlapping buffers. Notice the keyword restrict. void *memcpy(void * restrict s1, const void * restrict s2, size_t n); void *memmove(void *s1, const void *s2, size_t n); restrict roughly implies access to the buffer is not interfered with ...


4

Interesting project, writing it python does come with some limitations. Numpy is mostly written in C and there are good reasons for it. The memory management is much better and most of the array-manipulation routines can be done in-place instead of copying back and forth. AFAIK, there is no equivalency for pure c-arrays in Python. A tuple is probably the ...


4

Your code doesn't compile cleanly with GCC: move.c: In function 'memCopy': move.c:16:15: warning: comparison of distinct pointer types lacks a cast while (a2 < end) ^ Also 2 brief points: Why reinvent the wheel? Most implementations of memcpy, memmove etc in the standard libraries will be correctly and efficiently implemented, in ...


2

The problem with the representation (null-terminated char*, like C) is that it's inefficient for concatenation. If we use array+length, as is common for strings in many languages (including the C++ standard library), then we don't need to seek to the end for every operation (as we do here, hidden inside strcat() - even though we've already computed the ...


2

You did a single-null-allocation correctly(ish) once: if(str == nullptr){ this->str = new char; *(this->str) = '\0'; and incorrectly a second time: if(str == nullptr){ this->str = new char; this->str = '\0'; This will produce a memory leak. That aside, if your compiler allowed this without yelling about incompatible types, that ...


5

ConsumeNext() should not return a reference The problem with ConsumeNext() is that it returns a reference to an object. However, that element is also marked as consumed. So the caller cannot safely access this element, since it can be overwritten by a call to Push() from another thread. There are two options to solve this: Have ConsumeNext() return by value,...


2

XV6 bizarreness XV6 has made at least one ill-advised decision in forcing a non-standard signature of printf that requires a file descriptor be passed in. Maybe they should have omitted printf entirely and made you use fprintf instead, but anyway: at the least, you should make a stdout constant equal to 1 and use this instead of the numeric literal. You ...


1

If you're using clang or GCC ≥10, you can use the __is_same(type1, type2) intrinsic. For portability: #if !defined(__is_same) && (!defined(__clang__) || !defined(__GNUC__) || __GNUC__ < 10) #define IS_SAME(T1, T2) std::is_same_v<T1, T2> #else #define IS_SAME(T1, T2) __is_same(T1, T2) #endif


2

Welcome to Rust, and welcome to Code Review! rustfmt & clippy Always run cargo fmt and cargo clippy first. cargo fmt formats your code according to the official Rust Style Guide, and cargo clippy detects common mistakes and provides feedback on improving your code (none in this case). main.rs The biggest problem with the main function is its structure. ...


4

Incorrect size check itoa(num, 2, buffer); forms "17", which needs 3 char, even though only 2 provided. Perhaps OP meant 2 to be the string length allowed and not the string size needed. In which case sizeof buffer / sizeof *buffer is not the length allowed. Missing header Add #include <stdbool.h> Missing size check Corner case: strcpy(...


2

Few things to consider. Why are you writing your own implementation of something that already exists, long history of reliable use with well defined behaviour (fun/assignment - sure, generally - waste of effort likely to lead to problems)? What if input, from, or to are null, or not valid (not \0 terminated), OR overlap in any way (will it work)? from and ...


1

The big thing that jumped out to me was how you seem to be treating Haskell lists like Java... Arrays? (I haven’t used Java since... 2006?) If you’re repeatedly fiddling with the elements at the end of lists, you’re probably doing something wrong. I’m not sure what the complexity of this all is, but I think it might be something like \$\mathcal{O}(m^2n^2)\$ ...


0

The implementation is non-conformant. No handling of undefined PATH. Quoting man execp: search path is the path specified in the environment by ``PATH'' variable. If this variable is not specified, the default path is set according to the _PATH_DEFPATH definition in <paths.h> Once confstr returns 0, check errno, and act accordingly. Calling abort ...


0

The main sin (accidental pun) is putting it all in fn main. Prefer fancier but readable code over condensed code. This often boils down to adding a function for some assignments. Indeed, we can add a function for getting the content of let (mut input, len) and another for getting let pb. A minor sin is failing if fn metadata fails. You only need metadata to ...


Top 50 recent answers are included