2012/04/19

Size of memory allocation(malloc) for c language

From: BASIC programming forum - "C": Size of memory allocated to a pointer?


#include <stdio.h> 
#include <stdlib.h> 
#include <alloc.h> 

void *my_alloc(int size) {
    int *block;

    /* bump up request so we have a place to store the size */
    block = malloc(size+sizeof(int));
    if (block == NULL)
        return NULL;

    /* store the size */
    *block = size;
    /* and return pointer to next place (automatically scaled) */
    return block + 1;
}

void my_free(void *block0) {
    int *block = block0;

    /* go back to the actual allocation address */
    block--;
    free(block);
}

int my_msize(void *block0) {
    int *block;

    /* back up to the size of the block */
    block = (int *)block0 - 1;

    return *block;
}

int main() {
    char *p;
    int rc;

    p = my_alloc(50);
    printf("Size of p is %d\n", my_msize(p));
    my_free(p);
    return 0;
}

2012/01/06

c# hex str transformation

Form looks like this...

Code is below...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace StrHex
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             EncodingInfo[] infos = Encoding.GetEncodings();
  22.             for (int i = 0; i < infos.Length; i++)
  23.                 comboBox1.Items.Add(string.Format("{0} - {1}", infos[i].DisplayName, infos[i].Name));
  24.  
  25.             if (comboBox1.Items.Count > 0)
  26.                 comboBox1.SelectedIndex = 0;
  27.         }
  28.  
  29.         private void button1_Click(object sender, EventArgs e)
  30.         {
  31.             string name = comboBox1.Text.Split(new string[] {" - "}, StringSplitOptions.None)[1];
  32.             Encoding encode = Encoding.GetEncoding(name);
  33.             if (encode != null)
  34.             {
  35.                 Byte[] bs = encode.GetBytes(textBox1.Text);
  36.                 if (bs.Length * 2 > textBox1.MaxLength)
  37.                     return;
  38.  
  39.                 StringBuilder sb = new StringBuilder();
  40.                 foreach (Byte b in bs)
  41.                     sb.AppendFormat("{0:X2}", b);
  42.                 textBox1.Text = sb.ToString();
  43.             }
  44.         }
  45.  
  46.         private void button2_Click(object sender, EventArgs e)
  47.         {
  48.             string name = comboBox1.Text.Split(new string[] {" - "}, StringSplitOptions.None)[1];
  49.             Encoding encode = Encoding.GetEncoding(name);
  50.             if (encode != null)
  51.             {
  52.                 if (textBox1.TextLength % 2 == 0)
  53.                 {
  54.                     ArrayList al = new ArrayList();
  55.                     for (int i = 0; i < textBox1.TextLength; i += 2)
  56.                     {
  57.                         Byte b = 0;
  58.                         string hex = textBox1.Text.Substring(i, 2);
  59.                         if (Byte.TryParse(hex, System.Globalization.NumberStyles.HexNumber, null, out b))
  60.                             al.Add(b);
  61.                         else
  62.                             return;
  63.                     }
  64.                     textBox1.Text = encode.GetString((Byte[])al.ToArray(typeof(Byte)));
  65.                 }
  66.             }
  67.         }
  68.  
  69.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  70.         {
  71.             if (e.Control && e.KeyCode == Keys.A)
  72.                 textBox1.SelectAll();
  73.         }
  74.  
  75.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  76.         {
  77.             if (e.Control && e.KeyCode == Keys.A)
  78.             {
  79.                 textBox1.SelectAll();
  80.                 textBox1.Focus();
  81.             }
  82.  
  83.             if (e.Control && e.KeyCode == Keys.H)
  84.                 button1.PerformClick();
  85.  
  86.             if (e.Control && e.KeyCode == Keys.S)
  87.                 button2.PerformClick();
  88.  
  89.             if (e.Control && e.KeyCode == Keys.W)
  90.                 this.Close();
  91.         }
  92.  
  93.         private void textBox1_TextChanged(object sender, EventArgs e)
  94.         {
  95.             label2.Text = string.Format("Word Count: {0}", textBox1.TextLength);
  96.         }
  97.     }
  98. }
  99.  

2011/12/29

msys git single file history

In Git BASH
gitk -- path/to/file &

Or right click on file
Then click "Git History"

BTW...
Not only file, files in folders also do.

2011/12/26

net snmp 尋找 mib tree

先看 net snmp
把mib檔設定一下 然後載入mib檔

找名字含有ssl的snmp oid
snmptranslate -TB '.*ssl.*'

http://www.net-snmp.org/wiki/index.php/TUT:snmptranslate 看來的...

2011/12/13

html encode/decode using javascript

  1. String.prototype.encodeHTML = function() { var d=document.createElement("div");(d.textContent)?(d.textContent=this):d.innerText=this;return d.innerHTML };
  2. String.prototype.decodeHTML = function() { var d=document.createElement("div");d.innerHTML=this;return d.innerText||d.textContent };