Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading in QB64pe (again)
#11
Quote:
Quote: Wrote:1. Don't pass `tdata` on the stack to the thread. You should use  something like `malloc()` to allocate it because you don't know when the other thread will actually start using it, it could happen after invokeWorker returns.
I would appreciate it if you go into more detail on this. Perhaps some sample code?
This is an example of malloc() and realloc(). The malloc() function provides memory, the realloc() function can expand this memory to the desired size if necessary.
Any input other than a number ends the input. -- malloc() + realloc()

Code: (Select All)

//Benutzung von malloc() und realloc() - 28. Mai 2024

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int main(void)
{
int *i_zgr;
int zaehler = 0, summe;
size_t bytes = sizeof(int);

//Zunächst nur Platz für einen Wert
if((i_zgr = malloc(bytes)) == NULL)
{
printf("\nMalloc() fehlgeschlagen!");
exit(1);
}
printf("\nMehrere Zahlen eingeben fuer ein temporaeres Feld.");
printf("\nKeine Zahl bedeutet Ende der Eingabe.\n\n");

while(scanf("%d", &i_zgr[zaehler]) == 1)
{
++zaehler;
//Erstelltes Feld erweitern
if((i_zgr = realloc(i_zgr, bytes * (zaehler + 1))) == NULL)
{
printf("\n\nErweiterung fehlgeschlagen!");
exit(1);
}
}
summe = 0;
printf("\nSie haben eingegeben:\n");
for(int i = 0; i < zaehler; i++)
{
printf("Zeiger[%2d] = %3d\n", i, i_zgr[i]);
summe += i_zgr[i];
}
printf("\n\nSumme der eingegebenen Zahlen: %3d\n", summe);

//Speicher wieder freigeben
free(i_zgr);

return(0);
}

[Image: malloc-realloc2024-05-28-204243.jpg]
Reply


Messages In This Thread
Threading in QB64pe (again) - by justsomeguy - 05-27-2024, 12:58 AM
RE: Threading in QB64pe (again) - by DSMan195276 - 05-27-2024, 11:53 AM
RE: Threading in QB64pe (again) - by aurel - 05-27-2024, 12:26 PM
RE: Threading in QB64pe (again) - by justsomeguy - 05-27-2024, 02:32 PM
RE: Threading in QB64pe (again) - by DSMan195276 - 05-27-2024, 04:42 PM
RE: Threading in QB64pe (again) - by a740g - 05-27-2024, 06:26 PM
RE: Threading in QB64pe (again) - by DSMan195276 - 05-27-2024, 09:55 PM
RE: Threading in QB64pe (again) - by justsomeguy - 05-28-2024, 01:41 AM
RE: Threading in QB64pe (again) - by DSMan195276 - 05-28-2024, 01:31 PM
RE: Threading in QB64pe (again) - by justsomeguy - 05-28-2024, 04:03 PM
RE: Threading in QB64pe (again) - by Kernelpanic - 05-28-2024, 06:46 PM
RE: Threading in QB64pe (again) - by DSMan195276 - 05-28-2024, 11:14 PM
RE: Threading in QB64pe (again) - by justsomeguy - 05-29-2024, 03:33 PM
RE: Threading in QB64pe (again) - by DSMan195276 - 05-30-2024, 02:13 AM
RE: Threading in QB64pe (again) - by justsomeguy - 05-30-2024, 04:33 AM
RE: Threading in QB64pe (again) - by justsomeguy - 05-31-2024, 03:00 PM
RE: Threading in QB64pe (again) - by SMcNeill - 06-02-2024, 08:14 AM
RE: Threading in QB64pe (again) - by justsomeguy - 06-02-2024, 01:11 PM
RE: Threading in QB64pe (again) - by SMcNeill - 06-02-2024, 04:01 PM



Users browsing this thread: 4 Guest(s)