Thread: Access Violation

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Graham Aker
    In that case, what should I use in place of strtok? Because Atoi wasn't very helpful, and I need to split that string somehow.
    You could use strtok() in combination with atoi(), or you could use sscanf(), or you could write your own integer string parsing code. (Ah, but if this integer is always a single digit, then tabstop's idea applies).

    Quote Originally Posted by Graham Aker
    Also, DevC++ now tells me that "*emp->digits = (int*)malloc(sizeof(int)*arraysize);" makes an integer from a pointer without a cast. What does that mean?
    emp is a pointer to struct integer. emp->digits is a pointer to int. *emp->digits is an integer. What you probably wanted to write was:
    Code:
    emp->digits = malloc(sizeof(*emp->digits) * arraysize);
    The sizeof(*emp->digits) is preferred to sizeof(int) since it means that you will not have to change that in the event that the type of *emp->digits changes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Aright, that cleared up a couple problems, but DevC++ still tells me that "emp[j++]->digits = numbr[i] - '0';" has an invalid type argument of '->'. What does that mean?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Graham Aker
    DevC++ still tells me that "emp[j++]->digits = numbr[i] - '0';" has an invalid type argument of '->'. What does that mean?
    emp is a pointer to struct integer. emp[j++] is a struct integer. What you want is *emp[j++].digits
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So at some point, you should probably think for two seconds. Do you want to write successive numbers in different emp structures? Do you even have an array of emp structures to start with? No? So emp[j++] is soooooo not going to work.

    Edit: For that matter, you don't even have one emp structure. Why use malloc to create your structure? Just declare it and be done with it. But if you do have to use malloc, you do need to allocate enough space for that structure (sizeof(int) is NOT enough space).

  5. #20
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by tabstop View Post
    So at some point, you should probably think for two seconds. Do you want to write successive numbers in different emp structures? Do you even have an array of emp structures to start with? No? So emp[j++] is soooooo not going to work.

    Edit: For that matter, you don't even have one emp structure. Why use malloc to create your structure? Just declare it and be done with it. But if you do have to use malloc, you do need to allocate enough space for that structure (sizeof(int) is NOT enough space).
    What I want to do is write successive numbers into the digits array, then use the different emp structures to later add, subtract, or compare the different strings with one another.

    However, apparently I'm doing it wrong, since I keep crashingmy program before it can even get off the ground...

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Graham Aker View Post
    What I want to do is write successive numbers into the digits array
    Exactly. The digits array in the same emp. So don't try to change emp with each digit! It can only lead to heartache.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Graham Aker
    What I want to do is write successive numbers into the digits array, then use the different emp structures to later add, subtract, or compare the different strings with one another.
    So, at the moment you are dealing with one struct integer? You should know then that if emp->digit is a pointer to an int, *emp->digit is an int. Now, *(emp->digit + n) is equivalent to emp->digit[n]. Therefore, what you really wanted to write is emp->digit[j++].

    Quote Originally Posted by Graham Aker
    However, apparently I'm doing it wrong, since I keep crashingmy program before it can even get off the ground...
    tabstop referred to this line:
    Code:
    struct integer *num = (struct integer *)malloc(sizeof(int));
    As I noted, one way to get the correct size is to use the expression being declared, i.e.,
    Code:
    struct integer *num = malloc(sizeof(*num));
    But since at this point you just want to work with a struct integer to get the rest of the code right, you might as well create it on the stack:
    Code:
    struct integer num;
    Consequently, you would not need to free the struct integer pointer (which you failed to do anyway). You would pass the address of num to readnum().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by laserlight View Post
    So, at the moment you are dealing with one struct integer? You should know then that if emp->digit is a pointer to an int, *emp->digit is an int. Now, *(emp->digit + n) is equivalent to emp->digit[n]. Therefore, what you really wanted to write is emp->digit[j++].
    That worked... Sorta. It's compiling now, and I've got one line on-screen, but it still is crashing somewhere...

    Quote Originally Posted by laserlight View Post
    tabstop referred to this line:
    Code:
    struct integer *num = (struct integer *)malloc(sizeof(int));
    As I noted, one way to get the correct size is to use the expression being declared, i.e.,
    Code:
    struct integer *num = malloc(sizeof(*num));
    But since at this point you just want to work with a struct integer to get the rest of the code right, you might as well create it on the stack:
    Code:
    struct integer num;
    Consequently, you would not need to free the struct integer pointer (which you failed to do anyway). You would pass the address of num to readnum().
    I probably implemented it wrong, but the third snippet broke the program- it wouldn't even compile.

    The second, however, will still allow the program to compile.

  9. #24
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    emp is a pointer to struct integer. emp[j++] is a struct integer. What you want is *emp[j++].digits
    I'm sure - he do not want this.
    emp is pointer to one and only struct.

    He has no array of struct pointers, no could you apply [] operator to strcut in C

    what he probably want is closer to
    Code:
    emp->digits[j] = ...
    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

  10. #25
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Quote Originally Posted by vart View Post
    I'm sure - he do not want this.
    emp is pointer to one and only struct.

    He has no array of struct pointers, no could you apply [] operator to strcut in C

    what he probably want is closer to
    Code:
    emp->digits[j] = ...
    Yeah, I've got that now already. A new problem popped up somewhere else...

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You did read the part under it about passing &num to readnum instead of just num?

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Graham Aker
    I probably implemented it wrong, but the third snippet broke the program- it wouldn't even compile.
    That is probably because you did not take the address of the struct integer.

    Quote Originally Posted by vart
    I'm sure - he do not want this.
    emp is pointer to one and only struct.
    tabstop and I have addressed this in posts #19 and #22.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    @Tabstop:...Whoops. I forgot that.

    EDIT: Er, wait, now it's not even reading off the first line...

  14. #29
    Confused College Student Graham Aker's Avatar
    Join Date
    Jan 2009
    Posts
    62
    Okay, now it's telling me I have an Access Violation in the print function, which is right here:
    Code:
          printf("\n The arrays are: ");
          for (k=0; k<stringnum; k++){
               for (i=0; i<j; i++)
                   printf ("%d ", num[k].digits[i]);
          printf("\n");
          }
    The problem line is "printf ("%d ", num[k].digits[i]);", and for some odd reason I think the problem involves that [k] bit there...

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Graham Aker
    The problem line is "printf ("%d ", num[k].digits[i]);", and for some odd reason I think the problem involves that [k] bit there...
    num is a pointer to exactly one struct integer, right? Then num[k] is valid if and only if k == 0, since that would be equivalent to *(num+0) which is *num.

    You might want to post your updated code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  4. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM

Tags for this Thread