How can I modify the value of a string defined in a struct?
Based on your comments, let me modified the original question...
I wan开发者_运维百科t to create a struct with size of 4kb (this size is a requirement so I have to meet it). The problem was that I couldn't modify the value of the string variable contained in the struct because the compiler throws a segmentation fault. Currently, if I use a pointer to string instead of a string variable, I now know how to do it (thanks to you guys), however, I read that the way that I'm using to allocate the 4kb of memory (malloc) is not the best or the appropriate. If I use the "new" keyword, it dynamically allocates enough memory for the struct and it probably uses a different value than 4kb, right? and this is what I don't want.
I still have the doubt about why I could not modify the value of a string variable (not pointer) contained in my struct (something like paginas -> dato = "test"). It probably should be a consequence of the use of malloc
Anyway, I would really appreciate your advices about how to allocate the 4kb of memory.
The original code in c++ is the following:
#define TAM 4000
#define NUMPAGS 512
struct pagina
{
bitset<12> direccion;
char operacion;
char permiso;
string *dato; //I prefer to have a string variable
int numero;
};
void crearPagina(pagina* pag[], int pos, int dir)
{
pagina * paginas = (pagina*)malloc(sizeof(char) * TAM);
paginas -> direccion = bitset<12> (dir);
paginas -> operacion = 'n';
paginas -> permiso = 'n';
string **tempDato = &paginas -> dato;
char *temp = " ";
**tempDato = temp;
paginas -> numero = 0;
pag[pos] = paginas;
}
Thanks in advance!!!
dato is a pointer to a string. That is, it tells you where to look for the string, but it doesn't actually contain the string.
So if you wish to assign a string to it, you must allocate the memory to hold the string yourself, and give the pointer to that memory to the struct to hold for you.
pagina->dato = new string("test");
When you are finished with the struct, you will need to remember to release the memory for the string or you will get a "memory leak" (because, again, the struct is not responsible for the memory containing the string, only for remembering where you put it):
delete pagina->dato;
pagina->data = NULL;
struct pagina
{
pagina(int dir)
: direccion(dir)
, operacion('n')
, permiso('n')
, dato(" ")
, numero(0) {}
bitset<12> direccion;
char operacion;
char permiso;
std::string dato;
int numero;
};
void createPagina(pagina* pag[], int pos, int dir) {
pag[pos] = new pagina(dir);
}
This is more C++ (as oppose to C) way of doing what you want. Create a constructor instead of creation procedure and use new
instead of malloc
. Also I would advice using std::vector<pagina>
instead of raw pointer array.
struct pagina
{
bitset<12> direccion;
char operacion;
char permiso;
std::string dato;
int numero;
pagina(int dir)
: direccion(dir), operation('n'), permiso('n'), dato(" "), numero()
{
}
};
void crearPagina(std::vector<pagina>& pag, int dir)
{
pag.push_back(pagina(dir));
}
精彩评论