Locked How can I use realloc() ?

  • 7 สิงหาคม 2555 9:39
     
     

    Hi All,

    I'm using realloc() function in my code and there is some mistake. The code is given below:

    char* GetStudentsNames( char *szRollNumbers )

    {

    char *szReturnName = NULL;

    char *szRecName = NULL;

    // writing query with all Roll numbers here

    HRESULT hr = pRs->Open(query) // pRs is _RecordsetPtr

    if(SUCCEEDED(hr))

    {

    long lRecCount = pRs->GetRecordCount();

    if(lRecCount > 0)

    {

    pRs->MoveFirst();

    while(VARIANT_FALSE == pRs->EndOfFile)

    {

    szRecName = (assigning name from each record - first to last);

    if(szReturnName == NULL)

    {

    szReturnName = (char*)malloc(strlen(szRecName));

    szReturnName = szRecName;

    }

    else

    {

    if(strlent(szReturnName) < (strlen(szRecName)+strlen(azReturnName)))

    {

    szReturnName = (char*)realloc(szReturnName, (strlen(szRecName)+strlen(szReturnName)+2));

    }

    szReturnName = strcat(szReturnName, ", "); // adding coma in between names

    szReturnName = strcat(szReturnName, szRecName);

    }

    szRecName = NULL;

    pRs->MoveNext();

    }

    }

    }

    return szReturnName;

    }

    calling the above function as:

    char * szStudentsName = NULL;

    szStudentsName = GetStudentsNames("101"); // this call works fine

    szStudentsName = GetStudentsNames("101,102"); // this call works fine

    szStudentsName = GetStudentsNames("101,102,103"); // this call getting some name with garbage value and also duplicate entry

    Anyone pls tell me that there is any mistake in my code?

    Pls help...

    Regards,

    R-VR

ตอบทั้งหมด

  • 7 สิงหาคม 2555 9:49
     
     คำตอบ

    On 07/08/2012 11:39, R-VR wrote:

    Hi All,

    I'm using realloc() function in my code and there is some mistake. The code is given below:

    Unfortunately your code appears with a poor formatting (e.g. no indentation), so I find it hard to read.

    In any case, I'd suggest you to not use those raw C functions like malloc, realloc, strcat... Just use modern C++ and a string class, like std::string.

    Use std::string's operator+= and operator+ overloads to concatenate strings together, and you don't have to pay attention to allocating dynamic memory, calculate proper length, copy buffers, free memory, etc.

    And just return a std::string instead of a char*.

    Giovanni

    • ทำเครื่องหมายเป็นคำตอบโดย R-VR 7 สิงหาคม 2555 12:34
    •  
  • 7 สิงหาคม 2555 10:57
     
     
    Anyone pls tell me that there is any mistake in my code?
    Step through your program in the debugger to see what is happening.
     
    I agree with Giovanni that you would be better using std::string than C character array functions. For building strings, std::ostringsteam is often useful.
     

    David Wilkinson | Visual C++ MVP
  • 7 สิงหาคม 2555 12:32
     
     

    First thing where did you allocate memory to szRecName , Are you trying to assign some garbage to pointer or what.Second Roll Now should be int type not char pointer . Not able to see whether allocated memory ever get free inside your code. And where did you allocated memory to your szRollNumbers .Does it ever get used in your code. Will suggest you to put a break point and start debugging your code.

    Thanks


    Rupesh Shukla

  • 7 สิงหาคม 2555 12:34
     
     
    Thanks Giovanni...
  • 7 สิงหาคม 2555 13:36
     
     คำตอบ

    R-VR wrote:

    szReturnName = (char*)malloc(strlen(szRecName));
    szReturnName = szRecName;

    This makes no sense. You allocate a block of memory and make  szReturnName point to it. Then, in the very next line, you make  szReturnName point to something else, thus leaking the block you've just  allocated. You probably meant to call strcpy or similar.

    Note further that strlen() doesn't include a terminating NUL character  in its count. You need to allocate one extra byte for the terminating  NUL, otherwise you'll have a buffer overrun.

    if(strlent(szReturnName) < (strlen(szRecName)+strlen(azReturnName)))

    This check doesn't make sense. It boils down to "if (A < B + A)", which  of course is true most of the time, except when B is zero (that is,  szRecName is an empty string). If you want to check whether szRecName  points to an empty string, there are shorter ways to do that.

    szReturnName = (char*)realloc(szReturnName,  (strlen(szRecName)+strlen(szReturnName)+2));

    Since szReturnName doesn't point to a block of memory allocated with  malloc, you cannot call realloc on it.


    Igor Tandetnik

    • ทำเครื่องหมายเป็นคำตอบโดย R-VR 23 สิงหาคม 2555 5:52
    •