Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Parameter übergabe

Empfohlene Antworten

Veröffentlicht

Folgendes Problem:

es werden an eine Funktion Parameter übergeben:


cPicture *temppic = new cPicture;

temppic->SetSize(11, 11);

die Funktion hat den Sinn, den Speicherbereich zu vergrößern:

class cPicture

{

	void* pic;

	void SetSize(int newxsize, int newysize);

};


void cPicture::SetSize(int newxsize, int newysize)

{

	if(pic != NULL)

		pic = (SDL_Color*)realloc(pic, sizeof(SDL_Color)*(newxsize*newysize));

	else

		pic = (SDL_Color*)malloc(sizeof(SDL_Color)*(newxsize*newysize));

	if(pic == NULL)

	{

		cout << "couldn't malloc mem (" << newxsize << "x" << newysize << ")->" << newxsize*newysize << endl;

		return;

	}

}
ich bekomme als Ausgabe: "couldn't malloc mem (44x15)->660" bei anderen Zahlen funktioniert alles: zb.

cPicture *temppic = new cPicture;

temppic->SetSize(10, 11);

Wenn es mit größeren Zahlen als (44,15) auch klappt, hast du dir vermutlich irgendwo vorher bereits den Heap zerschossen, möglicherweise durch eine Überschreitung der Grenzen eines Arrays.

Warum hantierst du eigentlich überhaupt mit malloc, wenn du C++ benutzt?

Du meinst als alternative einfach array erstellen?

Der fehler trat eben mit diesen zahlen auf, ich hab dann noch probiert, obs mit konstanten geht, und mt denen giengs eben auch nicht

Und trozdem danke!

muss ich halt weiter nach andren fehlern suchen

Du meinst als alternative einfach array erstellen?
Nein, ich meine std::vector. Der nimmt dir die gesamte Speicherverwaltung ab.

Bau einfach überall in deinem Programm, wo du einen Arrayzugriff hast, eine Prüfung ein, ob der Index gültig ist, dann sollte sich das Problem schnell erledigt haben.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.