C++(Visual Studio 2012): Copying a function's parameter char* to a dynamically allocated one

StackOverflow https://stackoverflow.com/questions/21365941

  •  03-10-2022
  •  | 
  •  

Question

I have this structure defined and a class in my project. It is a class that holds id numbers generated by GetIdUsingThisString(char *), which is a function that loads a texture file into GPU and returns an id(OpenGL).

The problem is, when I try to read a specific file, the program crashes. When I run this program in VS with debugging it works fine, but running .exe crashes the program(or running without debugging from MSVS). By using just-n-time debugger I have found out that, for num of that specific file, Master[num].name actually contains "\x5" added(concatenation) at the end of the file path, and this is only generated for this one file. Nothing out of this method could do it, and I also use this type of slash / in paths, not \ .

struct WIndex{
    char* name;
    int id;
};

class Test_Class
{
public:    
    Test_Class(void);
    int AddTex(char* path);
    struct WIndex* Master;
    TextureClass* tex;
    //some other stuff...
};

Constructor:

Test_Class::Test_Class(void)
{
    num=0;
    Master=(WIndex*)malloc(1*sizeof(WIndex));

    Master[0].name=(char*)malloc(strlen("Default")*sizeof(char));
    strcpy(Master[0].name,"Default");
    Master[0].id=GetIdUsingThisString(Master[0].name);
}

Adding a new texture:(The bug)

int Test_Class::AddTex(char* path)
{
    num++;
    Master=(WIndex*)realloc(Master,(num+1)*sizeof(WIndex));

    Master[num].name=(char*)malloc(strlen(path)*sizeof(char));
    strcpy(Master[num].name,path);<---HERE

    Master[num].id=GetIdUsingThisString(path);
    return Master[num].id;
}

At runtime, calling AddTex with this file would have path with the right value, while Master[num].name will show this modified value after strcpy(added "\x5").

Question: Is there something wrong with copying(strcpy) to a dynamically allocated string? If i use char name[255] as a part of the WIndex structure, everything works fine.

More info: This exact file is called "flat blanc.tga". If I put it in a folder where I intended it to be, fread in GetIdUsingThisString throws corrupted heap errors. If I put it in a different folder it is ok. If I change it's name to anything else, it's ok again. If I put a different file and give it that same name, it is ok too(!!!). I need the program to be bug free of this kind of things because I won't know which textures will be loaded(if I knew I could simply replace them).

Was it helpful?

Solution

Master[num].name=(char*)malloc(strlen(path)*sizeof(char));

Should be

Master[num].name=(char*)malloc( (strlen(path)+1) * sizeof(char));

There was not place for the terminating NULL character

From http://www.cplusplus.com/reference/cstring/strcpy/:

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

The same happens here:

Master[0].name=(char*)malloc(strlen("Default")*sizeof(char));
strcpy(Master[0].name,"Default");

OTHER TIPS

Based on the definitions (below) - you should use strlen(string)+1 for malloc.

A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).

The strcpy() function shall copy the string pointed to by s2 (including the terminating null byte)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top