LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-11-2005, 01:03 AM   #1
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
oooooo so close to makeing this work.


ok heres the new porblem.

Code:
char*Catalogue = (char*)malloc(sizeof(char*)*10);
scanf("%s",&Catalogue);
if((myfile = fopen(&Catalogue,"r+"))==NULL)
{
   printf("\nFile could not be loaded.\n");
}
else
{
   printf("\nFile opened and has been read.\n");
   ReadCatalogue(myfile);
   fclose(myfile);
}
printf("hehe");
this is inside a loop whos condition is independant of the file useage, it is controled by what number a user selects earlier in the program and thus should not be affected by the opening of the file.

i input text.txt, which exists and has the following text in it.

Code:
reddas
red,da
25/04/1986
4.55
the output when this is run is:

Code:
File opened and has been read.
hehe11>
why does it close the program?

it should return to the top of the loop and ask me to select another option.

please help, i know that it calls ReadCatalogue, reads the stuff from the .txt file and returns to main, but it quits on me and that isnt acceptable.
 
Old 09-11-2005, 04:50 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
There is no loop in the code that you posted. Are you sure that the program isn't exiting normally?

A couple of style points:
1) Don't pass constant numbers directly into functions. #define a constant for 10 and use that.
2) You can probably find a constant for the maximum length of a filename in a standard include file; try using that instead
3) When reading a line into a fixed-length buffer, use fgets(&Catalogue,LENGTH,stdin); This way, you can't overflow the buffer if someone types too much in.

There are several ways to make a C program crash, but usually it comes down to accessing an area of memory that isn't allocated to you. This can mean overflowing a buffer, reading or writing past the end of an array, and so on.

If you are lucky then this problem causes a “segmentation fault” that makes the program crash immediately. Otherwise it makes the program crash a bit later on.

At a guess, the problem probably lies in the ReadCatalogue function. But that's not a given. You may get more information about where it crashes by changing the optimisation level of the program when you compile it. Also, make sure that the program compiles cleanly without any warnings, even with all warnings turned on (the -Wall flag if using gcc).
 
Old 09-11-2005, 05:05 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Code:
char*Catalogue = (char*)malloc(sizeof(char*)*10);
scanf("%s",&Catalogue);
I already told you to not cast what malloc() returns and don't allocate memory dynamically unless you have to.
sizeof(char*) is evaluated at compile time and is probably 4 on your system so you're allocating space for 40 chars
every time you run. Since the size is small (fits on the stack nicely) and is probably always the same unless you move
between 32-bit and 64-bit systems, I suggest allocating on the stack.

Next error is that you tell scanf to look for a string with "%s", but instead of passing it a pointer-to-char (Catalogue), you
pass it the address of Catalogue (effectively pointer-to-pointer-to-char), so no wonder it segfaults. Compile with all warnings turned on and the compiler would most likely complain that the format string doesn't match the variable. Errors like that are
begging for trouble.

Code:
char buffer[40];

scanf("%s", buffer); /* Fragile, we may type more than 39 chars */

printf("%s\n", buffer);
Much simpler.

Oh yeah, you probably want to skip scanf() altogether and use fgets() or something like rjlee suggested. When I code in C, the programs are usually not the kind that reads user input from the keyboard so I'm a bit weak in that department. But everything else I said holds true.

Last edited by Hivemind; 09-11-2005 at 05:08 AM.
 
Old 09-11-2005, 05:23 AM   #4
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Original Poster
Rep: Reputation: 15
you might be telling me stuff, but i dont understand it

EG

Code:
sizeof(char*) is evaluated at compile time and is probably 4 on your system so you're allocating space for 40 chars
every time you run
huh, were'd 40 come from?

and

Code:
Since the size is small (fits on the stack nicely) and is probably always the same unless you move
between 32-bit and 64-bit systems, I suggest allocating on the stack
WTF is a stack?

were 64 and 32 bits come into this and what are they?

imagine for a sec that i have NFI and that i have been using C for about 20 hours total and been looking on shite lecture notes the whole time.

laugh all you want at me, i dont care, but please dont get agro at me if i dont do something i dont understand.
 
Old 09-11-2005, 05:44 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally posted by Goblin_C_Noob
Code:
sizeof(char*) is evaluated at compile time and is probably 4 on your system so you're allocating space for 40 chars
every time you run
huh, were'd 40 come from?
sizeof() tells you how much memory (in bytes) the compiler allocates for each of the different types (int, char, etc). In your code, you're using it to find out how much is allocated for a char. He's saying on your system, 4 bytes are (probably) allocated for a char. Your code has "sizeof(char*)*10" in it and 10 * 4 is 40..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
makeing my own linux einstien Linux - General 5 02-22-2005 09:23 AM
error makeing k3b what do i do? glenn69 Linux - Newbie 1 09-12-2004 04:14 PM
cannot get my logitech mx 700 to work (very close tho!) dave_blob Linux - Hardware 6 02-11-2004 01:08 AM
I want some help on makeing my own Linux distro Posty Linux - Distributions 14 10-21-2003 10:12 PM
SAMBA and W2k still does not work, but I am so close PLS HELP cevjr Linux - Networking 5 08-06-2003 11:39 AM


All times are GMT -5. The time now is 09:52 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration