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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-11-2005, 02:03 AM
|
#1
|
Member
Registered: Sep 2005
Posts: 48
Rep:
|
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.
|
|
|
09-11-2005, 05:50 AM
|
#2
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
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).
|
|
|
09-11-2005, 06:05 AM
|
#3
|
Member
Registered: Sep 2004
Posts: 273
Rep:
|
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 06:08 AM.
|
|
|
09-11-2005, 06:23 AM
|
#4
|
Member
Registered: Sep 2005
Posts: 48
Original Poster
Rep:
|
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.
|
|
|
09-11-2005, 06:44 AM
|
#5
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep: 
|
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..
|
|
|
All times are GMT -5. The time now is 03:45 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|