473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

invalid pointer adress

Hello together,

the program below shows a behavior i do not understand. When compiled
with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function
test_it0 points to an invalid adress and an attempt to write to this
pointer causes the program to exit with a core dump.
Output after compiling with HP c-compiler:
1. ffffff78
1. 7eff3358

When compiled with the gcc compiler the functions test_it0 and
test_it1 are working in the same ( correct ) manner.
Output when compiled with gcc:
1. 800003fffeff3730
1. 800003fffeff3730

After moving the memset command just before the printff command it
also works ok with both compilers. Even if you comment out the line
"static char BLANK_VTRNR[2] = " ";" both functions are working.

Unfortunately i found this Problem while searching for the reason of a
program crash in a larger project. Thus i cant change to another
compiler and the described work araunds ( comment out af the
declaration, moving the memset command ) does not lead to the same
effect as in this small program.

Any comment is rather appreciated.

Thanks in Advance.
---------/snip --------------------
extern void *memset(void *, int, unsigned long);

static char BLANK_VTRNR[2] = " ";

struct s1{ char c[ 81 ]; };
struct s2{ void *p; };

void test_it0( void )
{
struct s1 v1;
memset( (void*)&v1, 0, sizeof( struct s1 ) );
struct s2 v2 = { &v1.c };
printf( "1. %lx\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}

void test_it1(void)
{
struct s1 v1;
struct s2 v2= { &v1.c };
printf( "1. %lx\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}

void main ()
{
test_it0();
test_it1();
}
------------/snap/---------------
Nov 14 '05 #1
5 2219
fk****@aspecta.com (FKothe) wrote:
the program below shows a behavior i do not understand. When compiled
with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function
test_it0 points to an invalid adress and an attempt to write to this
pointer causes the program to exit with a core dump.
Output after compiling with HP c-compiler:
1. ffffff78
1. 7eff3358 extern void *memset(void *, int, unsigned long);
Don't do this - #include the proper header.
static char BLANK_VTRNR[2] = " ";
(Whatever is this for? It isn't used.)
struct s1{ char c[ 81 ]; };
struct s2{ void *p; };
You cause undefined behaviour:
void test_it0( void )
{
struct s1 v1;
memset( (void*)&v1, 0, sizeof( struct s1 ) );
struct s2 v2 = { &v1.c };
printf( "1. %lx\n", v2.p );
here...
}

void test_it1(void)
{
struct s1 v1;
struct s2 v2= { &v1.c };
printf( "1. %lx\n", v2.p );
....and here...
}

void main ()


....and here.

The first two are probably (!) the most important. You cannot expect
consistent answers when you lie to printf(). You're not passing an
unsigned integer, you're passing a pointer. Replace %lx with %p, twice,
replace void main() with int main(void), return 0 from main(), and try
again.

Richard
Nov 14 '05 #2
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<42****************@news.individual.net>...
fk****@aspecta.com (FKothe) wrote:
the program below shows a behavior i do not understand. When compiled
with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function
test_it0 points to an invalid adress and an attempt to write to this
pointer causes the program to exit with a core dump.
Output after compiling with HP c-compiler:
1. ffffff78
1. 7eff3358
extern void *memset(void *, int, unsigned long);


Don't do this - #include the proper header.
static char BLANK_VTRNR[2] = " ";


Yes, you are right, this declaration is absolutly unnecessary, but
commenting this line out the address of v2.p in test_it0 becomes
valid( and identical to that in test_it1 ).
To explain how i came to this program:
I am working on a larger project, which exits with a core dump. The
reason for this is that there ist a strcpy instruction to a misleading
pointer.
After I found where the pointer gets its invalid adress, i copied this
part to a new programm and included al the projects includes. As the
error still occures i startet to remove all includes and definitions
until i found the definition of "static char BLANK_VTRNR[2] = " ";"
changed the program behavior. Because i do not have any idea why this
happened I asked for help here.
After I changed the Programm in the way you suggested the same error
appears:
./test_it
1. ffffff78
1. 7eff3328

----------/snip/--------------
#include <stdio.h>

static char BLANK_VTRNR[2] = " ";

struct s1{ char c[ 81 ]; };
struct s2{ void *p; };

void test_it0( void )
{
struct s1 v1;
memset( (void*)&v1, 0, sizeof( struct s1 ) );
struct s2 v2 = { &v1.c };
printf( "1. %p\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}

void test_it1(void)
{
struct s1 v1;
struct s2 v2= { &v1.c };
printf( "1. %p\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}

int main ()
{
test_it0();
test_it1();
return 0;
}
----------/snip/--------------

Unfortunately, removing the static char BLANK_VTRNR[2] = " ";
instruction in the projekt does not have the same effect.
In the following you can see the part of the original code, which has
the described error:

int leseExterneHinweise_masch_storno( void )
{
int iRes = -1; /* Fehler */
char cfDateiMitPfad[ 1024 ];
int iDateiId;
BOOL bEofDatei = FALSE;
int iAnzSaetze = 0;

t_MaschStoExtern AktSatz;
t_ptrMaschStoExtern pNeuerSatz = NULL;
t_ptrMaschStoExtern pLetzterSatz = NULL;

t_AusdatFeldbeschreibung ExterneHinweise[ MASCHSTO_EXTERN_ANZ_FD ]
=
{ /* Typ, Laenge , Pointer zur
Variablen , Fehler */
{ AUSDAT_TYPE_STRING, MASCHSTO_ATTRNAME_LEN,
AktSatz.cfAttributName, FALSE },
{ AUSDAT_TYPE_STRING, MASCHSTO_ATTRWERT_LEN,
AktSatz.cfAttributWert, FALSE },
{ AUSDAT_TYPE_STRING, MASCHSTO_AUSGABE_LEN , AktSatz.cfAusgabe
, FALSE }
};
/* breakpoint here */
....
where
typedef struct s_AusdatFeldbeschreibung
{
short sTyp;
int iLaenge;
void *pWert;
BOOL bFehlerhaft;
} t_AusdatFeldbeschreibung;

and
typedef struct sMaschStoExtern t_MaschStoExtern;
typedef t_MaschStoExtern *t_ptrMaschStoExtern;

struct sMaschStoExtern
{
char cfAttributName[ MASCHSTO_ATTRNAME_LEN + 1 ];
char cfAttributWert[ MASCHSTO_ATTRWERT_LEN + 1 ];
char cfAusgabe[ MASCHSTO_AUSGABE_LEN + 1 ];
t_ptrMaschStoExtern pNext;
};

When having a look at the structures on the breakpoint:
&AktSatz.cfAttributName = 0x800003ffff429cb0

ExterneHinweise[0]:
sTyp = 6 ( according to definition of AUSDAT_TYPE_STRING )
iLaenge = 30 ( according to definition MASCHSTO_ATTRNAME_LEN )
pWert = 0xfffffffffffffea0 <error reading Address
0xfffffffffffffea0:Bad Address>
bFehlerhaft = 0 ( according to definition of FALSE )

But:
&ExterneHinweise[0].pWert( signed char **) 0x800003ffff429d60


(Whatever is this for? It isn't used.)
struct s1{ char c[ 81 ]; };
struct s2{ void *p; };


You cause undefined behaviour:
void test_it0( void )
{
struct s1 v1;
memset( (void*)&v1, 0, sizeof( struct s1 ) );
struct s2 v2 = { &v1.c };
printf( "1. %lx\n", v2.p );


here...
}

void test_it1(void)
{
struct s1 v1;
struct s2 v2= { &v1.c };
printf( "1. %lx\n", v2.p );


...and here...
}

void main ()


...and here.

The first two are probably (!) the most important. You cannot expect
consistent answers when you lie to printf(). You're not passing an
unsigned integer, you're passing a pointer. Replace %lx with %p, twice,
replace void main() with int main(void), return 0 from main(), and try
again.

Richard

Nov 14 '05 #3
fk****@aspecta.com (FKothe) wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<42****************@news.individual.net>...
fk****@aspecta.com (FKothe) wrote:
the program below shows a behavior i do not understand. When compiled
with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function
test_it0 points to an invalid adress and an attempt to write to this
pointer causes the program to exit with a core dump.
Output after compiling with HP c-compiler:
1. ffffff78
1. 7eff3358
extern void *memset(void *, int, unsigned long);


Don't do this - #include the proper header.
static char BLANK_VTRNR[2] = " ";


Yes, you are right, this declaration is absolutly unnecessary, but
commenting this line out the address of v2.p in test_it0 becomes
valid( and identical to that in test_it1 ).


There's no reason from a C POV why the value above would not be valid;
nor why it should be identical to the other.
After I found where the pointer gets its invalid adress, i copied this
part to a new programm and included al the projects includes. As the
error still occures i startet to remove all includes and definitions
until i found the definition of "static char BLANK_VTRNR[2] = " ";"
changed the program behavior.
Then it probably causes whatever object you scribble on to be placed
somewhere else in memory. The error is very unlikely to be caused by
that line itself. You need to find out where that invalid pointer comes
from, not what its representation is. Somewhere in your code you are
doing something illegal to a pointer, probably some out-of-bounds
arithmetic, and that error is getting passed on.
Because i do not have any idea why this happened I asked for help here.
After I changed the Programm in the way you suggested the same error
appears:
./test_it
1. ffffff78
1. 7eff3328
Again, I don't see why this should be an error. You have to completely
different pointers, to two completely different objects. There's no
reason why they should be the same in the first place.

In the following you can see the part of the original code, which has
the described error:
This code does something completely different from what your original
example did. That called _two_ functions, and printed two different
pointers to separate objects in those separate functions. It is no
surprise that those could be different. This code seems to involve only
a single function. That's a whole other kettle of red herrings.

[ Rearranged for easier reading. ]
typedef struct sMaschStoExtern t_MaschStoExtern;
typedef t_MaschStoExtern *t_ptrMaschStoExtern;
struct sMaschStoExtern
{
char cfAttributName[ MASCHSTO_ATTRNAME_LEN + 1 ];
char cfAttributWert[ MASCHSTO_ATTRWERT_LEN + 1 ];
char cfAusgabe[ MASCHSTO_AUSGABE_LEN + 1 ];
t_ptrMaschStoExtern pNext;
}; typedef struct s_AusdatFeldbeschreibung
{
short sTyp;
int iLaenge;
void *pWert;
BOOL bFehlerhaft;
} t_AusdatFeldbeschreibung; t_MaschStoExtern AktSatz; t_AusdatFeldbeschreibung ExterneHinweise[ MASCHSTO_EXTERN_ANZ_FD ] =
{ /* Typ, Laenge , Pointer zur Variablen , Fehler */
{ AUSDAT_TYPE_STRING, MASCHSTO_ATTRNAME_LEN, AktSatz.cfAttributName, FALSE },


And here's the rub. In C89,
# All the expressions in an initializer for an object that has static
# storage duration or in an initializer list for an object that has
# aggregate or union type shall be constant expressions.

A struct is an aggregate type; the address of an automatic object is not
a constant expression.

This restriction is lifted for aggregates and unions in C99, btw. It's
still there for static objects, for obvious reasons.

Richard
Nov 14 '05 #4
In article <98**************************@posting.google.com >
FKothe <fk****@aspecta.com> wrote:
After I changed the Programm in the way you suggested the same error
appears:
./test_it
1. ffffff78
1. 7eff3328

----------/snip/--------------
#include <stdio.h>
I note there is no "#include <string.h>" to declare memset()...
static char BLANK_VTRNR[2] = " ";

struct s1{ char c[ 81 ]; };
struct s2{ void *p; };

void test_it0( void )
{
struct s1 v1;
memset( (void*)&v1, 0, sizeof( struct s1 ) );
struct s2 v2 = { &v1.c };
Declaration "after code" (after the call to memset, without an open
brace) is a C99 feature. Apparently your compiler supports at least
this part of C99.
printf( "1. %p\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}

void test_it1(void)
{
struct s1 v1;
struct s2 v2= { &v1.c };
printf( "1. %p\n", v2.p );
/*
strcpy( v2.p, "Hallo ich schreibe mal was rein.." );
printf( "%s\n", v2.p );
*/
}

int main ()
{
test_it0();
test_it1();
return 0;
}
We have to do a little bit of guessing about the "%p" output in:
./test_it

1. ffffff78
1. 7eff3328

(because of course Standard C says nothing about it), but it appears
to be a straightforward hex-dump of the actual 32-bit address. (On
the other hand, other output you have shown suggests that the machine
has 64-bit addresses, and probably multiple compiler models such as
IL32P64 -- int-and-long are 32 bits and pointers are 64, I32LP64, and
maybe even an ILP32 model, as this output suggests.)

Assuming a conventional machine and 32-bit pointers, at least one of
those numbers is quite likely quite wrong; the two values should be
near each other, e.g., both "about" 7eff33xx or both "about" ffffffxx.
If including <string.h> does not fix the problem, then you have
probably found a compiler bug -- and you have a nice short program
to demonstrate it, which improves the chance of getting it fixed,
at least. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
fk****@aspecta.com (FKothe) wrote:
t_MaschStoExtern AktSatz;

t_AusdatFeldbeschreibung ExterneHinweise[ MASCHSTO_EXTERN_ANZ_FD ] =
{ /* Typ, Laenge , Pointer zur Variablen , Fehler */
{ AUSDAT_TYPE_STRING, MASCHSTO_ATTRNAME_LEN, AktSatz.cfAttributName, FALSE },


And here's the rub. In C89,
# All the expressions in an initializer for an object that has static
# storage duration or in an initializer list for an object that has
# aggregate or union type shall be constant expressions.

A struct is an aggregate type; the address of an automatic object is not
a constant expression.


Note that I tried this in Dev-C++, which accepted it (DJGPP does not),
and it gave the same value for the two pointers (even though it didn't
have to accept the code at all). Try and see what happens if you compile
and run the following code:

#include <stdio.h>

int main()
{
typedef struct sMaschStoExtern t_MaschStoExtern;
typedef t_MaschStoExtern *t_ptrMaschStoExtern;
struct sMaschStoExtern {
char cfAttributName[ 30 ];
char cfAttributWert[ 30 ];
char cfAusgabe[ 30 ];
t_ptrMaschStoExtern pNext;
};

typedef struct s_AusdatFeldbeschreibung {
short sTyp;
int iLaenge;
void *pWert;
int bFehlerhaft;
} t_AusdatFeldbeschreibung;

t_MaschStoExtern AktSatz;

t_AusdatFeldbeschreibung ExterneHinweise[] = {
{ 6, 30, AktSatz.cfAttributName, 0 },
{ 6, 30, AktSatz.cfAttributWert, 0 },
{ 6, 30, AktSatz.cfAusgabe , 0 }
};

printf("Akt: %p\tExt: %p\n", (void *)AktSatz.cfAttributName,
(void *)ExterneHinweise[0].pWert);

getchar();
return 0;
}

If this gives two different values, or the compiler complains about it,
you'll probably just have to initialise your structs differently. If it
gives the same value for both (as it does for me), your problem seems to
be elsewhere.

Richard
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
3
by: Lalatendu Das | last post by:
Hi , Any way i have problem related to pointer let's say i have a double pointer like node_t **headptr =NULL ; //global one // let's say this is the defination of node_t typedef struct list {...
4
by: c language | last post by:
Hi All, I have written a C++ program, it can be compiled (in UNIX) but when I run it, I usually gives the following errors: free(): invalid pointer 0x51d4a0! free(): invalid pointer 0x51d460!...
12
by: spibou | last post by:
Why is a pointer allowed to point to one position past the end of an array but not to one position before the beginning of an array ? Is there any reason why the former is more useful than the...
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
3
Sagittarius
by: Sagittarius | last post by:
Hi there. I have a problem concerning an UDP socket in C++ (Winsock). The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in...
3
by: john | last post by:
Hey, I know we use the pointer this to obtain a class object or class member data. I don't follow the reason for example this code. I'am quite confused assingment operator const B...
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
4
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg);...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.