-1

I am trying to solve this problem. I am a beginner in C.

                        uint8_t *instrumentStringsCount;
instrumentStringsCount = malloc(sizeof(uint8_t));
dbGettingStringsNumbers(instrumentStringsCount, familyID);
uint8_t sizeCount = instrumentStringsCount[0];
uint8_t i = 0;

char stringTransferArray[10];
char *mainArray[sizeCount];

while(i < sizeCount)
{
    sprintf(stringTransferArray, "%d strings", instrumentStringsCount[i+1]);
    mainArray[i]= stringTransferArray;
    i++;
}

The thing is that stringTransferArray is dynamically changing (instrumentStringsCount: being fetched from the db). So the values of the pointers are always being changed to the last value in the array. I know the reason for that; however, I just need a way to solve this.

Thanks for your efforts

Miriana Itani
  • 865
  • 9
  • 25

2 Answers2

0

My problem is in this line: mainArray[i]= stringTransferArray; the stringTransferArray is changing and when that happens, all the values of the mainArray show the last value associated with stringTransferArray

As for your problem, there are functions to duplicate strings. It's not really standardized in the C specification, but I know of no platform that doesn't have a strdup function. – Some programmer dude

Armali
  • 18,255
  • 14
  • 57
  • 171
-1

sizeof(uint8_t) is likely to be 1 byte. And that is not enough for instrumentStringsCount (so you are malloc-ing a too small memory zone, which will give you buffer overflow when using later instrumentStringsCount[i+1], a typical case of undefined behavior; you really should be very scared). You probably want to keep in another variable the allocated size (and you should have a convention to know the allocated size), and you should test against failure of malloc so code:

size_t instrumentStringSize = 24;
uint8_t *instrumentStringsCount = malloc(instrumentStringSize * sizeof(uint8_t));
if (!instrumentStringsCount) { perror("malloc"); exit(EXIT_FAILURE); };

You might want to also keep the used length of your buffer.

At some times in your program, you may want to grow your instrumentStringsCount memory zone. Since you don't want to call realloc or malloc at every round, you probably should grow the size with e.g. an affine function:

size_t newsize = 4*instrumentStringSize/3+ 10;
uint8_t* newStringsCount = malloc(newsize * sizeof(uint8_t));

etc (e.g. test against failure of malloc and copy using memcpy the old memory zone into the new one, etc...).

At last, you should use snprintf (and give the actual size of the buffer) instead of sprintf (because you really want to avoid buffer overflow and other undefined behavior).

If on Linux, compile with all warnings & debug info (gcc -Wall -Wextra -g). The instrumentation options of gcc like -fsanitize=address and other sanitizers would also be helpful. Then use the gdb debugger & valgrind.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you for your reply but I guess you did not understand my question. My problem is in this line: mainArray[i]= stringTransferArray; the stringTransferArray is changing and when that happens, all the values of the mainArray show the last value associated with stringTransferArray – Miriana Itani Dec 11 '16 at 11:39
  • No, you are `malloc`-ing a too small memory zone. That is UB. And **you should really be *very scared***. – Basile Starynkevitch Dec 11 '16 at 16:34
  • I insist that you should be very scared and read more about undefined behavior. – Basile Starynkevitch Dec 11 '16 at 16:47
  • Thank you for your concern but I just needed an answer for the above question. What I am dealing with is a very small unique particular case. I am not that concerned with that fact. However, do you have any way to solve my problem? Maybe using a different function? – Miriana Itani Dec 12 '16 at 09:29
  • Since your code has UB, the "I just need a way to solve this" question requires removing the undefined behavior (so using `malloc` *properly*, not wrongly). – Basile Starynkevitch Dec 12 '16 at 13:19