memory address of *wchar_t

D

DDD

I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

When I debug my program, I found the above code had some strange
things. sFieldsUri+1 is equal to sValFind+0.

But if I change the codes to the following:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sFieldsUri = new w_char[100];
}

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sVal Find = new w_char[100];
}
sFieldsUri+0, sFieldsUri+1, and sValFind+0, sValFind+1 are all
difference.
Thanks for all.
 
A

Alf P. Steinbach

* DDD:
I have some codes:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

The above shouldn't compile even if 'w_char' was the name of a type.

You can't take the address of a built-in type rvalue.

As a novice, simply don't use raw pointers.


Cheers, & hth.,

- Alf
 
J

Jim Langston

DDD said:
I have some codes:
w_char **sFieldsUri;

Okay, sFieldsUri is a pointer to a pointer to a w_char.
sFieldsUri = &(new w_char[2]);

new returns a pointer to the allocated memory. You are trying to get the
address of this pointer? wtf? I don't think this is what you really want
to do.

Now, if you wanted your sFieldsUri to pointer two two arrays of w_char,
first you need to allocate memory for the pointers themselves.

sFieldsUri = new w_char*[2];
Now sFieldsUri would point to to w_char pointers. Now you could allocate
the acutal memory for the w_chars.

sFieldsUri[0] = new w_char[somesize];
sFieldsUri[1] = new w_char[somesize];

Allocating memory this way is fairly confusing and one can get lost as to
where their pointers actually point. It is better to not use pointers if
you can. What is wrong with std::string?
w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

When I debug my program, I found the above code had some strange
things. sFieldsUri+1 is equal to sValFind+0.

But if I change the codes to the following:
w_char **sFieldsUri;
sFieldsUri = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sFieldsUri = new w_char[100];
}

w_char **sValFind = nsnull;
sValFind = &(new w_char[2]);

for(int i=0; i<2; i++)
{
sVal Find = new w_char[100];
}
sFieldsUri+0, sFieldsUri+1, and sValFind+0, sValFind+1 are all
difference.
Thanks for all.
 
D

DDD

DDD said:
I have some codes:
       w_char **sFieldsUri;

Okay, sFieldsUri is a pointer to a pointer to a w_char.
       sFieldsUri = &(new w_char[2]);

new returns a pointer to the allocated memory.  You are trying to get the
address of this pointer?  wtf?  I don't think this is what you really want
to do.

Now, if you wanted your sFieldsUri to pointer two two arrays of w_char,
first you need to allocate memory for the pointers themselves.

sFieldsUri = new w_char*[2];
Now sFieldsUri would point to to w_char pointers.  Now you could allocate
the acutal memory for the w_chars.

sFieldsUri[0] = new w_char[somesize];
sFieldsUri[1] = new w_char[somesize];

Allocating memory this way is fairly confusing and one can get lost as to
where their pointers actually point.  It is better to not use pointers if
you can.  What is wrong with std::string?


       w_char **sValFind = nsnull;
       sValFind =  &(new w_char[2]);
When I debug my program, I found the above code had some strange
things. sFieldsUri+1 is equal to sValFind+0.
But if I change the codes to the following:
       w_char **sFieldsUri;
       sFieldsUri = &(new w_char[2]);
       for(int i=0; i<2; i++)
       {
               sFieldsUri = new w_char[100];
       }

       w_char **sValFind = nsnull;
       sValFind =  &(new w_char[2]);
       for(int i=0; i<2; i++)
       {
               sVal Find = new w_char[100];
       }
sFieldsUri+0,  sFieldsUri+1, and sValFind+0, sValFind+1 are all
difference.
Thanks for all.


Thank you very much, Jim. Yes, you are right. &(new w_char[2]) is not
I wanna get. And new w_char*[2] works well.
Last, I wanna say that &(new w_char[2]) works differently in debug and
release version, because sValFind = &(new w_char[2]) is a error, a
big error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,331
Latest member
ElaneLyttl

Latest Threads

Top