C Pointers arrays and WTF

Networking/Security Forums -> Programming and More

Author: MattALocation: Eastbourne + London PostPosted: Wed Feb 01, 2006 3:26 pm    Post subject: C Pointers arrays and WTF
    ----
OK I must have had a few too many puffs on the crackpipe this morning before i got on the train.

The exercise I was doing is now 'much better thank you', and I've radically changed the code but...... My code seriously broke my X server I'm guessing it was something to do with what was actually in the memory address's at the location I was pointing to, but on compiling and running the below code it printed out my X-windows config , which was not intended and screwed my .ICEauthority file...somehow.


Code:


#include <stdio.h>
char *array[12] = {"O", "X", "O", "X", "O", "X", "O", "X", "O", "X", "O", "X"};

main()
{
char (*ptr)[12], count;
for (count = 0; count <12 ; count++)
print_array(ptr++);
}

void print_array(char (*ptr)[12])
{
int *p, count;
p = (int *)ptr;
for (count = 0; count < 12; count++)
        printf("%s\n", *p++);
}

matta@slax:~/cprogramming/cin21days/csource/day15$ ./ex6
./ex6
(null)
CPLUS_INCLUDE_PATH=/usr/lib/qt/include:/usr/lib/qt/include
MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man:/usr/lib/java/man:/usr/lib/qt/doc/man
KDE_MULTIHEAD=false
HZ=100
HOSTNAME=slax.
SHELL=/bin/bash
TERM=xterm
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/matta/.gtkrc-2.0:/home/matta/.kde/share/config/gtkrc
GTK_RC_FILES=/etc/gtk/gtkrc:/home/matta/.gtkrc:/home/matta/.kde/share/config/gtkrc
GS_LIB=/home/matta/.fonts
MANPATH=/usr/local/man:/usr/man:/usr/X11R6/man:/usr/lib/java/man:/usr/lib/qt/doc/man
KDE_MULTIHEAD=false
HZ=100
HOSTNAME=slax.
SHELL=/bin/bash
TERM=xterm
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/matta/.gtkrc-2.0:/home/matta/.kde/share/config/gtkrc
GTK_RC_FILES=/etc/gtk/gtkrc:/home/matta/.gtkrc:/home/matta/.kde/share/config/gtkrc
GS_LIB=/home/matta/.fonts
WINDOWID=4194311
HUSHLOGIN=FALSE
OLDPWD=/home/matta
HOSTNAME=slax.
SHELL=/bin/bash
TERM=xterm
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/matta/.gtkrc-2.0:/home/matta/.kde/share/config/gtkrc
GTK_RC_FILES=/etc/gtk/gtkrc:/home/matta/.gtkrc:/home/matta/.kde/share/config/gtkrc
GS_LIB=/home/matta/.fonts
WINDOWID=4194311
HUSHLOGIN=FALSE
OLDPWD=/home/matta
QTDIR=/usr/lib/qt
LC_ALL=C
KDE_FULL_SESSION=true
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/matta/.gtkrc-2.0:/home/matta/.kde/share/config/gtkrc
GTK_RC_FILES=/etc/gtk/gtkrc:/home/matta/.gtkrc:/home/matta/.kde/share/config/gtkrc
GS_LIB=/home/matta/.fonts
WINDOWID=4194311
HUSHLOGIN=FALSE
OLDPWD=/home/matta
QTDIR=/usr/lib/qt
LC_ALL=C
KDE_FULL_SESSION=true
USER=matta
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:
GDK_USE_XFT=1
WINDOWID=4194311
HUSHLOGIN=FALSE
OLDPWD=/home/matta
QTDIR=/usr/lib/qt
LC_ALL=C
KDE_FULL_SESSION=true
USER=matta
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:
GDK_USE_XFT=1
KDEDIR=/opt/kde
SESSION_MANAGER=local/slax:/tmp/.ICE-unix/5843
T1LIB_CONFIG=/usr/share/t1lib/t1lib.config
QTDIR=/usr/lib/qt
LC_ALL=C
KDE_FULL_SESSION=true
USER=matta
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:
GDK_USE_XFT=1
KDEDIR=/opt/kde
SESSION_MANAGER=local/slax:/tmp/.ICE-unix/5843
T1LIB_CONFIG=/usr/share/t1lib/t1lib.config
KONSOLE_DCOP=DCOPRef(konsole-5873,konsole)
MINICOM=-c on
MAIL=/var/spool/mail/matta
USER=matta
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.bat=01;32:*.BAT=01;32:*.btm=01;32:*.BTM=01;32:*.cmd=01;32:*.CMD=01;32:*.com=01;32:*.COM=01;32:*.dll=01;32:*.DLL=01;32:*.exe=01;32:*.EXE=01;32:*.arj=01;31:*.bz2=01;31:*.deb=01;31:*.gz=01;31:*.lzh=01;31:*.rpm=01;31:*.tar=01;31:*.taz=01;31:*.tb2=01;31:*.tbz2=01;31:*.tbz=01;31:*.tgz=01;31:*.tz2=01;31:*.z=01;31:*.Z=01;31:*.zip=01;31:*.ZIP=01;31:*.zoo=01;31:*.asf=01;35:*.ASF=01;35:*.avi=01;35:*.AVI=01;35:*.bmp=01;35:*.BMP=01;35:*.flac=01;35:*.FLAC=01;35:*.gif=01;35:*.GIF=01;35:*.jpg=01;35:*.JPG=01;35:*.jpeg=01;35:*.JPEG=01;35:*.m2a=01;35:*.M2a=01;35:*.m2v=01;35:*.M2V=01;35:*.mov=01;35:*.MOV=01;35:*.mp3=01;35:*.MP3=01;35:*.mpeg=01;35:*.MPEG=01;35:*.mpg=01;35:*.MPG=01;35:*.ogg=01;35:*.OGG=01;35:*.ppm=01;35:*.rm=01;35:*.RM=01;35:*.tga=01;35:*.TGA=01;35:*.tif=01;35:*.TIF=01;35:*.wav=01;35:*.WAV=01;35:*.wmv=01;35:*.WMV=01;35:*.xbm=01;35:*.xpm=01;35:
GDK_USE_XFT=1
KDEDIR=/opt/kde
SESSION_MANAGER=local/slax:/tmp/.ICE-unix/5843
T1LIB_CONFIG=/usr/share/t1lib/t1lib.config
KONSOLE_DCOP=DCOPRef(konsole-5873,konsole)
MINICOM=-c on
MAIL=/var/spool/mail/matta
PATH=/home/matta/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/X11R6/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:.
LC_COLLATE=C
KONSOLE_DCOP_SESSION=DCOPRef(konsole-5873,session-1)
KDEDIR=/opt/kde
SESSION_MANAGER=local/slax:/tmp/.ICE-unix/5843
T1LIB_CONFIG=/usr/share/t1lib/t1lib.config
KONSOLE_DCOP=DCOPRef(konsole-5873,konsole)
MINICOM=-c on
MAIL=/var/spool/mail/matta
PATH=/home/matta/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/X11R6/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:.
LC_COLLATE=C
KONSOLE_DCOP_SESSION=DCOPRef(konsole-5873,session-1)
INPUTRC=/etc/inputrc
PWD=/home/matta/cprogramming/cin21days/csource/day15
JAVA_HOME=/usr/lib/java
KONSOLE_DCOP=DCOPRef(konsole-5873,konsole)
MINICOM=-c on
MAIL=/var/spool/mail/matta
PATH=/home/matta/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/X11R6/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:.
LC_COLLATE=C
KONSOLE_DCOP_SESSION=DCOPRef(konsole-5873,session-1)
INPUTRC=/etc/inputrc
PWD=/home/matta/cprogramming/cin21days/csource/day15
JAVA_HOME=/usr/lib/java
LANG=en_US
PS1=\u@\h:\w\$
PS2=>
PATH=/home/matta/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/games:/usr/X11R6/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:.
LC_COLLATE=C
KONSOLE_DCOP_SESSION=DCOPRef(konsole-5873,session-1)
INPUTRC=/etc/inputrc
PWD=/home/matta/cprogramming/cin21days/csource/day15
JAVA_HOME=/usr/lib/java
LANG=en_US
PS1=\u@\h:\w\$
PS2=>
SHLVL=5
HOME=/home/matta
XCURSOR_THEME=default
INPUTRC=/etc/inputrc
PWD=/home/matta/cprogramming/cin21days/csource/day15
JAVA_HOME=/usr/lib/java
LANG=en_US
PS1=\u@\h:\w\$
PS2=>
SHLVL=5
HOME=/home/matta
XCURSOR_THEME=default
GCONF_TMPDIR=/tmp
LS_OPTIONS= --color=auto -F -b -T 0
LESS=-M
LANG=en_US
PS1=\u@\h:\w\$
PS2=>
SHLVL=5
HOME=/home/matta
XCURSOR_THEME=default
GCONF_TMPDIR=/tmp
LS_OPTIONS= --color=auto -F -b -T 0
LESS=-M
LOGNAME=matta
VISUAL=mcedit
LESSOPEN=|lesspipe.sh %s

