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.

G++-3.3 Stack-Allocation bug?

Empfohlene Antworten

Veröffentlicht

Hallo, bitte schlagt mich nicht wenn das hier eine unqualifizierte Frage sein sollte... :rolleyes:

Das folgende kleine Programm soll mir zeigen, wieviel Platz ein char-Array auf dem Stack tatsächlich belegt...


#include <iostream>

using namespace std;


main() 

{

	int b;

	char a[97];

	cout << ((unsigned) &-((unsigned) &a) << endl;

}

[/code]

Auf einem Athlon 1400 mit gcc-2.95.4 kompiliert wird beispielsweise "100" ausgegeben, wie ich es erwartet hatte, da 97 chars ebend 97Bytes belegen und dann noch auf ganze dwords "aufgerundet wird".

Mit gcc-3.3.4 dagegen wird "124" ausgegeben, es werden also offenbar 24Bytes verschwendet.... ist das so geplant; wenn ja, warum?

Grüße, atajh

  • 3 Wochen später...

Hmmm...

Also ich mache das eigentlich immer mit dem "sizeof" Operator?


#include <iostream>

using namespace std;


main() 

{

        int b;

        char a[97];

        cout << ((unsigned) &-((unsigned) &a) << endl;

        cout << sizeof(a) << endl;

}

[/code]




Das ganze liefert auf einem AthlonXP mit FreeBSD als OS GCC 3.4.2 folgendes Ergebnis:

[code] bash-2.05b$ ./testa.bin 124 97 bash-2.05b$

Der sizeof Operator leifert mir die zu erwartenden 97 Byte;

dein Ausdruck zur Adressberechnung nicht.

Kann es sein das dieser Ausdruck fehlerbehaftet ist?

Verstehe ich es richtig dass Du die Differenz der Adresse des Integer Wertes

und der Adresse des Arrays von einander abziehst?

Macht das keinen Sinn oder verstehe ich das was falsch?

Bye

SystemError,

blutiger C++ Anfänger

Soooo, noch mal ein kleines Update von mir.

Schau Dir mal diesen Code hier an:


#include <iostream>

using namespace std;


main() 

{

        int b;

        char a[97];

        cout << ((unsigned) &a[97])-((unsigned) &a[0]) << endl;

        cout << sizeof(a) << endl;

}

Der liefert sowohl auf einem FreeBSD System mit GCC 3.4.2 als auch auf einer AIX Kiste mit GCC 2.95.2 folgendes Ergebnis:

bash-2.05$ ./a.out cpp 

97

97

So denke ich mir das:

Ich nehme die Adresse von a[97] (letztes Array Elementes + 1 Element)

und ziehe dann davon die Adresse von a[0] (erstes Array Element) ab.

Dazwischen liegen dann 97 Byte, so wie es sein sollte?

Sorry wenn ich falsch leige... ...bin wie gesagt blutiger Anfänger in C++.

Bye

SystemError

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.