linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* strange behavious
@ 2002-12-21  5:25 Mohammed Khalid Ansari
  2002-12-23  8:19 ` Glynn Clements
  0 siblings, 1 reply; 13+ messages in thread
From: Mohammed Khalid Ansari @ 2002-12-21  5:25 UTC (permalink / raw)
  To: linux c programming mailing list


Hi,

I was running a simple program to test two dimensional pointer. When I 
compiled it and executed, it received segmentaion fault, but when I 
checked it in debugger step by step, it went smoothly and produced the 
expected output. I did it many times and got the same behavious. Following 
is my program

###################

#include <stdio.h>

void parse (char **);
char	*words[] = {"mohammed", "khalid", "ansari"}; 
int main()
{
	int	num_words, i;
	
	parse (words);
	for (i=0; i<3; i++)
		printf ("%s\n", *(words+i));
	return 0;
}

void
parse (char *word[])
{
	char	*buf[] = {"abc", "bcd", "cde"};
	int	i=0;
	
	for (i=0; i<3; i++)
		strcpy (*word++, buf[i]);
}

##########

Please try yourself and see. What could be the problem.

with regards...

-- 

**************************************************************************

Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
Assistant Manager II                          (off) : 0091-022-2024641
National Centre for Software Technology   Fax       : 0091-022-2049573 
8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
Mumbai 400021.

Homepage : http://soochak.ncst.ernet.in/~khalid			  	  

**************************************************************************


^ permalink raw reply	[flat|nested] 13+ messages in thread
* RE: strange behavious
@ 2002-12-23  8:27 Alvarez Alberto-AALVARB1
  2002-12-24  5:15 ` Mohammed Khalid Ansari
  0 siblings, 1 reply; 13+ messages in thread
From: Alvarez Alberto-AALVARB1 @ 2002-12-23  8:27 UTC (permalink / raw)
  To: Mohammed Khalid Ansari, linux c programming mailing list

Hi,
	i'll try to answer your question. This may be not be an accurate answer, but i think it'll do.

	The way you've assigned values to 'words' is the reason of the failure. The allocation of memory takes place in program data space (the same place where code is), so you can't modify it.
	If you want to modify that array, you should dynamically allocate it. Something like this will work,

	char **words;
	words = calloc(3,sizeof(char **));
	words[0]=calloc( strlen("mohammed")+1,sizeof(char *));
	words[1]=calloc( strlen("khalid")+1,sizeof(char *));
	words[2]=calloc( strlen("ansari")+1,sizeof(char *));



	Why does it work when debugging it? I suppose it's due to the debugger behaviour. It isn't a "normal" execution, since the debugger has to control everything in the program. I think that every memory position of the program is allocated at debugger's heap, so it's writable.


Regards,

Alberto Alvarez Besada

Tlf.:        +34 914002155
e-mail.:   aalvarb1@motorola.com
            


> -----Original Message-----
> From: Mohammed Khalid Ansari [mailto:khalid@ncst.ernet.in]
> Sent: sabado, 21 de diciembre de 2002 6:25
> To: linux c programming mailing list
> Subject: strange behavious
> 
> 
> 
> Hi,
> 
> I was running a simple program to test two dimensional 
> pointer. When I 
> compiled it and executed, it received segmentaion fault, but when I 
> checked it in debugger step by step, it went smoothly and 
> produced the 
> expected output. I did it many times and got the same 
> behavious. Following 
> is my program
> 
> ###################
> 
> #include <stdio.h>
> 
> void parse (char **);
> char	*words[] = {"mohammed", "khalid", "ansari"}; 
> int main()
> {
> 	int	num_words, i;
> 	
> 	parse (words);
> 	for (i=0; i<3; i++)
> 		printf ("%s\n", *(words+i));
> 	return 0;
> }
> 
> void
> parse (char *word[])
> {
> 	char	*buf[] = {"abc", "bcd", "cde"};
> 	int	i=0;
> 	
> 	for (i=0; i<3; i++)
> 		strcpy (*word++, buf[i]);
> }
> 
> ##########
> 
> Please try yourself and see. What could be the problem.
> 
> with regards...
> 
> -- 
> 
> **************************************************************
> ************
> 
> Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
> Assistant Manager II                          (off) : 0091-022-2024641
> National Centre for Software Technology   Fax       : 
> 0091-022-2049573 
> 8th flr,Air India Build. Nariman Point,   E-Mail    : 
> khalid@ncst.ernet.in 	
> Mumbai 400021.
> 
> Homepage : http://soochak.ncst.ernet.in/~khalid		
> 	  	  
> 
> **************************************************************
> ************
> 
> -
> To unsubscribe from this list: send the line "unsubscribe 
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2002-12-25 21:39 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-21  5:25 strange behavious Mohammed Khalid Ansari
2002-12-23  8:19 ` Glynn Clements
2002-12-24  5:20   ` Mohammed Khalid Ansari
2002-12-25  9:50     ` Glynn Clements
     [not found]   ` <Pine.LNX.4.33.0212241046350.31854-100000@soochak.ncst.erne t.in>
2002-12-24 10:12     ` Stephen Satchell
2002-12-25 12:28       ` Mohammed Khalid Ansari
2002-12-25 13:58         ` Glynn Clements
     [not found]       ` <Pine.LNX.4.33.0212251757430.11856-100000@soochak.ncst.erne t.in>
2002-12-25 21:39         ` Stephen Satchell
  -- strict thread matches above, loose matches on Subject: below --
2002-12-23  8:27 Alvarez Alberto-AALVARB1
2002-12-24  5:15 ` Mohammed Khalid Ansari
2002-12-25  9:55   ` Glynn Clements
2002-12-25 12:31     ` Mohammed Khalid Ansari
2002-12-25 13:44       ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).