wierd pointer behavior
hi ,
i wrote this function :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
PlayerResult playerGetInstruments(Player player, Instrument** instruments,
int* amount) {
if (player == NULL || amount == NULL ) {
return PLAYER_NULL_ARG;
}
int indexLast = 0;
indexLast = getInstrumentsCount(player);
if(indexLast <=0){
return PLAYER_INSTRUMENT_AMOUNT_ERR;
}
if(*instruments!= NULL){
free(*instruments);
}
(*instruments) = (Instrument*) malloc(sizeof(Instrument*) * indexLast);
if ((*instruments) == NULL ) {
return PLAYER_MEMORY_ERR;
}
PlayerResult result;
if (!playerIsLegal(player, &result)) {
free((*instruments));
return result;
}
for (int i = 0; i < indexLast; i++) {
instruments[i] = (Instrument*)malloc(sizeof(struct instrument_t));
strcpy((*instruments[i]).name,player->instruments[i].name);
strcpy((*instruments[i]).profession,player->instruments[i].profession);
}
*amount = indexLast;
return PLAYER_SUCCESS;
}
| |
and i tried testing it with this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
Player player=playerCreate("Nadav", "Peled", 2);
Instrument* instruments, inst, inst2;
instruments = NULL;
int amount;
strcpy(inst.name, "guitar");
strcpy(inst.profession, "guitarist");
strcpy(inst2.name, "bass");
strcpy(inst2.profession, "bassist");
PlayerResult res=playerAddInstrument(player, inst);
res=playerAddInstrument(player, inst2);
//Chcking func with legal inputs
res=playerGetInstruments(player, &instruments, &amount);
ASSERT(res==PLAYER_SUCCESS);
ASSERT(amount==2);
ASSERT((strcmp(instruments[0].name, inst.name)==0) &&
(strcmp(instruments[1].name, inst2.name)==0));
| |
my problem is when running in eclipse until the line
return PLAYER_SUCCESS
the stack seems ok meaning
1 2 3 4
|
instruments[0].name = "guitar"
instruments[0].profession = "guitarist"
instruments[1].name = "bass"
instruments[1].profession = "bassist"
| |
but when i return to the testing function (the second above)
i get in stack
1 2 3 4
|
instruments[0].name = "guitar"
instruments[0].profession = "guitarist"
instruments[1].name = "i\0\0\0\0bass"
instruments[1].profession = "\0\0\0\0bassist"
| |
and i tried adding more instruments then i got :
1 2
|
instruments[2].name = "\0\0\0\0i\0\0\0\0XXXX"
instruments[2].profession = "\0\0\0\0\0\0\0\0XXXXXX"
| |
its like it's adding every time 4 bytes
why is that ???
thnx
Last edited on
That seems a little confused to me.
In:
|
PlayerResult playerGetInstruments(Player player, Instrument** instruments, int* amount)
| |
you should think of instruments as a pointer passed by reference.
With that in mind, this is a reasonable statement:
|
(*instruments) = (Instrument*) malloc(sizeof(Instrument*) * indexLast);
| |
but this isn't:
1 2 3 4 5
|
for (int i = 0; i < indexLast; i++) {
instruments[i] = (Instrument*)malloc(sizeof(struct instrument_t));
strcpy((*instruments[i]).name,player->instruments[i].name);
strcpy((*instruments[i]).profession,player->instruments[i].profession);
}
| |
Also, don't use malloc in C++ unless there's a specific good reason to.
can you please explain what do you mean by unreasonable statement .
Last edited on
You're treating it as an array of Instrument*, which it clearly isn't.
Topic archived. No new replies allowed.