Thread: Increment int in structure

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    6

    Increment int in structure

    Hey all!
    I am quite a beginner and have a very beginner-ish question.

    I have a structure containing an int value.
    I also have a pointer to an instance of that structure and want to increase the int value.

    My code:
    Code:
    pointer->value = pointer->value + 1;
    or
    Code:
    pointer->value++;
    crashes the second time I execute it.
    Always.

    I am using the same code in another function, with the same pointer, there it works.
    The pointer value is not null, I know that for sure.
    The int value is 1 at the time of the crash.

    Is there a general reason why such a simple command could crash?
    If not I'll keep digging, but that thing has kept me occupied for two days now and I'm feeling slightly ridiculous.

    Thanks for any help and have a nice day!
    Last edited by fulumbler; 03-06-2017 at 09:28 AM. Reason: awful spelling

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    There is something else going on. You need to post your code.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    6
    Quote Originally Posted by algorism View Post
    There is something else going on. You need to post your code.
    You were absolutely right, thank you algorism!
    I'm afraid it might have something to do with the RAM limit and memory not being freed fast enough.

    I might post a related question if I can't resolve the issue myself, I should do it then in a new thread I guess, since the title doesn't really fit anymore.

    Thank you for your answer!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Memory corruption is an insidious error. The effects can show up far away from the cause. Just because the pointer is not NULL doesn't mean it's valid. It's probably being overwritten at some point (with a non-null value).

  5. #5
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by fulumbler View Post
    You were absolutely right, thank you algorism!
    I'm afraid it might have something to do with the RAM limit and memory not being freed fast enough.

    I might post a related question if I can't resolve the issue myself, I should do it then in a new thread I guess, since the title doesn't really fit anymore.

    Thank you for your answer!
    This from the book "The C Programming Language" by Kernighan and Ritchie. Page 120 - "A structure declaration that is not followed by a list of variables allocates no storage." Maybe this has something to do with the problem? You aren't allocating memory correctly, therefore the pointer is pointing to nothing, and this causes your program to crash.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Terrance View Post
    This from the book "The C Programming Language" by Kernighan and Ritchie. Page 120 - "A structure declaration that is not followed by a list of variables allocates no storage."
    I think you have completely misunderstood that statement. It doesn't say that a struct variable, if not explicitly defined, doesn't have space allocated for it. It says that if you don't follow a structure declaration/definition with a list of variables, it doesn't allocate any space and acts as a template for future use( the way most of us use it ).
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Pre and Post Increment !
    By thebenman in forum C Programming
    Replies: 25
    Last Post: 10-30-2014, 01:05 AM
  2. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  3. Increment Operator
    By theunderdog118 in forum C Programming
    Replies: 4
    Last Post: 05-31-2010, 11:00 AM
  4. Help with increment operator
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 11:06 PM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

Tags for this Thread