Thread: variable array size

  1. #31
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That would be a violation of a class invariant. There should probably be an assert there to make sure the invariant holds, but no special handling code is required.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #32
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's called User Error.
    If you lie to a function, expect bad things.

  3. #33
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    An assert would do the trick. But the objective is to work with the library client, not against him.

    Undefined behavior is pretty harsh punishment.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #34
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by King Mir View Post
    An assert would do the trick. But the objective is to work with the library client, not against him.

    Undefined behavior is pretty harsh punishment.
    assert() only works in debug mode, so unless they actually test with the debug build it won't help; but I'll add it anyways.
    But then how do you stop the user from entering and oldSize that's bigger than it really is, or passing an invalid (not NULL) pointer?

    Now if anyone has any ideas on how to do this on UNIX, that would be really cool!

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't really understand this, though:
    Code:
    new( &pNew[i] ) T( ptr[i] );
    Doesn't that basically mean construct a new object instead of copying it? Or maybe that basically invokes the copy constructor, I think?
    I'm also rusty on that syntax... but new returns a pointer to an allocated object... but I'm guessing that it is just supposed to create an object in the allocated memory and invoke the (copy) constructor?

    Just a thought: but your realloc function requires memory to be allocated with HeapAlloc to work. And if such is the case, you can use HeapSize to get size (no OldSize) and to see if it's a valid pointer.
    Last edited by Elysia; 01-29-2008 at 03:16 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Doesn't that basically mean construct a new object instead of copying it?
    Well, what do you think copying is? It's creating a new object equivalent to an existing one.

    new returns a pointer to the allocated object. But in the case of placement new, this pointer is equal to the one we passed to it, so keeping it is unnecessary.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CornedBee View Post
    Well, what do you think copying is? It's creating a new object equivalent to an existing one.
    I was confused about if it was copying the state of the object over, as well, since new would just invoke the constructor.

    new returns a pointer to the allocated object. But in the case of placement new, this pointer is equal to the one we passed to it, so keeping it is unnecessary.
    So this is placement new then. I see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It would invoke the constructor. The copy constructor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I thought so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Just a thought: but your realloc function requires memory to be allocated with HeapAlloc to work. And if such is the case, you can use HeapSize to get size (no OldSize) and to see if it's a valid pointer.
    Cool, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM