none
Problem with HeapAlloc() RRS feed

  • Question

  • Hello,
    I use Visual Studio 2008, and I have a problem with allocating memory on windows.
    This is the part of my code:

    char** split_data(char* cache,int bounds)
    {
        char** parts=NULL;
        int i=0,j=0,len=0;
        len=strlen(cache);
        if(bounds<=0||len==0){return NULL;}
        if(bounds>0&&len>0){parts=(char**)HeapAlloc(GetProcessHeap(),(HEAP_NO_SERIALIZE|HEAP_ZERO_MEMORY),sizeof(char*)*bounds);}
        if(!parts){error(9,hwnd_main);}
        parts[0]=cache;
        for(i=0;i<=len;i++)
        {
            if(cacheIdea==SEP)
            {
                if(j==bounds)
                {
                    cacheIdea=0;
                    break;
                }
                else
                {
                    j++;
                    parts[j]=&cache[i+1];
                    cacheIdea=0;
                }
            }
        }
        return parts;
    }

    To explain:
    cache parameter = "Jim~Maria~George~Mike~"
    SEP = `~`
    bound = 4
    I want this fonction to return parts:
    parts[0] = "Jim"
    parts[1] = "Maria"
    parts[2] = "George"

    parts[3] = "Mike"

    The strange thing is, that when I run my application by pushing the "Debug" button in Visual Studio, my function works, but when I run my application from Windows Explorer, either in Debug or Release, my application crushes:

    A visual studio Just-in-Time debbuger windows appears
    "An unhandled win32 exception occured in application_name.exe [3916]

    Any idea on how to fix this issue ?

    Thank you
    Friday, December 14, 2007 1:22 PM

Answers

  • First thing please use code tags.Second Point to remember Array index always start from 0 and end at n-1. So you can say range is between 0 to n-1 where n is the total no of argument in the array.

    Thanx
    Thursday, December 20, 2007 5:44 PM

All replies

  • Post correction Idea   = [ i ];
    Friday, December 14, 2007 1:31 PM
  •  lektrosonic wrote:

        for(i=0;i<=len;i++)

    Try to use i<len instead.

    Thursday, December 20, 2007 3:45 PM
    Moderator
  • First thing please use code tags.Second Point to remember Array index always start from 0 and end at n-1. So you can say range is between 0 to n-1 where n is the total no of argument in the array.

    Thanx
    Thursday, December 20, 2007 5:44 PM