Thread: Quick C Question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    Quick C Question

    I have a C homework to do (I am a C++ guy) and the question asks us how to fix the following code the crashes:

    int *a = 0;
    *a = 5;

    Fix it using dynamic memory management or static variables.

    Now I have written programs that are 10,000 times longer. I am lost at what they are trying to do with the given code. So please help!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    The code is attempting to store the value 5 into the variable a.

    That should be enough for someone who has "written programs that are 10,000 times longer" .

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    OK, I thought that for a second...i just sure as heck hope thats what my teacher is trying to show. It's simply two lines of retarded code...at least to make a mistake like that. So...I was just making sure...thanks!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >(I am a C++ guy)
    Those two lines are C++ code too. But if you never studied pointers in C++, you wouldn't know what they do.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    Tbh, I have always known pointers from when I learned them, but via school we have always used Java, so I haven't worked that much with pointers. I used an array to fix the code, but I was not sure how dynamic memory management played into the code, but I found static code that worked.

    To make the code not crash was not hard, but to make the code do what the person who wrote it wanted needed that answer from Kennedy, so thanks!
    Last edited by vgame64; 09-27-2006 at 04:41 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vgame64
    I have a C homework to do (I am a C++ guy) and the question asks us how to fix the following code the crashes:

    int *a = 0;
    *a = 5;

    Fix it using dynamic memory management or static variables.
    To clarify the issue, for people who have not written programs 10000 times longer:

    The reason the code is incorrect is because the pointer doesn't point to anything specific. That's bad. Make it point to a variable first.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Tbh, I have always known pointers from when I learned them
    Admittedly, pointers aren't used that much in modern C++ anymore. They are used behind the scenes of course, but using vectors, strings, and such, you don't need to be concerned with them unless you're doing something fancy.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mmmm, 10K lines of C++ without a single pointer?

    "For the millionth time, stop exaggerating!"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    A bit off topic, but you must always initalize your pointers, if you do not have a variable to assign the pointer to, please always assign it to NULL. This is one of the safest things you can do in C,C++ programming, as a dangling or lost pointer is pointing to an unknown part of computer memory which is very dangerous

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    The code should be
    Code:
    int *a = malloc(sizeof(int)); /*malloc() allocates required memory for one int and returns its address*/
    *a = 5;
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    static int b = 0;
    int *a = &b;
    *a = 5;
    printf("b=%d\n", *a);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM