473,724 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange pointer problem, now ansi example

Hi All,

i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.

----------------------------------------------
//example 1:
typedef int t_Array[10];
int main(int argc, char* argv[])
{
t_Array array;
array[0] = 123;
array[9] = 321;

t_Array *ptrArray[] = {&array};
t_Array *element = ptrArray[0];

printf("array = 0x%08x, ptrArray[0] = 0x%08x, element = 0x%08x, *element =
0x%08x\n",
array, ptrArray[0], element, *element);

printf("array[9] = %d, (*element)[9] = %d, element[9] = %d\n",
array[9], (*element)[9], element[9]);

return 0;
}

now according to the print statements, the different pointers all point to
the same thing, even though 'element' is dereferenced at one place, and not
dereferenced at another place. if i try to print a value from the array, i
get 2 different results.
??
----------------------------------------------
example 2:
typedef int t_Array[10];
void function(t_Arra y Array)
{
t_Array *ptrArray[] = {&Array}; //error
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
return 0;
}
the 2 declarations of the arrays look the same to me, but 1 gives an error,
and the other one doesn't. i have looked up the documentation of that
error, but that didn't shine much light.
??

----------------------------------------------
example 3:
typedef int t_Array[10];
void function(t_Arra y Array)
{
t_Array *element = NULL;
void * ptrArray[] = {NULL};

ptrArray[0] = &Array; //first element should be pointer to a t_Array
element = (t_Array *)ptrArray[0];
printf("Weird Failure: (*element)[9] = %d\n", (*element)[9]);

ptrArray[0] = Array; //first element should be Array itself
element = (t_Array *)ptrArray[0];
printf("Weird Success: (*element)[9] = %d\n", (*element)[9]);
}
int main(int argc, char* argv[])
{
t_Array array;
array[9] = 321;
function(array) ;
return 0;
}
this is what really caused my headaches. i used void pointers to get rid of
the error, but that caused weird behavior. in the first situation in
'function', i expect to dereference a pointer to end up with an array, but
that clearly is not what happens.
in the second situation i expect to do something illegal: ie to use a
pointer to an array as the array itself, and somehow that seems to work.
what do i miss here?

i would be very grateful for some light on this murky behavior (or at least
my murky understanding).

kind regards,
Bruno.
Jul 27 '05 #1
3 2359
Bruno van Dooren wrote:
i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.

----------------------------------------------
//example 1:
typedef int t_Array[10];
't_Array' is a synonym for an array of 10 ints.
int main(int argc, char* argv[])
{
t_Array array;
'array' is an array of 10 ints.
array[0] = 123;
array[9] = 321;
The rest is left uninitialised.

t_Array *ptrArray[] = {&array};
'ptrArray' is an array of (1) pointers to arrays of 10 ints.
t_Array *element = ptrArray[0];
'element' is a pointer to an array of 10 ints, initialised from the first
element of 'ptrArray' array.

printf("array = 0x%08x, ptrArray[0] = 0x%08x, element = 0x%08x, *element =
0x%08x\n",
array, ptrArray[0], element, *element);
Here you print the address of the first element of the 'array' (a pointer
to an int), then the address of the 'array' array (a pointer to an array
of 10 ints), then the same thing (as 'element' since you initialised it to
the same value as 'ptrArray[0]'), then the address of the first element of
the array to which 'element' points (IOW, the address of the first element
of 'array' array). They are all the same location in memory.

printf("array[9] = %d, (*element)[9] = %d, element[9] = %d\n",
array[9], (*element)[9], element[9]);
Here 'array[9]' is an int. (*element)[9] is another int (the same as
'array[9]'), and 'element[9]' is the _tenth_array_of _10_ints_sittin g_in_
_memory_countin g_from_'array'_ , which *doesn't exist* since 'array' is
the only array there is. This statement has undefined behaviour because
you're dereferencing an invalid pointer ('element[9]').

return 0;
}

now according to the print statements, the different pointers all point to
the same thing, even though 'element' is dereferenced at one place, and not
dereferenced at another place. if i try to print a value from the array, i
get 2 different results.
Only 2 different results?
??
----------------------------------------------
example 2:
typedef int t_Array[10];
void function(t_Arra y Array)
'Array' is passed by value? Most likely it decays to a pointer to 'int'
here since arrays cannot be passed by value.
{
t_Array *ptrArray[] = {&Array}; //error
Of course. You're trying to initialise a pointer to an array from the
address of a pointer. Different types.

To learn what your 'Array' here is, use <typeinfo> and do

printf("The type of 'Array' is (%s)\n", typeid(Array).n ame());
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
return 0;
}
the 2 declarations of the arrays look the same to me, but 1 gives an error,
and the other one doesn't. i have looked up the documentation of that
error, but that didn't shine much light.
??
Do a bit of experimenting before asking:
-----
// example 2:
#include <stdio.h>
#include <typeinfo>

typedef int t_Array[10];
void function(t_Arra y Array)
{
printf("%s\n", typeid(Array).n ame());
// t_Array *ptrArray[] = {&Array}; //error
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
function(array) ;
return 0;
}
-----
----------------------------------------------
example 3:
typedef int t_Array[10];
void function(t_Arra y Array)
Again 'Array' is a pointer to int.
{
t_Array *element = NULL;
void * ptrArray[] = {NULL};

ptrArray[0] = &Array; //first element should be pointer to a t_Array
The first element is a pointer to 'void'. Why should it be anything but?
element = (t_Array *)ptrArray[0];
Now you're invoking undefined behaviour because to begin with, 'Array' is
not a pointer to a t_Array.
printf("Weird Failure: (*element)[9] = %d\n", (*element)[9]);
Of course it's weird. What else should undefined behaviour be?

ptrArray[0] = Array; //first element should be Array itself
element = (t_Array *)ptrArray[0];
printf("Weird Success: (*element)[9] = %d\n", (*element)[9]);
There is no success or failure here. The behaviour is still undefined.
}
int main(int argc, char* argv[])
{
t_Array array;
array[9] = 321;
function(array) ;
return 0;
}
this is what really caused my headaches. i used void pointers to get rid of
the error, but that caused weird behavior. in the first situation in
'function', i expect to dereference a pointer to end up with an array, but
that clearly is not what happens.
Yes. Arrays in C++ are rather strange things. They are prone to decay.
The main reason for your troubles is that you don't take the decay into
consideration.
in the second situation i expect to do something illegal: ie to use a
pointer to an array as the array itself, and somehow that seems to work.
what do i miss here?

i would be very grateful for some light on this murky behavior (or at least
my murky understanding).


Ask more questions after you read my answers.

V
Jul 27 '05 #2
i will post my questions tomorrow after i have had time to read your reply
thouroughly. it is now 22:30 (Belgium) and i think this is a good time to
close my laptop and go to bed :).

thanks in advance.
Bruno.

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:DG******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
Bruno van Dooren wrote:
i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.

----------------------------------------------
//example 1:
typedef int t_Array[10];


't_Array' is a synonym for an array of 10 ints.
int main(int argc, char* argv[])
{
t_Array array;


'array' is an array of 10 ints.
array[0] = 123;
array[9] = 321;


The rest is left uninitialised.

t_Array *ptrArray[] = {&array};


'ptrArray' is an array of (1) pointers to arrays of 10 ints.
t_Array *element = ptrArray[0];


'element' is a pointer to an array of 10 ints, initialised from the first
element of 'ptrArray' array.

printf("array = 0x%08x, ptrArray[0] = 0x%08x, element = 0x%08x,
*element =
0x%08x\n",
array, ptrArray[0], element, *element);


Here you print the address of the first element of the 'array' (a pointer
to an int), then the address of the 'array' array (a pointer to an array
of 10 ints), then the same thing (as 'element' since you initialised it to
the same value as 'ptrArray[0]'), then the address of the first element of
the array to which 'element' points (IOW, the address of the first element
of 'array' array). They are all the same location in memory.

printf("array[9] = %d, (*element)[9] = %d, element[9] = %d\n",
array[9], (*element)[9], element[9]);


Here 'array[9]' is an int. (*element)[9] is another int (the same as
'array[9]'), and 'element[9]' is the _tenth_array_of _10_ints_sittin g_in_
_memory_countin g_from_'array'_ , which *doesn't exist* since 'array' is
the only array there is. This statement has undefined behaviour because
you're dereferencing an invalid pointer ('element[9]').

return 0;
}

now according to the print statements, the different pointers all point
to
the same thing, even though 'element' is dereferenced at one place, and
not
dereferenced at another place. if i try to print a value from the array,
i
get 2 different results.


Only 2 different results?
??
----------------------------------------------
example 2:
typedef int t_Array[10];
void function(t_Arra y Array)


'Array' is passed by value? Most likely it decays to a pointer to 'int'
here since arrays cannot be passed by value.
{
t_Array *ptrArray[] = {&Array}; //error


Of course. You're trying to initialise a pointer to an array from the
address of a pointer. Different types.

To learn what your 'Array' here is, use <typeinfo> and do

printf("The type of 'Array' is (%s)\n", typeid(Array).n ame());
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
return 0;
}
the 2 declarations of the arrays look the same to me, but 1 gives an
error, and the other one doesn't. i have looked up the documentation of
that
error, but that didn't shine much light.
??


Do a bit of experimenting before asking:
-----
// example 2:
#include <stdio.h>
#include <typeinfo>

typedef int t_Array[10];
void function(t_Arra y Array)
{
printf("%s\n", typeid(Array).n ame());
// t_Array *ptrArray[] = {&Array}; //error
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
function(array) ;
return 0;
}
-----

----------------------------------------------
example 3:
typedef int t_Array[10];
void function(t_Arra y Array)


Again 'Array' is a pointer to int.
{
t_Array *element = NULL;
void * ptrArray[] = {NULL};

ptrArray[0] = &Array; //first element should be pointer to a t_Array


The first element is a pointer to 'void'. Why should it be anything but?
element = (t_Array *)ptrArray[0];


Now you're invoking undefined behaviour because to begin with, 'Array' is
not a pointer to a t_Array.
printf("Weird Failure: (*element)[9] = %d\n", (*element)[9]);


Of course it's weird. What else should undefined behaviour be?

ptrArray[0] = Array; //first element should be Array itself
element = (t_Array *)ptrArray[0];
printf("Weird Success: (*element)[9] = %d\n", (*element)[9]);


There is no success or failure here. The behaviour is still undefined.
}
int main(int argc, char* argv[])
{
t_Array array;
array[9] = 321;
function(array) ;
return 0;
}
this is what really caused my headaches. i used void pointers to get rid
of
the error, but that caused weird behavior. in the first situation in
'function', i expect to dereference a pointer to end up with an array,
but
that clearly is not what happens.


Yes. Arrays in C++ are rather strange things. They are prone to decay.
The main reason for your troubles is that you don't take the decay into
consideration.
in the second situation i expect to do something illegal: ie to use a
pointer to an array as the array itself, and somehow that seems to work.
what do i miss here?

i would be very grateful for some light on this murky behavior (or at
least
my murky understanding).


Ask more questions after you read my answers.

V

Jul 27 '05 #3
>> ----------------------------------------------
//example 1:
typedef int t_Array[10];
't_Array' is a synonym for an array of 10 ints.
int main(int argc, char* argv[])
{
t_Array array;


'array' is an array of 10 ints.
array[0] = 123;
array[9] = 321;


The rest is left uninitialised.

t_Array *ptrArray[] = {&array};


'ptrArray' is an array of (1) pointers to arrays of 10 ints.
t_Array *element = ptrArray[0];


'element' is a pointer to an array of 10 ints, initialised from the first
element of 'ptrArray' array.

printf("array = 0x%08x, ptrArray[0] = 0x%08x, element = 0x%08x,
*element =
0x%08x\n",
array, ptrArray[0], element, *element);


Here you print the address of the first element of the 'array' (a pointer
to an int), then the address of the 'array' array (a pointer to an array
of 10 ints), then the same thing (as 'element' since you initialised it to
the same value as 'ptrArray[0]'), then the address of the first element of
the array to which 'element' points (IOW, the address of the first element
of 'array' array). They are all the same location in memory.

if i understand you correctly, element is a pointer to the first value, and
(*element) is a pointer to the first value. how can that be? the
dereferencing doesn't seem to make a difference.

printf("array[9] = %d, (*element)[9] = %d, element[9] = %d\n",
array[9], (*element)[9], element[9]);
Here 'array[9]' is an int. (*element)[9] is another int (the same as
'array[9]'), and 'element[9]' is the _tenth_array_of _10_ints_sittin g_in_
_memory_countin g_from_'array'_ , which *doesn't exist* since 'array' is
the only array there is. This statement has undefined behaviour because
you're dereferencing an invalid pointer ('element[9]').

but the previous print statement showed that element and *element are the
same. if so, then they are both pointers to the first element in the array.
in that case, the [] operator should have the same behavior?

return 0;
}

now according to the print statements, the different pointers all point
to
the same thing, even though 'element' is dereferenced at one place, and
not
dereferenced at another place. if i try to print a value from the array,
i
get 2 different results.
Only 2 different results?
??
----------------------------------------------
example 2:
typedef int t_Array[10];
void function(t_Arra y Array)


'Array' is passed by value? Most likely it decays to a pointer to 'int'
here since arrays cannot be passed by value.
{
t_Array *ptrArray[] = {&Array}; //error


Of course. You're trying to initialise a pointer to an array from the
address of a pointer. Different types.

so they are different because Array is now a real pointer, located on the
stack somewhere, and not the array itself?

To learn what your 'Array' here is, use <typeinfo> and do

printf("The type of 'Array' is (%s)\n", typeid(Array).n ame());
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
return 0;
}
the 2 declarations of the arrays look the same to me, but 1 gives an
error, and the other one doesn't. i have looked up the documentation of
that
error, but that didn't shine much light.
??
Do a bit of experimenting before asking:

the explanation is the same as your previous one (i think): in the no error
case, the array is a real array, while in the subroutine it has decayed to a
real pointer.
\ -----
// example 2:
#include <stdio.h>
#include <typeinfo>

typedef int t_Array[10];
void function(t_Arra y Array)
{
printf("%s\n", typeid(Array).n ame());
// t_Array *ptrArray[] = {&Array}; //error
}
int main(int argc, char* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
function(array) ;
return 0;
}
-----

----------------------------------------------
example 3:
typedef int t_Array[10];
void function(t_Arra y Array)
Again 'Array' is a pointer to int.
{
t_Array *element = NULL;
void * ptrArray[] = {NULL};

ptrArray[0] = &Array; //first element should be pointer to a t_Array


The first element is a pointer to 'void'. Why should it be anything but?

its type is void, but what it points to is the array. my mistake here is
that i assumed that it would be a pointer to a pointer, instead of a pointer
to the first element.
element = (t_Array *)ptrArray[0];
Now you're invoking undefined behaviour because to begin with, 'Array' is
not a pointer to a t_Array.

indeed. i thought i had a pointer to a pointer, but instead i have a pointer
to the first element.
printf("Weird Failure: (*element)[9] = %d\n", (*element)[9]);
Of course it's weird. What else should undefined behaviour be?

in that case this should fail, since my assumption was not valid.. it seems
as if i am dereferencing whatever value is in the first element.


ptrArray[0] = Array; //first element should be Array itself
element = (t_Array *)ptrArray[0];
printf("Weird Success: (*element)[9] = %d\n", (*element)[9]);
There is no success or failure here. The behaviour is still undefined.

the reason that this works is that (probably) the value of the pointer is
copied into the first element of prtArray, so dereferencing that value would
give me the address again, on which i could then (legally) use the []
operator on it to end up with a value in my original array.
}
int main(int argc, char* argv[])
{
t_Array array;
array[9] = 321;
function(array) ;
return 0;
}
this is what really caused my headaches. i used void pointers to get rid
of
the error, but that caused weird behavior. in the first situation in
'function', i expect to dereference a pointer to end up with an array,
but
that clearly is not what happens.
Yes. Arrays in C++ are rather strange things. They are prone to decay.
The main reason for your troubles is that you don't take the decay into
consideration.

indeed.
in the second situation i expect to do something illegal: ie to use a
pointer to an array as the array itself, and somehow that seems to work.
what do i miss here?

i would be very grateful for some light on this murky behavior (or at
least
my murky understanding).


Ask more questions after you read my answers.

i think that i understan now (or at least know what i did wrong)
the main reason for my confusion stems from using the typedef. that led me
to believe that &array would give me a pointer to a pointer, instead of a
pointer to the first value.

i still don't understand though why (in my first example) element and
*element have the same value, yet using the [] operator on them gives
different results.

thanks for your reply.

kind regards,
Bruno.

V


Jul 28 '05 #4

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

Similar topics

5
2308
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece of code in question always crashes in an STL operation such as a vector.push_back, but the location of the crash changes as I change how various parts are handled in memory, i.e., make some things dynamically allocated instead of new'd. I know...
79
125229
by: syntax | last post by:
what is the size of a pointer? suppose i am writing, datatype *ptr; sizeof(ptr); now what does this sizeof(ptr) will give? will it give the size of the
204
13041
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
26
774
by: Meenu | last post by:
Hi, Can two different far pointers contain two different addresses but refer to the same location in memory?
10
1762
by: weichaoliu | last post by:
I know that this problem is concerned to GCC. But I hope somebody here can tell me some detailed things about why. The version of g++ I'm using is: g++ (GCC) 3.4.4 (mingw special). The code is: // BEGIN #include<iostream> int main(void) {
7
2858
by: Flash Gordon | last post by:
Reading the standard, va_list is an object type (, so I believe the following should be possible: #include <stdarg.h> void foo(va_list *arg) { /* do some stuff which conditionally might read parameters off arg */ }
17
2718
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This compiler I use for the Samsung 16 bit Smartcard chips and I want to know if it is compliant with the ANSI standard or if it violates it. Have a look at this super simple example, where the value of b is incorrect:
26
3053
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
4
3383
by: gw7rib | last post by:
I'm using a system in which TCHAR is typedef-ed to represent a character, in this case a wchar_t to hold Unicode characters, and LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume it's supposed to be a pointer to constant TCHAR, though they seem to be defined in parallel rather than one typedef using the other. I'm perfectly happy using LPCTSTR for a constant string, but for some reason it seems odd to use LPCTSTR as an...
0
8868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8741
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,...
0
9389
Oralloy
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...
0
9090
tracyyun
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...
0
8063
agi2029
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...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2149
bsmnconsultancy
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...

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.