Thread: problem in windows vista

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    Post problem in windows vista

    Hi all.

    I am developing a RAID configuration tool. As a part of creation of a volume, i am


    allocating some memory dynamically , using malloc() function for n number of pointers and later i am trying to free the memory using the free function.


    However the free() function throws an exception some times. It shows some hexadecimal address and says that the memory at that location could not be read.

    This is not happening in any other platforms like server2003 ,windows xp but only on Vista.

    the code some thing like this


    Code:
    pptr = (struct xyz **) malloc(
                                sizeof(struct xyz *) * 8);
    
    ......
    
    // code to create the RAID volume. The 8 pointers that have been allocated memory above,   are intialized to some addresses here.
    ......
    
    
    
    
    for(i=0; i< numdisks; i++)
    
    {
     if(pptr[i]!=NULL)
     {    
      free(pptr[i]);
     }
    }
    Here the value of numdisks is always less than or equal to 8. So there is no chance of freeing an uninitialized pointer.

    Please let me know if there is any thing specific to windows vista that is throwing such error message.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You need to free ONLY ONCE, if in your code you malloc only one time like you show:
    Code:
    free(pptr);
    Strange that is is only a Vista thing

    EDIT: If by chance you do this afterwards (not shown in code)
    Code:
    for(i=0;i<8;++i)
       pptr[i] = malloc(sizeof(struct xyz)*something));
    then you shouldn't get an error the way you free if you have NOT already have free(pptr)
    Last edited by C_ntua; 09-30-2008 at 04:52 AM.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    malloc() & free() are C functions, therefore they cannot throw exceptions.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am assuming "throwing an exception" means something along the lines of access violation, or segmentation fault, not actually throwing as in C++'s meaning of throwing.
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Here the value of numdisks is always less than or equal to 8. So there is no chance of freeing an uninitialized pointer.
    Says who?
    If you didn't
    a) assign a value with malloc
    b) assign a value of NULL
    when you created the thing, then your if(pptr[i]!=NULL) thing is utterly pointless. If the memory pointer is garbage, it won't be NULL and it won't be a valid pointer. Either way, you lose.

    It's also pointless if you did those things, because free(NULL) is a harmless thing.

    It would seem to me that you're assuming that malloc initialises memory (it doesn't). As such, if you don't use all 8 slots, then you're freeing garbage.
    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.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Everyone has got on you about the one thing your code did correctly, instead of pointing out the one area where you did things wrong

    Example:
    Code:
    #define RAID_SIZE 8
    
    // later on in your code...
    
    pptr = reinterpret_cast<xyz **>(malloc(sizeof(xyz *) * RAID_SIZE));
    if(!pptr)
      return; // or whatever
    
    for(int i = 0; i < RAID_SIZE; ++i)
      pptr[i] = malloc(sizeof(xyz));
    
    // later on
    
    if(pptr)
    {
      for(int i = 0; i < RAID_SIZE; ++i)
      {
        if(pptr[i])
          free(pptr[i]);
      }
    
      free(pptr);
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    By the way, the missing step that I added coupled with the fact that you were trying to free each un-allocated pointer as if you had generated them as demonstrated in my example. So although everyone flamed you for doing something that technically wasn't wrong, they are right in stating that it is not doing what you think it is doing. Using my sample code though you should be a-ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you write directly into video memory in windows vista?
    By kypronite in forum Game Programming
    Replies: 3
    Last Post: 08-02-2008, 02:12 PM
  2. OpenGL Quesiton: Does Windows XP, Vista still at OpenGL 1.1?
    By indigo0086 in forum Game Programming
    Replies: 8
    Last Post: 05-21-2007, 11:18 AM
  3. Windows XP (and Vista) shiney look and feel
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 09-18-2005, 02:56 PM
  4. Application Termination Problem in Windows XP
    By wasabee in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 12:53 PM
  5. C++ Gurus, UNIX vs. Windows ascii problem perhaps?
    By puck in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2003, 10:33 PM