Strange thing about malloc()

Discussion in 'C Programming' started by Ronny Mandal, Apr 19, 2005.

  1. Ronny Mandal

    Ronny Mandal Guest

    Hi!

    I am writing a program that'll read binary integers from a file, put them
    into a dynamic array. THe array is first declared

    int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.

    When N is odd, the program will work just fine, however when N is even, it
    will not work.

    Is it just a Win32-issue, or a bug?

    --

    Regards,

    Ronny Mandal


    This e-mail and any attachment are confidential and may be privileged or
    otherwise protected from disclosure. It is solely intended for the person(s)
    named above. If you are not the intended recipient, any reading, use,
    disclosure, copying or distribution of all or parts of this e-mail or
    associated attachments is strictly prohibited. If you are not an intended
    recipient, please notify the sender immediately by replying to this message
    or by telephone and delete this e-mail and any attachments permanently from
    your system.
    Ronny Mandal, Apr 19, 2005
    #1
    1. Advertising

  2. >This e-mail and any attachment are confidential and may be privileged or
    >otherwise protected from disclosure. It is solely intended for the person(s)
    >named above. If you are not the intended recipient, any reading, use,
    >disclosure, copying or distribution of all or parts of this e-mail or
    >associated attachments is strictly prohibited. If you are not an intended
    >recipient, please notify the sender immediately by replying to this message
    >or by telephone and delete this e-mail and any attachments permanently from
    >your system.


    It's absolutely ridiculous to put this on a worldwide USENET posting.
    Don't threaten people you want to help you.

    >I am writing a program that'll read binary integers from a file, put them
    >into a dynamic array. THe array is first declared
    >
    >int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >
    >When N is odd, the program will work just fine, however when N is even, it
    >will not work.


    Describe how you can tell the difference between "works" and "doesn't work".
    Some code would be nice.

    I suspect that you are running over the end of the array, and that
    you can get away with it with an odd number (possibly because malloc()
    rounds up the size), but not with an even number (when it might not
    round up). But there's no way to tell without code.

    Gordon L. Burditt
    Gordon Burditt, Apr 19, 2005
    #2
    1. Advertising

  3. Ronny Mandal wrote on 19/04/05 :
    > I am writing a program that'll read binary integers from a file, put them
    > into a dynamic array. THe array is first declared
    >
    > int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >
    > When N is odd, the program will work just fine, however when N is even, it
    > will not work.
    >
    > Is it just a Win32-issue, or a bug?


    Hard to say. What do you meant by "it will not work." ? Post a
    compilable snippet that exposes the problem with a description of the
    environment (required inputs, expected outputs...)

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "C is a sharp tool"
    Emmanuel Delahaye, Apr 19, 2005
    #3
  4. Ronny Mandal

    Eric Sosman Guest

    Ronny Mandal wrote:
    > Hi!
    >
    > I am writing a program that'll read binary integers from a file, put them
    > into a dynamic array. THe array is first declared
    >
    > int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >
    > When N is odd, the program will work just fine, however when N is even, it
    > will not work.
    >
    > Is it just a Win32-issue, or a bug?


    The latter. My crystal ball tells me your error
    is on line 42.

    "Doctor, it hurts!"

    "What hurts?"

    "What do you think you're doing, snooping in my
    personal affairs?"

    --
    Eric Sosman, Apr 19, 2005
    #4
  5. Ronny Mandal

    Ronny Mandal Guest


    > It's absolutely ridiculous to put this on a worldwide USENET posting.


    No it is not. I've noticed stranger things about c-releases, e.g in
    non-stable releases that is almost like "doesn't work when the moon is
    full", which other people from the USENET have had issues with. And then,
    because of the USENET, the inquirer receives help.

    > Don't threaten people you want to help you.
    >
    >>I am writing a program that'll read binary integers from a file, put them
    >>into a dynamic array. THe array is first declared
    >>
    >>int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >>
    >>When N is odd, the program will work just fine, however when N is even, it
    >>will not work.

    >
    > Describe how you can tell the difference between "works" and "doesn't
    > work".
    > Some code would be nice.


    Well, in general people on USENET is not interested in posted code. But
    after trying to isolate the problem into a few lines of code, it still
    behaves strange.

    >
    > I suspect that you are running over the end of the array, and that
    > you can get away with it with an odd number (possibly because malloc()
    > rounds up the size), but not with an even number (when it might not
    > round up). But there's no way to tell without code.


    This seems like a plausible answer, regarding the "you can get away with it
    with an odd number". I've never read anyplace about how malloc works
    "internally", but maybe it is written in the FAQ. But since the FAQ is
    really extensive, I sometimes ask people from ther USENET.
    >
    > Gordon L. Burditt


    --

    Ronny Mandal
    Ronny Mandal, Apr 19, 2005
    #5
  6. Ronny Mandal

    Artie Gold Guest

    Gordon Burditt wrote:
    >>This e-mail and any attachment are confidential and may be privileged or
    >>otherwise protected from disclosure. It is solely intended for the person(s)
    >>named above. If you are not the intended recipient, any reading, use,
    >>disclosure, copying or distribution of all or parts of this e-mail or
    >>associated attachments is strictly prohibited. If you are not an intended
    >>recipient, please notify the sender immediately by replying to this message
    >>or by telephone and delete this e-mail and any attachments permanently from
    >>your system.

    >
    >
    > It's absolutely ridiculous to put this on a worldwide USENET posting.
    > Don't threaten people you want to help you.
    >


    That part of the sig is likely put there by the server through which the
    message was posted. It's probably not the OP's fault.

    --ag


    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Apr 19, 2005
    #6
  7. Ronny Mandal

    Artie Gold Guest

    Ronny Mandal wrote:
    >>It's absolutely ridiculous to put this on a worldwide USENET posting.

    >
    >
    > No it is not. I've noticed stranger things about c-releases, e.g in
    > non-stable releases that is almost like "doesn't work when the moon is
    > full", which other people from the USENET have had issues with. And then,
    > because of the USENET, the inquirer receives help.


    He was referring to the stuff at the botom of your sig.
    >
    >
    >>Don't threaten people you want to help you.
    >>
    >>
    >>>I am writing a program that'll read binary integers from a file, put them
    >>>into a dynamic array. THe array is first declared
    >>>
    >>>int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >>>
    >>>When N is odd, the program will work just fine, however when N is even, it
    >>>will not work.

    >>
    >>Describe how you can tell the difference between "works" and "doesn't
    >>work".
    >>Some code would be nice.

    >
    >
    > Well, in general people on USENET is not interested in posted code. But
    > after trying to isolate the problem into a few lines of code, it still
    > behaves strange.
    >

    Not true. Best is to post the *minimal* compilable code that exhibits
    the behavior that made you post in the first place.
    >
    >>I suspect that you are running over the end of the array, and that
    >>you can get away with it with an odd number (possibly because malloc()
    >>rounds up the size), but not with an even number (when it might not
    >>round up). But there's no way to tell without code.

    >
    >
    > This seems like a plausible answer, regarding the "you can get away with it
    > with an odd number". I've never read anyplace about how malloc works
    > "internally", but maybe it is written in the FAQ. But since the FAQ is
    > really extensive, I sometimes ask people from ther USENET.
    >


    Understood. When it comes to malloc(), however, its internal workings
    should be generally transparent. As has been mentioned above, an overrun
    in some other piece of dynamically allocated memory is likely the culprit.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Apr 19, 2005
    #7
  8. Jonathan Bartlett, Apr 19, 2005
    #8
  9. "Ronny Mandal" <> writes:
    > I am writing a program that'll read binary integers from a file, put them
    > into a dynamic array. THe array is first declared
    >
    > int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >
    > When N is odd, the program will work just fine, however when N is even, it
    > will not work.
    >
    > Is it just a Win32-issue, or a bug?


    You have a typo on line 372 of main.c.

    I'm only guessing, but that's all I can do with the information you've
    provided. What is your program attempting to do? How exactly does it
    not work?

    Can you post a small complete program that exhibits the problem?

    (If it's a Win32 issue, we can't help you here.)

    > This e-mail and any attachment are confidential and may be privileged or
    > otherwise protected from disclosure.

    [snip]

    It's not an e-mail, it's a Usenet posting. I suggest you drop the
    meaningless disclaimer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 19, 2005
    #9
  10. In article <>,
    Artie Gold <> wrote:

    >That part of the sig is likely put there by the server through which the
    >message was posted. It's probably not the OP's fault.


    Quite so, but if we don't complain to people who use such servers, how
    are they ever going to be replaced?

    If, by choice, you use a server that adds this sort of thing to your
    message, it makes you look silly. If you work for a company that puts
    it on all your outgoing messages, it makes your company look as if
    they don't understand the net, and are probably the sort who spend
    more money on lawyers than products.

    -- Richard
    Richard Tobin, Apr 19, 2005
    #10
  11. Ronny Mandal

    Flash Gordon Guest

    Ronny Mandal wrote:
    > Hi!
    >
    > I am writing a program that'll read binary integers from a file, put them
    > into a dynamic array. THe array is first declared
    >
    > int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >
    > When N is odd, the program will work just fine, however when N is even, it
    > will not work.
    >
    > Is it just a Win32-issue, or a bug?


    There is almost certainly a bug somewhere in *your* code, my guess would
    be overrunning the buffer you have created. Reduce your program to the
    smallest compilable example that exhibits the problem and does not use
    extensions and post it here and you might get some help.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Apr 19, 2005
    #11
  12. Ronny Mandal

    Ronny Mandal Guest

    Sorry about the footer, sometimes I forget to remove it, it comes
    automatically, but not on my personal behalf.

    Indeed, it is not my typing.
    Ronny Mandal, Apr 19, 2005
    #12
  13. Jonathan Bartlett <> writes:
    > If you're interested in the workings of malloc(), you should check out this:
    >
    > http://www.ibm.com/developerworks/linux/library/l-memory/


    Correction: If you're interested in the workings of some
    system-specific implementations of malloc().

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 19, 2005
    #13
  14. In article <d43l6c$5ms$>,
    "Ronny Mandal" <> wrote:

    > Hi!
    >
    > I am writing a program that'll read binary integers from a file, put them
    > into a dynamic array. THe array is first declared
    >
    > int* a = malloc( sizeof( int ) * N ) <-- N is the number of slots.
    >
    > When N is odd, the program will work just fine, however when N is even, it
    > will not work.
    >
    > Is it just a Win32-issue, or a bug?


    Have a good look at your code. Are you by any chance writing to a [N] ?
    Assuming that malloc () was successful, you are allowed to write to a
    [0] up to a [N-1], but not a [N].
    Christian Bau, Apr 20, 2005
    #14
  15. Ronny Mandal

    Richard Bos Guest

    (Richard Tobin) wrote:

    > In article <>,
    > Artie Gold <> wrote:
    >
    > >That part of the sig is likely put there by the server through which the
    > >message was posted. It's probably not the OP's fault.

    >
    > Quite so, but if we don't complain to people who use such servers, how
    > are they ever going to be replaced?
    >
    > If, by choice, you use a server that adds this sort of thing to your
    > message, it makes you look silly. If you work for a company that puts
    > it on all your outgoing messages, it makes your company look as if
    > they don't understand the net, and are probably the sort who spend
    > more money on lawyers than products.


    IOW, like Microsoft ;-)

    Richard
    Richard Bos, Apr 20, 2005
    #15
  16. Ronny Mandal

    Ronny Mandal Guest

    Thanks for helping me out, strange thing that I just *forgot* that a[N]
    holds N-1 elements. malloc worked just fine from the beginning.

    Ronny Mandal
    Ronny Mandal, Apr 21, 2005
    #16
  17. Ronny Mandal

    Alan Balmer Guest

    On Fri, 22 Apr 2005 00:35:40 +0200, "Ronny Mandal"
    <> wrote:

    >Thanks for helping me out, strange thing that I just *forgot* that a[N]
    >holds N-1 elements. malloc worked just fine from the beginning.
    >

    Um, not exactly. When you have
    int* a = malloc( sizeof( int ) * N ) ;

    a can hold N elements, but the first one is a[0] and the last one is
    a[N-1].

    Probably what you meant.

    --
    Al Balmer
    Balmer Consulting
    Alan Balmer, Apr 22, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais

    why is casting malloc a bad thing?

    Brian Blais, Jan 23, 2004, in forum: C Programming
    Replies:
    231
    Views:
    3,248
    Dave Thompson
    Feb 5, 2004
  2. John
    Replies:
    13
    Views:
    684
  3. ravi
    Replies:
    0
    Views:
    439
  4. Barry
    Replies:
    39
    Views:
    2,090
    Jerry Coffin
    Aug 7, 2007
  5. Zam
    Replies:
    1
    Views:
    217
    Mark Schupp
    Mar 14, 2005
Loading...

Share This Page