Skip to content

Instantly share code, notes, and snippets.

View muaddib1971's full-sized avatar

Paul Miller muaddib1971

  • RMIT University
View GitHub Profile
@muaddib1971
muaddib1971 / badfree.cpp
Created May 15, 2024 03:02
memory problems
#define size 30
#include <cstdlib>
void allocate_array(int** array) { *array = new int[size]; }
int main() {
int* array;
allocate_array(&array);
int count;
for (count = 0; count < size; ++count) {
array[count] = count;
}
#include <stdio.h>
#include <stdlib.h>
enum month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
const char* month_str[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
int main() {
enum month themonth;
for (themonth = JAN; themonth <= DEC; ++themonth) {
switch (themonth) {
#include <iostream>
#include <string>
#include <limits>
int main(void) {
int count{};
while(true) {
std::string s;
std::cout << "ctrl-d has been pressed " << count << " times "
<< std::endl;
@muaddib1971
muaddib1971 / vector_init.cpp
Created April 10, 2023 06:39
vector initialisation
#include <vector>
#include <iostream>
int main(int argc, char * argv[]) {
//check for the correct args
if(argc != 3) {
std::cerr << "error: invalid args" << std::endl;
return EXIT_FAILURE;
}
//extract widht and height from the args
@muaddib1971
muaddib1971 / IntList.cpp
Created April 4, 2023 08:49
c++ linked list
#include "IntList.h"
#include <climits>
Node::Node(int _data) : next(nullptr), data(_data) {}
void Node::setNext(Node* next) { this->next = next; }
Node* Node::getNext() { return next; }
int Node::getData() { return data; }
LinkedList::LinkedList() : head(nullptr), size(0) {}
void LinkedList::add(int data) {
@muaddib1971
muaddib1971 / menu.cpp
Last active March 25, 2023 12:02
a menu using the command pattern
#include <iostream>
#include <string>
class menu_action {
public:
virtual bool operator() ()const = 0;
};
class play_game : public menu_action {
public:
@muaddib1971
muaddib1971 / summit.py
Created January 17, 2023 08:29
python code to implenet sum
#!/usr/bin/env python3
result = 0
for n in range(1,101):
result +=5*n*n
print (result)
@muaddib1971
muaddib1971 / linkedlist.py
Created September 15, 2022 03:23
linked list example
class Node:
def __init__(self, num: int) -> None:
self.num = num
self.next = None
class LinkedList:
def __init__(self):
self.len = 0
self.head = None
@muaddib1971
muaddib1971 / alloc.c
Last active September 15, 2022 01:26
beginning of malloc impementation
#include <stdio.h>
#include <unistd.h>
struct alloc {
void* data;
long size;
};
struct alloc thememory[BUFSIZ];
struct alloc frees[BUFSIZ] = {0};
long num_allocs = 0;
#!/bin/bash
valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all $@