Author: HopperG PostPosted: Wed Feb 01, 2006 5:13 pm    Post subject:
    ----
Well the first thing I notice is that you never bring the array into the program at all, it is never referenced by the pointers to be printed out. So really you are just making a pointer, setting it to nothing, and printing out the nothing that you have set it to.

Code:
#include <stdio.h>
char *array = "OXOXOXOXOXOX";

void print_array(char *ptr)
{
        for(; *ptr; ptr++)
           printf("%c", *ptr);
        printf("\n");
}

int main()
{
   char *ptr = array;
   for (; *ptr; ptr++)
      print_array(ptr);
   return 0;
}


I think this is what you are trying to do. I've gotten rid of the 'count' variables as I think that will force you to understand a little better what it going on. The for loops will run until *ptr is 'false', which is just zero, so they will run until *ptr points to a null byte, i.e. the end of a string. There can be no other reason other than the end of the string that *ptr would point to a null byte, so this is a safe thing to do.

HTH

Author: MattALocation: Eastbourne + London PostPosted: Wed Feb 01, 2006 5:43 pm    Post subject:
    ----
it's where the hell did those environmental variables come from that bothers me and why did it crash X

Author: squidlyLocation: Umm.. I dont know.. somewhere PostPosted: Wed Feb 01, 2006 6:17 pm    Post subject:
    ----
