We changed our privacy policy. Read more.

New answers tagged

6

Don't talk like a pirate I would prefer the name array (another reason not to using namespace std;) to arr. And I'd prefer a more descriptive name like data or numbers to either. Don't waste work int push(int value) { length++; if (length == (size + 1)) grow(); data[length - 1] = value; return length; } ...


6

Consider using size_t instead of int. The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to problems, particularly as 64-bit architectures become more prevalent. It is generally bad practice to repeat code that does the same thing. Consider changing copyToNewSize to something ...


17

First of all, there is a major problem with your class: it does not respect the rule of three/five/zero: it has a non trivial destructor that destroys memory allocated in constructor it neither defines nor deletes the copy constructor and assignment operator Let us look at the following code: DynamicArray dynarr[16]; // initialize values in dynarr if ...


1

char *strdup_(const char *src) { char *dst = malloc(strlen (src) + 1); if (dst == NULL) return NULL; strcpy(dst, src); return dst; } You are calling strlen in order to allocate memory, and then calling strcpy which works by calling strlen first! Remember the length, and use ...


5

Answers to your questions How can I deal with cleaning up resources in a more concise way? As of now, I'm trying to emulate exception handling by letting errors bubble up to main and doing general cleanup there. I thought about passing around a structure (allocator pattern) to error-throwing functions. C++ makes this a lot easier, with RAII and language ...


4

Toby Speight has already given a great answer, I'd just like to highlight the security issues: Remove the line length limit You have arbitrarily set a limit to the length of lines. However, what happens if the input has a line longer than that? If fgets() reaches the maximum line length, it will stop reading any further characters, and will return a pointer ...


8

#define MAX_LINE_WIDTH 100 Arbitrary limits such as this are difficult to get right. No matter how large you make it, the real world somehow always manages to find data that exceeds the limit. In this case, as we use this limit with fgets(), we really need to check whether the terminating newline was read instead of always assuming we get a complete line. ...


5

Here are some things that may help you improve your program. Fix the bug In assemble() the file is read with fread which is appropriate, but later when the buffer is being processed to strip comments, the code makes the invalid assumption that the buffer is terminated with a NUL character. To fix that, explicitly terminate the string: // Read the contents ...


4

Alternatives to Valgrind for memory allocation checking I don't believe Valgrind is supported on M1 Mac machines, therefore the memory allocation and deallocation was written somewhat blind. Luckily there are other ways to check whether you have memory leaks. In particular, Clang includes support for the AddressSanitizier. Compile and link your program ...


3

I know this is a really old question but, have you tried using a Session instance when calling wreq's getWith? Using the getWith version included in Network.Wreq creates a separate session for each request, hindering resource sharing and heavily increasing memory usage. It can also cause memory fragmentation, which translates into an increasingly larger ...


Top 50 recent answers are included