Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > pointers and bus errors in C

pointers and bus errors in C
Thread Tools
Mac Enthusiast
Join Date: Sep 2000
Location: France
Status: Offline
Reply With Quote
Jul 22, 2002, 01:28 PM
 
Hi everyone
I'm currently learning the C language, but I ran across problems in my programs when using pointers
I want to write a program in C that takes 2 files as its arguments, compares them, and return on stdout every line that differs from one file to the other
Here's the beginning of the code :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include &lt;stdio.h&gt;
#define MAX 100

int main(int argc, char *argv[])
{
char *l;
char *m;
FILE *fp1;
FILE *fp2;

if (argc != 3) {
printf(&quot;stuff&quot;
return 0;
}

fp1=fopen(*++argv,&quot;r&quot;
fp2=fopen(*++argv,&quot;r&quot;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Now I noticed that the next line, which is :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">fgets(l, MAX, fp1);</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">generates a bus error.
I suppose this is a problem with the (char *) pointer, but I don't really get what's going wrong...
Anyone can help ?
     
Forum Regular
Join Date: Jul 2002
Location: Elkton, MD
Status: Offline
Reply With Quote
Jul 22, 2002, 02:36 PM
 
I see a few potential problems with your code...

1) No checks to make sure the files were actually opened (fp1 & fp2).
2) The *++argv thing, while it may work, is too complicated: change to argv[1] and argv[2].
3) Finally, the source of your problem is the fgets line. I'm not 100% sure fgets will allocate memory for you so I'm pretty sure you have to allocate space before calling fgets().

You can try the following, change:

char *l;

to

char l[1024];

Then change your fgets() line to:

fgets( &l, MAX, fp1 )

Unlike other languages C does not set new variables to a default value (like Java for instance). When your program starts *l could be pointing anywhere in memory...

Damosan
     
Axel  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: France
Status: Offline
Reply With Quote
Jul 22, 2002, 03:43 PM
 
Your solution works if I use the line :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">fgets(l, MAX, fp1);</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">That's great, but I still don't get these problems with pointers and memory allocation...
     
Junior Member
Join Date: Feb 2002
Status: Offline
Reply With Quote
Jul 22, 2002, 06:03 PM
 
For a start,
char l[MAX];
makes more sense than
char l[1024];

Anyway, when you call fgets(), you pass a pointer to the memory where it will put the string. In the original version, you were passing an uninitialized pointer which doesn't point anywhere meaningful. fgets() attempted to access the invalid memory, causing a crash. Arrays are automatically allocated, so they don't start out as invalid.
self = [[JeffBinder alloc] init];
     
Axel  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: France
Status: Offline
Reply With Quote
Jul 23, 2002, 03:25 AM
 
OK I think I get it now...
I have another problem which is similar :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">char *l=&quot;Axel&quot;;
*l='z';</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">(I know the program seems useless, but I'm just trying to learn the basics )
I thought that the pointer "l" pointed to the first character in the string "Axel". That means that the second line should change "A" to 'z'
I get the same Bus Error instead
Could any of you explain this behavior ?
Thx in advance for your patience
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 23, 2002, 05:41 AM
 
EDIT: D'oh. I totally misread what you were trying to do and didn't get. Sorry. Anyway, Tobias got it right, so I'll just leave it with his.

(Oy, how embarrassing writing a detailed response to a question someone didn't ask.)

<small>[ 07-23-2002, 09:29 AM: Message edited by: Chuckit ]</small>
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Jul 23, 2002, 05:46 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Axel:
<strong>OK I think I get it now...
I have another problem which is similar :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">char *l=&quot;Axel&quot;;
*l='z';</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">(I know the program seems useless, but I'm just trying to learn the basics )
I thought that the pointer "l" pointed to the first character in the string "Axel". That means that the second line should change "A" to 'z'
I get the same Bus Error instead
Could any of you explain this behavior ?
Thx in advance for your patience </strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">char *l="Axel"; makes l point to a string constant, that cannot be changed. Instead to one of the following:

char l[] = "Axel"; /* create a char array initialized to ['A','x','e','l','\0']. This initialization will allocate exactly five bytes of memory for l (the size of the string you init it with), so keep that in mind. Since arrays and pointers really are the same you can use l as an char* */

char *l = malloc(strlen("Axel") + 1);
strcpy(l, "Axel");
...
free(l);
/* manually allocate memory for the string, put the contents in, use it, and manually free the memory */

This stuff can be a bit hard to learn and remember when you start with C. I'm so glad I've moved on to ObjC, where string manipulation (and so much else) is a breeze. But then of course, you'll need some basic C skills to get there, so hang in there. Good luck!

/Tobias
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Jul 23, 2002, 06:13 AM
 
double post <img border="0" title="" alt="[Frown]" src="frown.gif" />

<small>[ 07-23-2002, 07:14 AM: Message edited by: tobli ]</small>
     
Axel  (op)
Mac Enthusiast
Join Date: Sep 2000
Location: France
Status: Offline
Reply With Quote
Jul 23, 2002, 09:10 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by tobli:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Axel:
<strong>OK I think I get it now...
I have another problem which is similar :
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">char *l=&quot;Axel&quot;;
*l='z';</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">(I know the program seems useless, but I'm just trying to learn the basics )
I thought that the pointer "l" pointed to the first character in the string "Axel". That means that the second line should change "A" to 'z'
I get the same Bus Error instead
Could any of you explain this behavior ?
Thx in advance for your patience </strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">char *l="Axel"; makes l point to a string constant, that cannot be changed. Instead to one of the following:

char l[] = "Axel"; /* create a char array initialized to ['A','x','e','l','\0']. This initialization will allocate exactly five bytes of memory for l (the size of the string you init it with), so keep that in mind. Since arrays and pointers really are the same you can use l as an char* */

char *l = malloc(strlen("Axel") + 1);
strcpy(l, "Axel");
...
free(l);
/* manually allocate memory for the string, put the contents in, use it, and manually free the memory */

This stuff can be a bit hard to learn and remember when you start with C. I'm so glad I've moved on to ObjC, where string manipulation (and so much else) is a breeze. But then of course, you'll need some basic C skills to get there, so hang in there. Good luck!

/Tobias</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thx a lot for this great explanation
Actually I'm also gradually discovering ObjC (through the columns at macdevcenter.com), but as you said, I have to learn the basics first.
     
Forum Regular
Join Date: Jul 2002
Location: Elkton, MD
Status: Offline
Reply With Quote
Jul 24, 2002, 08:58 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thx a lot for this great explanation
Actually I'm also gradually discovering ObjC (through the columns at macdevcenter.com), but as you said, I have to learn the basics first.[/QB]</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I may suggest just starting with ObjC. While it's true that there are a TON of free sources for learning C there are also quite a few C idioms that you will see which may not directly apply to ObjC. That doesn't mean the idioms arn't useful in ObjC it's just that they might not be the ObjC Way of Doing Things(tm).

There is a LARGE (200 or so pages) ObjC tutorial on Apple's site in PDF format. I wish I could buy a printed copy as I hate printing stuffy myself.

Damosan
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 25, 2002, 01:22 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by damosan:
<strong> </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thx a lot for this great explanation
Actually I'm also gradually discovering ObjC (through the columns at macdevcenter.com), but as you said, I have to learn the basics first.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I may suggest just starting with ObjC. While it's true that there are a TON of free sources for learning C there are also quite a few C idioms that you will see which may not directly apply to ObjC. That doesn't mean the idioms arn't useful in ObjC it's just that they might not be the ObjC Way of Doing Things(tm).[/QB]</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I would disagree. While there may be a few things you'll pick up in C that you'll find new ways of doing, there are a lot more that you'll miss if you just jump into Objective-C. There are no books I've heard of that give you all the information you need to be proficient in Objective-C. They all (Apple's Objective-C documentation above all) assume some degree of proficiency in C to begin with. If you just jump into Objective-C as you're suggesting, you'll probably never learn these fine points of pointer initialization and arithmetic, or how bitwise operators work, or a million other small but potentially very important things.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Forum Regular
Join Date: Jul 2002
Location: Elkton, MD
Status: Offline
Reply With Quote
Jul 25, 2002, 04:35 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">I would disagree.</font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I stand corrected. I just reviewed the PDF I was referring to and all it seems to be is a high-level approach to ObjC.

That being the case I'd think learning C 1st might be a very good thing indeed. <img border="0" title="" alt="[Wink]" src="wink.gif" />
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 10:06 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2