The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Which Base Level Filesets needed by a specific program? cypher82 UNIX for Advanced & Expert Users 4 05-29-2008 08:07 AM
Help needed regarding c program dwgi32 High Level Programming 2 11-19-2007 10:44 AM
Wierd thing about FSs and VGs mhenryj AIX 4 11-13-2007 04:42 PM
Wierd results with awk amatheny Shell Programming and Scripting 2 11-01-2007 06:12 PM
Wierd Message???? lesstjm UNIX for Dummies Questions & Answers 6 01-04-2002 10:01 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-14-2007
karthikb23 karthikb23 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 18
Wierd C program. Help Needed

Hi,
Please see this:
When i make a declaration as:

char *i, j[15], *k;
and then do
sprintf( k, "print.sh %s", i );

the program works fine.

But when i change the declaration to:
char *i, *k;
and then do
sprintf( k, "print.sh %s", i );
I get a segmentation fault at the 'sprintf' statement.

The program works only when j is an array of 15. Nothing less/more

Please Note: variable j is just a dummy. I do absolutely nothing with it.
Also, i know using sprintf the way i have done is illegal. The pointer is not assigned to anything, prior to such a statement.

It may be silly but could someone please explain why this happens? Guess something to do with the way memory is allocated.

Thanks in advance!
  #2 (permalink)  
Old 11-14-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
You are trying to write to random memory as "k" has not been pointed at anything. The kernel can give you three answers...

(a) let you do it

(b) trap because you are writing to read only memory, eg the program image

(c) trap because you are writing to memory that has not been allocated to you
  #3 (permalink)  
Old 11-14-2007
karthikb23 karthikb23 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 18
Well, on the above lines, i could further deduce:

1. in one case, k was pointing to stdout (dont know how!), coz its memory contents were that of my printf statement, prior to the sprintf. (Surprising!)

2. In one case, k was pointing to the string part of my sprintf statement. Hence again, it is not illegal, and the program would work. (Seems possible).

However, this kind of valid but garbage initializations seem to happen only when variable j is declared. Else, the program halts by SEGV.

But i still believe that this is a special and one-off case. Guess the same code would not work on another machine. (Mine is Solaris).

Any comments/inputs/further insight anyone?
  #4 (permalink)  
Old 11-14-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
The stack that main is using won't be untouched virgin memory, it will have been used for subroutine calls by the program's prolog, ie crt0.o (or whatever) prior to main() being called.

The memory is truely in an unknown state, but I take your point about the contents being repeatable under certain conditions. I would call it a case of deja vu.
  #5 (permalink)  
Old 11-14-2007
karthikb23 karthikb23 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 18
agree . However in repeated runs, the program variables are allocated the same memory location.
Even on reading in a very large string (20 chars) and sending it to sprintf, it is surprising how there is no segmentation fault.

At some point, unless 'k' is pointing to stdout, the length of the memory should cause violation, and program should get SEGV right?

Also, if at all 'k' points to stdout, on doing a flush immediately, i should see the contents of 'k' right? coz it sould overwrite previous contents of stdout.
  #6 (permalink)  
Old 11-14-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by karthikb23 View Post
Also, if at all 'k' points to stdout, on doing a flush immediately, i should see the contents of 'k' right? coz it sould overwrite previous contents of stdout.
Try and see...?

Are you running it under gdb and see where things are really going?
  #7 (permalink)  
Old 11-14-2007
karthikb23 karthikb23 is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 18
i'll try n post t results
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0