Hi,
I have an array like
Foo **ppArr = new Foo*[ size ];
I would like to check if the array holds a pointer to an object at a
certain index using something like
if ( ppArr[ idx ] == NULL ) { ... }
However, this statement always evaluates to false, as ppArr[ idx ]
always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7).
Do I have to set all fields explicitly to NULL (using something like
UNIX's bzero()) to get this to work?
Thanks,
Jo 12 4059
Jo Siffert wrote: Hi,
I have an array like Foo **ppArr = new Foo*[ size ];
I would like to check if the array holds a pointer to an object at a certain index using something like if ( ppArr[ idx ] == NULL ) { ... }
However, this statement always evaluates to false, as ppArr[ idx ] always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7). Do I have to set all fields explicitly to NULL (using something like UNIX's bzero()) to get this to work?
Yes. Thanks, Jo
--
Karl Heinz Buchegger kb******@gascad .at
Jo Siffert posted: Hi,
I have an array like Foo **ppArr = new Foo*[ size ];
I would like to check if the array holds a pointer to an object at a certain index using something like if ( ppArr[ idx ] == NULL ) { ... }
However, this statement always evaluates to false, as ppArr[ idx ] always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7). Do I have to set all fields explicitly to NULL (using something like UNIX's bzero()) to get this to work?
Thanks, Jo
Foo* &pArr = *(new Foo*[size]);
memset(pArr,0,s izeof(Foo*[size]));
delete [] &pArr;
Some-one may reply to this and say that setting padding bits to 0 is
undefined behaviour.
[Insert quote from Standard here]
-JKop
JKop wrote:
Foo* &pArr = *(new Foo*[size]);
memset(pArr,0,s izeof(Foo*[size]));
delete [] &pArr;
I know that you have become a newborn fan of references, but in the
above the reference doesn't buy you anthing except adding
confusion. If you do dynamic allocation, catch the pointer as
what it is: a pointer.
Foo** pArr = new Foo* [ size ];
....
delete [] pArr;
This also will correct the problem you have when doing:
Foo* &pArr = *(new Foo*[size]);
pArr[0] = NULL;
delete [] &pArr;
The assignment
pArr[0] = NULL;
does *not* do what most programmers would expect it to do:
set the 0-th pointer to NULL. Instead it tries to assign NULL
to the object pointed to by the 0-th pointer.
--
Karl Heinz Buchegger kb******@gascad .at
In article <b8************ **************@ posting.google. com>, jo********@gmx. net (Jo Siffert) wrote: I have an array like Foo **ppArr = new Foo*[ size ];
Use vector<Foo*> and all problems brought up in the discussion go away.
JKop wrote: I have an array like Foo **ppArr = new Foo*[ size ];
I would like to check if the array holds a pointer to an object at a certain index using something like if ( ppArr[ idx ] == NULL ) { ... }
However, this statement always evaluates to false, as ppArr[ idx ] always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7). Do I have to set all fields explicitly to NULL (using something like UNIX's bzero()) to get this to work?
Thanks, Jo
Foo* &pArr = *(new Foo*[size]);
memset(pArr,0,s izeof(Foo*[size]));
delete [] &pArr;
Some-one may reply to this and say that setting padding bits to 0 is undefined behaviour. ...
More than that. Setting non-padding (value-forming) bits of a pointer to
0 doesn't necessarily produce the null-pointer value of corresponding
type. In other words, it is not guaranteed that elements of the array
will compare equal to 'NULL' after that 'memset'.
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich posted: JKop wrote: I have an array like Foo **ppArr = new Foo*[ size ];
I would like to check if the array holds a pointer to an object at a certain index using something like if ( ppArr[ idx ] == NULL ) { ... }
However, this statement always evaluates to false, as ppArr[ idx ] always has a value of 0xcdcdcdcd instead of NULL (using VC++ 7). Do I have to set all fields explicitly to NULL (using something like UNIX's bzero()) to get this to work?
Thanks, Jo
Foo* &pArr = *(new Foo*[size]);
memset(pArr,0,s izeof(Foo*[size]));
delete [] &pArr;
Some-one may reply to this and say that setting padding bits to 0 is undefined behaviour. ...
More than that. Setting non-padding (value-forming) bits of a pointer to 0 doesn't necessarily produce the null-pointer value of corresponding type. In other words, it is not guaranteed that elements of the array will compare equal to 'NULL' after that 'memset'.
This I don't understand.
-JKop
JKop wrote: More than that. Setting non-padding (value-forming) bits of a pointer to 0 doesn't necessarily produce the null-pointer value of corresponding type. In other words, it is not guaranteed that elements of the array will compare equal to 'NULL' after that 'memset'.
This I don't understand.
It's pretty simple once you know it.
When setting a pointer to 'doesn't point anywhere' we write
int* pPtr = 0;
Even if the above looks like assigning the bit pattern for 0 to a pointer
variable, it need not be so. A specific platform might use a completely
different bit pattern for describing: pointer to nowhere. Well, even if
a specific platform does that, we still write pPtr = 0, and the compiler
has to replace 0 with the bit pattern used at that platform.
Pointer_value_0 != bit_pattern_for _0
The compiler can do this, because it knows about pointers and this
special case. But memset() doesn't.
--
Karl Heinz Buchegger kb******@gascad .at
Karl Heinz Buchegger posted: JKop wrote: > More than that. Setting non-padding (value-forming) bits of a > pointer to 0 doesn't necessarily produce the null-pointer value of > corresponding type. In other words, it is not guaranteed that > elements of the array will compare equal to 'NULL' after that > 'memset'. >
This I don't understand.
It's pretty simple once you know it.
When setting a pointer to 'doesn't point anywhere' we write
int* pPtr = 0;
Even if the above looks like assigning the bit pattern for 0 to a pointer variable, it need not be so. A specific platform might use a completely different bit pattern for describing: pointer to nowhere. Well, even if a specific platform does that, we still write pPtr = 0, and the compiler has to replace 0 with the bit pattern used at that platform.
Pointer_value_0 != bit_pattern_for _0
The compiler can do this, because it knows about pointers and this special case. But memset() doesn't.
Interesting! I'm assuming this is in the Standard, yes?
Similarly, if you write:
int* jk; //Global variable
It may get 0, or it may get whatever is supposed to be a NULL pointer. That
right?
-JKop
JKop wrote: Karl Heinz Buchegger posted:
JKop wrote: > More than that. Setting non-padding (value-forming) bits of a > pointer to 0 doesn't necessarily produce the null-pointer value of > corresponding type. In other words, it is not guaranteed that > elements of the array will compare equal to 'NULL' after that > 'memset'. >
This I don't understand. It's pretty simple once you know it.
When setting a pointer to 'doesn't point anywhere' we write
int* pPtr = 0;
Even if the above looks like assigning the bit pattern for 0 to a pointer variable, it need not be so. A specific platform might use a completely different bit pattern for describing: pointer to nowhere. Well, even if a specific platform does that, we still write pPtr = 0, and the compiler has to replace 0 with the bit pattern used at that platform.
Pointer_value_0 != bit_pattern_for _0
The compiler can do this, because it knows about pointers and this special case. But memset() doesn't.
Interesting! I'm assuming this is in the Standard, yes?
Yes. of course
But it's not written down that way. It follows from some rules that
turn around null pointer values, null pointer constants and their
required behaviour. Similarly, if you write:
int* jk; //Global variable
It may get 0, or it may get whatever is supposed to be a NULL pointer. That right?
It may be everything. No initialization -> undefined (except in static
cases. God how I hate those exceptions everywhere:
static int i;
i has a value of 0. Even without initialization. It's a leftover from C)
--
Karl Heinz Buchegger kb******@gascad .at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Fred Zwarts |
last post by:
If I am right, members of a class that are const and not static
must be initialized in the initialization part of a constructor.
E.g.
class C {
private:
const int I;
public:
C();
};
|
by: simondex |
last post by:
Hi, Everyone!
Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.
Truly Yours, Simon Dexter
|
by: Henry |
last post by:
I finally thought I had an understanding of multi dimensional arrays in C
when I get this:
#include <stdio.h>
#define max_x 3
#define max_y 5
int array;
|
by: Peter B. Steiger |
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was static; if the input
file contained more than entries, tough.
This time I want to do it right - use a dynamic array that increases in
size with each word read from the file. A few test programs that make use
of **List and realloc( List, blah...
|
by: Charles Sullivan |
last post by:
Assume I have a static array of structures the elements of which
could be any conceivable mixture of C types, pointers, arrays.
And this array is uninitialized at program startup.
If later in the program I wish to return this array to its
startup state, can this be accomplished by writing binary
zeroes to the entire memory block with memset(). E.g.,
static struct mystruct_st {
int x1;
| |
by: kk_oop |
last post by:
Hi. I recently wrote a simple little template that defines an array
that checks attempts to use out of bounds indexes. The only problem is
that it does provide the use array style value initialization when the
type is instantiated. Any suggestions for a mod that would allow array
initialization syntax?
***********Here's the type:
#ifndef CHECKED_ARRAY_
#define CHECKED_ARRAY_
|
by: toton |
last post by:
Hi,
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
Test x = {Test(3),Test(6)}; using array initialization list. (Note
Test do NOT have a default ctor).
Is it possible to do so in the class parameter initialization using
specific ctor?
|
by: jamx |
last post by:
How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array;
};
|
by: anon.asdf |
last post by:
Hi!
Q. 1)
How does one write:
sizeof(array of 5 "pointers to double")
???
I know that
sizeof(pointer to an array of 5 doubles) can be written as:
sizeof(double (*));
|
by: vippstar |
last post by:
The subject might be misleading.
Regardless, is this code valid:
#include <stdio.h>
void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |