Thread: help with error

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    help with error

    I keep getting an error and I can't figure out why:

    "Unhandled exception at 0x004115f4 in polynomials.exe: 0xC0000005: Access violation writing location 0x00339000."

    When I step through that section of code, everything works fine.
    Am I missing something or could there be something further on that corrupts my program?
    Any help is appreciated!

    Code:
    char *a, *polynomial[MAX], ch;
    
    for(k = 0; k < number; k++) {
    			for(p = 0; ; p++) {
    				ch = fgetc(text);
    				*(a+p) = ch;           //error here
    				if(ch == '\n') {
    					break;
    				}
    			}
    			if(k < number - 1) {
    				polynomial[k] = a;
    				a = calloc(1,MAXPOLY*sizeof(char));
    				if(a == NULL) {
    					printf("Error involved in allocating space. Cannot complete task.");
    					exit(EXIT_FAILURE);
    				}
    			}
    		}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You write to a in the first for loop, but don't have anything to write to until the second.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    But how would that generate an error?
    I went through both loops, and everything worked.
    Then I ran the entire program, and it stopped there.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You never initialize your pointer "a" to anything. It has random data in it.

    You need to test for EOF after the fgetc().

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have a char* a. That's fine, it's a pointer. Currently it's pointing to Hoboken. And then, you try to write a letter to that memory address, and that doesn't work. You need to make a point at memory you own; conveniently, this is what calloc does (you need to calloc before you even think about touching a).

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    I initialized a*, but I'm still getting the same problem...

    I'm trying to read a file of polynomials where the first number tells you how many polynomials are in the file. From there I have to add, multiply, and evaluate them, but I have a general idea on how to do that. At the moment, I'm having problems with creating the array of pointers to separate polynomials.

    <ex>
    Code:
    3
    26x^5 + 5x^2 + 323x^3 + 50x^2 + 14x
    8x^3 + 11x^2 + 34x + 4
    21x^6 + 3x^5 + 4x + 4
    Code:
    #define MAXPOLY 100
    #define CHAR 1000
    #define NAME 50
    
    int main(void) {
    
    	FILE *text;
    	int number, exp, p, k, go;
    	float coeff;
    	char again = 'Y', newFile = 'Y', operation, ch;
    	char fileName[NAME], *polynomial[MAXPOLY], charArray[MAXPOLY], *token, *a;
    	struct poly *first = NULL, *sum, *multiply1, *multiply2, *newLink, *pointers;
    
    
    	while(newFile == 'Y' || newFile == 'y') {
    		printf("\nEnter the file where the polynomials are found: \n");
    		fscanf(stdin, " &#37;s", &fileName);
    		text = fopen(fileName, "r");
    /* If file failed to open, print error statement and ask for a different file. */
    		if(text == NULL) {
    			printf("Error involved in opening file. Cannot open %s\n", fileName);
    			continue;
    		}
    		fscanf(text, " %d", &number);
    /* Create an array of pointers. If error involved in allocating memory, terminate program. */
    		pointers = calloc(number, sizeof(struct poly));
    		if(pointers == NULL) {
    			printf("Error involved in allocating space. Cannot complete task.");
    			break;
    		}
    
    		a = &charArray[0];
    		ch = fgetc(text);
    		for(k = 0; k < number; k++) {
    			for(p = 0; p < MAXPOLY ; p++) {
    				ch = fgetc(text);
    				*(a+p) = ch;
    				if(ch == '\n') {
    					break;
    				}
    			}
    			if(k < number - 1) {
    				polynomial[k] = a;
    				a = calloc(1,MAXPOLY*sizeof(char));
    				if(a == NULL) {
    					printf("Error involved in allocating space. Cannot complete task.");
    					exit(EXIT_FAILURE);
    				}
    			}
    		}
    	return 0;
    }
    Last edited by arrgh; 04-16-2008 at 11:15 AM.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    For a prudent programming practice, consider modifying your FOR loop:

    Code:
    for(p = 0; p < MAXPOLY ; p++) {
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(p = 0; ; p++) {
    				ch = fgetc(text);
    				*(a+p) = ch;
    				if(ch == '\n') {
    					break;
    				}
    use fgets
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    don't you need the length of the string in order to use fgets? if so, how would you know the length of the string without reading each char?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, fgets() needs to be told the MAXIMUM of the string you want to be able to read - and it will read at most that much, or it finds a newline, whcihever happens first.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    	char *fileName[NAME], *polynomial[MAXPOLY], charArray[MAXPOLY], *token, *a;
    	struct poly *first = NULL, *sum, *multiply1, *multiply2, *newLink, *pointers;
    
    
    	while(newFile == 'Y' || newFile == 'y') {
    		printf("\nEnter the file where the polynomials are found: \n");
    		fscanf(stdin, " %s", fileName);
    Don't you ever get warnings? You should.
    You are defining an array of pointers and passing that to fscanf. Unfortunately, it won't warn here, but other places possibly.

    Read up on pointers and memory:
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    ok, I've changed the filename array to an array of type char and inserted an &. After compiling, there are 0 errors and 0 warnings, but the program still stops at
    Code:
    				*(a+p) = ch;
    I tried using fgets to write directly to the polynomial array of pointers, and the program compiled but still stopped midway.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by arrgh View Post
    ok, I've changed the filename array to an array of type char and inserted an &. After compiling, there are 0 errors and 0 warnings, but the program still stops at
    No no no. If you read the above article correctly, you should know that when passing an array, the address is passed, so in essence, a pointer is passed. If you add &, then it will pass the address of the pointer instead, which won't do you any good.

    It's better you use fgets instead, since the compiler can warn you if you do wrong then.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    so..
    Code:
    fgets(fileName, NAME, stdin);

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, check the manual for fgets again.
    It should be
    Code:
    fgets(buffer, sizeof(buffer), stdin)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM