Thread: Uninitialised value was created by a stack allocation --geting desperate!

  1. #16
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by laserlight View Post
    ..
    This demonstrates yet again that the practice of having pointer typedefs for non-opaque pointer types is a poor practice.
    I second that! Unfortunately, it is quite common
    "Talk is cheap, show me the code" - Linus Torvalds

  2. #17
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    It is very convenient. The fact that I make mistakes in this approach is due to my perfectionism (I scatter consts everywhere, necessary and not necessary). This does not mean that this technique itself is wrong.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  3. #18
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It is very convenient.
    O_o

    How? You save a couple or three characters?

    This does not mean that this technique itself is wrong.
    The fact that you can't follow your aggressive `const' strategy, one which I too apply, without still further `typedef' pointers, a `const' flavor, should give you a good understanding of why the technique really is bad practice.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #19
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This demonstrates yet again that the practice of having pointer typedefs for non-opaque pointer types is a poor practice.
    O_o

    The other valid case: pointers to functions when used as function parameters.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #20
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by zub
    Code:
        Point new = NEW(Point);
        assert(new);
    This is not using assert correctlty.

    The idea of assert is to help you when developing the code - When you release the code, you are supposed to define NDEBUG before the <assert.h> and all of the asserts are ignored.

    When your code uses asserts to check if malloc was successful, you loose the checks when NDEBUG is defined.

    Also, exiting a program on a failed malloc is also very poor practice: Imagine that your user has spent hours making a document, all of the sudden they get a message saying, "Malloc Failed" and the program shuts down. The user is left confused and (rightfully) angry. What you should do is tell the user that the system is short on memory and ask them to close some other programs that they might be using.
    Fact - Beethoven wrote his first symphony in C

  6. #21
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by zub
    Code:
    #define NEW(x) ((x)malloc(sizeof(struct x)))
    And why are you casting your malloc in your macro?

    Also, laserlight has asked you not to spoonfeed answers out.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 08-03-2014, 10:57 PM
  2. Memory Allocation in Stack
    By aakashjohari in forum C Programming
    Replies: 9
    Last Post: 01-20-2011, 01:45 PM
  3. memory allocation on the stack
    By R.Stiltskin in forum C++ Programming
    Replies: 7
    Last Post: 04-01-2009, 12:18 PM
  4. stack-based allocation
    By eXodus31337 in forum C++ Programming
    Replies: 10
    Last Post: 01-05-2009, 11:03 PM
  5. memory allocation from stack and heap ?
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 12:37 AM