471,049 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Pointer being set to null (not by me)??

Hello,

I have an executable that is calling a DLL I have built. The DLL
requires two static libraries to run (genuine statics as opposed to
the 'stub' .lib files that still require a DLL).

In the DLL source I have the following code...

{
char *l_H = (char *)malloc(200);
sprintf(l_H, "Hello");

namespace1::namespace2::namespace3::Thing l_Thing;

free(l_H);
}

....now the strange thing is that after executing the initialization of
l_Thing, l_H has been set to NULL (0x00000000).

I have a breakpoint that is triggered when free(l_H) is reached (I
know for a fact that the free() has not yet executed as VC++
breakpoints are triggered before a line of code is processed. Also I
have had other lines of code between free() and the initialization of
Thing - not shown here - where I see l_H has been reset). Also, l_H is
not going out of scope before the free() is reached.

l_H is local and Thing knows nothing about it. The constructor of
Thing is as follows (in .cpp file)...

namespace namespace1
{
namespace namespace2
{
namespace namespace3
{
Thing::Thing()
{
m_D = 0;
}

...other code ...

}
}
}

....where m_D is declared as a double with private access in the header
file.

Observations
------------

- I declare m_D as an int, the code behaves as expected (i.e. "Hello"
remains in l_H after the initialization of Thing).

- If I remove m_D = 0 from the constructor of Thing, the code works
normally

- If I declare a double and assign it a value in the main code (after
removing m_D = 0 from the Thing constructor) after the initialization
of Thing, the code works as normal.

I am posting this in microsoft.public.vc.language and comp.lang.c++ as
I am not sure whether it's a Microsoft thing or a C++ issue.

Can anyone explain this?

Thanks,

Matthew
Jul 19 '05 #1
4 2911
Are you sure that malloc() isn't returning NULL? Pointers don't initialize
themselves to NULL, the compiler just picks some random address (usually
invalid). VC++ in debug mode picks 0xCCCCCCCC, therefore we know it's
actually being set to 1_H. Check to make sure malloc() isn't returning NULL,
which would indicate an error.

"Matthew" <de**@webfuture.com> wrote in message
news:8d**************************@posting.google.c om...
Hello,

I have an executable that is calling a DLL I have built. The DLL
requires two static libraries to run (genuine statics as opposed to
the 'stub' .lib files that still require a DLL).

In the DLL source I have the following code...

{
char *l_H = (char *)malloc(200);
sprintf(l_H, "Hello");

namespace1::namespace2::namespace3::Thing l_Thing;

free(l_H);
}

...now the strange thing is that after executing the initialization of
l_Thing, l_H has been set to NULL (0x00000000).

I have a breakpoint that is triggered when free(l_H) is reached (I
know for a fact that the free() has not yet executed as VC++
breakpoints are triggered before a line of code is processed. Also I
have had other lines of code between free() and the initialization of
Thing - not shown here - where I see l_H has been reset). Also, l_H is
not going out of scope before the free() is reached.

l_H is local and Thing knows nothing about it. The constructor of
Thing is as follows (in .cpp file)...

namespace namespace1
{
namespace namespace2
{
namespace namespace3
{
Thing::Thing()
{
m_D = 0;
}

...other code ...

}
}
}

...where m_D is declared as a double with private access in the header
file.

Observations
------------

- I declare m_D as an int, the code behaves as expected (i.e. "Hello"
remains in l_H after the initialization of Thing).

- If I remove m_D = 0 from the constructor of Thing, the code works
normally

- If I declare a double and assign it a value in the main code (after
removing m_D = 0 from the Thing constructor) after the initialization
of Thing, the code works as normal.

I am posting this in microsoft.public.vc.language and comp.lang.c++ as
I am not sure whether it's a Microsoft thing or a C++ issue.

Can anyone explain this?

Thanks,

Matthew

Jul 19 '05 #2

"MiniDisc_2k2" <Ma******@nospam.com> wrote in message
news:Kh*****************@news2.east.cox.net...
invalid). VC++ in debug mode picks 0xCCCCCCCC, therefore we know it's
actually being set to 1_H.


I'm sorry that's "therefore we know it's actually being set to __NULL__"
Jul 19 '05 #3
Thanks for the reply.

The malloc is not returning NULL as when I step over the sprintf
statement in the IDE debugger, I see "Hello" in l_H.

"MiniDisc_2k2" <Ma******@nospam.com> wrote in message news:<Kh*****************@news2.east.cox.net>...
Are you sure that malloc() isn't returning NULL? Pointers don't initialize
themselves to NULL, the compiler just picks some random address (usually
invalid). VC++ in debug mode picks 0xCCCCCCCC, therefore we know it's
actually being set to 1_H. Check to make sure malloc() isn't returning NULL,
which would indicate an error.

"Matthew" <de**@webfuture.com> wrote in message
news:8d**************************@posting.google.c om...
Hello,

I have an executable that is calling a DLL I have built. The DLL
requires two static libraries to run (genuine statics as opposed to
the 'stub' .lib files that still require a DLL).

In the DLL source I have the following code...

{
char *l_H = (char *)malloc(200);
sprintf(l_H, "Hello");

namespace1::namespace2::namespace3::Thing l_Thing;

free(l_H);
}

...now the strange thing is that after executing the initialization of
l_Thing, l_H has been set to NULL (0x00000000).

I have a breakpoint that is triggered when free(l_H) is reached (I
know for a fact that the free() has not yet executed as VC++
breakpoints are triggered before a line of code is processed. Also I
have had other lines of code between free() and the initialization of
Thing - not shown here - where I see l_H has been reset). Also, l_H is
not going out of scope before the free() is reached.

l_H is local and Thing knows nothing about it. The constructor of
Thing is as follows (in .cpp file)...

namespace namespace1
{
namespace namespace2
{
namespace namespace3
{
Thing::Thing()
{
m_D = 0;
}

...other code ...

}
}
}

...where m_D is declared as a double with private access in the header
file.

Observations
------------

- I declare m_D as an int, the code behaves as expected (i.e. "Hello"
remains in l_H after the initialization of Thing).

- If I remove m_D = 0 from the constructor of Thing, the code works
normally

- If I declare a double and assign it a value in the main code (after
removing m_D = 0 from the Thing constructor) after the initialization
of Thing, the code works as normal.

I am posting this in microsoft.public.vc.language and comp.lang.c++ as
I am not sure whether it's a Microsoft thing or a C++ issue.

Can anyone explain this?

Thanks,

Matthew

Jul 19 '05 #4

Matthew wrote:

Hello,

I have an executable that is calling a DLL I have built. The DLL
requires two static libraries to run (genuine statics as opposed to
the 'stub' .lib files that still require a DLL).

In the DLL source I have the following code...

{
char *l_H = (char *)malloc(200);
sprintf(l_H, "Hello");

namespace1::namespace2::namespace3::Thing l_Thing;

free(l_H);
}

...now the strange thing is that after executing the initialization of
l_Thing, l_H has been set to NULL (0x00000000).

I have a breakpoint that is triggered when free(l_H) is reached (I
know for a fact that the free() has not yet executed as VC++
breakpoints are triggered before a line of code is processed. Also I
have had other lines of code between free() and the initialization of
Thing - not shown here - where I see l_H has been reset). Also, l_H is
not going out of scope before the free() is reached.

l_H is local and Thing knows nothing about it. The constructor of
Thing is as follows (in .cpp file)...

namespace namespace1
{
namespace namespace2
{
namespace namespace3
{
Thing::Thing()
{
m_D = 0;
}

...other code ...

}
}
}

...where m_D is declared as a double with private access in the header
file.

Observations
------------

- I declare m_D as an int, the code behaves as expected (i.e. "Hello"
remains in l_H after the initialization of Thing).

- If I remove m_D = 0 from the constructor of Thing, the code works
normally

- If I declare a double and assign it a value in the main code (after
removing m_D = 0 from the Thing constructor) after the initialization
of Thing, the code works as normal.

I am posting this in microsoft.public.vc.language and comp.lang.c++ as
I am not sure whether it's a Microsoft thing or a C++ issue.

Can anyone explain this?


I'm a bit curious of the "other code" in the Thing's contructor, if
you're overwriting say an array on the stack here, you might get exactly
this behaviour.
Change in behaviour when adding/removing also points in this direction.

Overwriring the stack is an evil and often hard-to-get error...

Does i really fails in a sample as small as what you've shown?
-Alf
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sathyashrayan | last post: by
41 posts views Thread by Alexei A. Frounze | last post: by
23 posts views Thread by bluejack | last post: by
69 posts views Thread by fieldfallow | last post: by
27 posts views Thread by Erik de Castro Lopo | last post: by
2 posts views Thread by toton | last post: by
15 posts views Thread by khan | last post: by
2 posts views Thread by Giorgos Keramidas | last post: by
30 posts views Thread by ggnguser | last post: by

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.