Can we have a guide to allocating memory?


  • 0
    P

    When coding in C, in code that I can free externally without leaks (tested with valgrind), I have been receiving several annoying misaligned bytes messages (indicating that e.g. something tried to use array access with pointers or vice versa, that sort of thing) or errors allocating memory. I do habitually adopt conventions like freeing my own memory and returning null when a malloc call fails, should I not be doing this? Would it be possible to provide some guidelines for test developers as far as providing information to coders so we know exactly how memory will be freed? This happens most often with, for instance, linked lists or 2-D arrays, in which most coders will be naturally inclined to allocate multiple blocks of memory. If we should allocate a single block of memory to hold everything and try to index within that block, please let us know. Otherwise, generally, the way things are this breaks testing code on our own compilers, and introduces problems completely unrelated to the computer science problems we are trying to solve that are much, much more complicated.

    Some template code as far as allocating memory, or even methods we can call such as

    newObject()
    object->addRow(varArgsInteger 1, varArgsInteger 2, ...)

    would be very awesome. I would be happy writing a

    object->free()

    method on my own for solutions if you want to shift responsibility back to me, just template this out please.


  • 0
    P

    @pennell Sorry I did not mean to add this to interview questions :/ Could this be moved to the OJ section?


  • 0

    @pennell No problem, I have just moved it to Comments & Feedback.


  • 0

    @pennell I'm not sure what you meant, did you mean the code you're able to get Accepted in LeetCode is having issues with your local testing? The main purpose of LeetCode is to allow you to focus on the algorithm itself, not worry about language specific things such as freeing memory properly, which you can assume whatever memory you allocated in your function will be freed properly after function return.


  • 0

    Better show some actual solution(s) demonstrating the issue(s) so this can be properly looked into.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.