Code:
 char *array[12] = {"O", "X", "O", "X", "O", "X", "O", "X", "O", "X", "O", "X"}; /* 1 */

main()
{
   char (*ptr)[12] /* 2 */   , count;
   for (count = 0; count <12 ; count++)
   print_array(ptr++);
/* no return */
}

void print_array(char (*ptr)[12])  /* ok passing a pointer to a pointer to an array no problem */
{

int *p, /* 3 */ count;
p = (int *)ptr; /* 3 */
for (count = 0; count < 12; count++)
        printf("%s", *p++);  /* ok you are printing out a string that is pointed to by a int */
}


Ok. From what it looks like you have several issues.
1. the global array of pointers. They are all pointing to the memory address of "0" and "X" being converted to short ints.
Also you didn't allocate memory for the array.
2. ok you declare an array of pointers that are pointing to non-initalized memory holding what ever was in the memory address before.
3. Why convert the address that is stored in *ptr[i] to an int? A char is all that is needed to hold a memory address.
Also by pointing back to the the ptr array in main() you have declared but didn't allocate or declare where its pointing 2.

Now back to your main message. Why did it print out your ENV variables? because that is where ptr was poining to from what was in memory that *ptr was pointing to.
Why did it crash out X?
Well it mostlikey tired to print memory to the screen that caused X to throw an error and die.

Author: hugoLocation: Netherlands, Europe PostPosted: Wed Feb 01, 2006 6:19 pm    Post subject:
    ----
MattA wrote:
it's where the hell did those environmental variables come from that bothers me and why did it crash X

Yeh well, undefined pointers have, well, undefined results.

See it as plain coincidence. Basically, it could as well have wiped your harddrive.

The pointer pointed to some memory-space where this stuff appeared. The printf( "%s\n", ... ) tells the program to print out stuff until it finds a NULL character, so an uninitialized pointer is bound to print out something when 'passed' to a %s -- thing is it may not always be what one expects. Smile



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group