What I thought about pointers and what they really are?

One bright morning, the day I had started learning C, I stumbled onto something called pointers. A guy on the forums claimed that if you know pointers you know C. I planned to study pointers all night-continuing-to-morning. The tutorial mentioned about something called an address. The addresses related to memory and how they store data is something you need to know before you study pointers. Lets start with strings.

How are strings represented in memory?

See boy, strings say you gotta have the chars, a null byte and n + 1 bytes of memory.

If its on the stack, the string would be like this:

  • H
  • E
  • L
  • L
  • O
  • \0

The ordering of the characters depending on the endianess. What is char *?

Its a variable holding the address of the first character of the string. Literally!

We’ve arrived at the definition of a pointer here. That’s what a pointer is always, a variable holding the address. Its 4 bytes on a 32 bit system and 8 bytes on the 64 bit one. The pointer still have types thought, it always contains a memory address but the pointer behaves differently for different types. Read more about it when we discuss offsets.

How are arrays represented in memory?

It’s a similar deal with arrays, except a few facts:

char [] != char *

For 1 year since I started learning C, I assumed they were the same thing.

2.2: But I heard that char a[] was identical to char *a.

Not at all.  (What you heard has to do with formal parameters to functions; see question 2.4.)  Arrays are not pointers.  The array declaration “char a[6];” requests that space for six characters be set aside, to be known by the name “a.”  That is, there is a location named “a” at which six characters can sit. The pointer declaration “char *p;” on the other hand, requests a place which holds a pointer.  The pointer is to be known by the name “p,” and can point to any char (or contiguous array of chars) anywhere.

As usual, a picture is worth a thousand words.  The statements

	char a[] = "hello";
	char *p = "world";

would result in data structures which could be represented like this:

	   +---+---+---+---+---+---+
	a: | h | e | l | l | o |\0 |
	   +---+---+---+---+---+---+
	   +-----+     +---+---+---+---+---+---+
	p: |  *======> | w | o | r | l | d |\0 |
	   +-----+     +---+---+---+---+---+---+

It is important to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location “a,” move three past it, and fetch the character there.  When it sees the expression p[3], it emits code to start at the location “p,” fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to.  In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently.  (See also questions 17.19 and 17.20.)

Source: http://www.lysator.liu.se/c/c-faq/c-2.html

Offsets with pointers

Pointers will have different offsets depending on the exact type of the pointer. So if you make a pointer to an array using:

#include

int main()
{

 char a[10];
 scanf("%s", a);
 printf("%p\n%p\n", &a, &a + 1);

}

Try to guess the difference.

It’s not 1!

 

 

 

 

 

 

 

aneesh@mybaby:~$ ./point 
1234567
0x7ffede46e3b0
0x7ffede46e3ba

Its 10.

How is that? Well, &a is a pointer to an array, its not a pointer to a character, its a pointer to the full array. Yes it still contains the memory address of the start of the array, which is the first character, but the type of the pointer is char (*)[10] which is very different from char *

to be continued…

Bashing everyone out of my college access point

I got my hands on a Atheros network card and tried to run a popular deauth attack on my college WiFi. You pretend to be a client on the network and then send a dauth packet to the WiFi access point. The WiFi AP, thinks that the client wants to de-authenticate, and removes it out of the network. Here’s how to do it:

sudo airodump-ng mon0 -w mypacket

Let it run for around 2-3 minutes.

Run noWifiForYou.sh

Watch your friends rage! :D

 

All my python code is so broken

I have coded in python2.7 for about 2 years and I had no clue that all of my code was so broken. The reason for this sudden realization is my use of input() in my code. I started using this little evil function when I was lazy and I didn’t want to type int(raw_input(“Some integer: “)). It seemed so innocent, just there to help you with your inputs, everyone told me that, but did we even know – it’s the most unsafe thing we could write in python. IT EVALUATES WHATEVER WE WRITE AS IF IT WERE PYTHON CODE! Python oh python, why would you name it input()? At the time I started using it, I think a lot of forum websites, while using, didn’t document its implications completely. But most of the links I visited today did warn me about this – IT EVALUATES TO CODE FOR GOD’S SAKE!

Here, see this:

import requests
from requests.auth import HTTPBasicAuth

def dont_print_me():
 print "Secrets"

userid = input("Input: ")

print userid

Screenshot from 2015-04-29 16:39:53

I can’t blame the function name completely, my lack of knowledge about the function was the major reason. Another lesson to why not to use any function in your code, without going through its documentation.

The code works perfectly fine in Python 3.