Reading Memory

M

Maxime Savard

Hi there,

What I'm trying to do is quite simple and in fact I have found a way to
do it but I am not sure if it is OK. What I mean is sometime in c++ stuff
can "work" but not be "correct". Here is my code maybe you'll see something
wrong :

char test[4] = {'1','2','3','4'};
char get = '0';

int add = (int)&test;

add++;

memcpy(&get, (void*)add, sizeof(char));

printf("Second char in array is : %c\n", get);

My objective here was to read the '2' in the test array without using
test[1] as the starting point to read. Then I discovered that when using
&test+1 as the starting address I was in fact increasing it by 4 (maybe it
is basic stuff here but I don't know why, I'm kinda newb). Then I came up
with this technique. The final question is... Is it "correct" ?

Thanks for any info!
Max.

P.s. Sorry for any typo I'm french
 
I

Ian Collins

Maxime said:
Hi there,

What I'm trying to do is quite simple and in fact I have found a way to
do it but I am not sure if it is OK. What I mean is sometime in c++ stuff
can "work" but not be "correct". Here is my code maybe you'll see something
wrong :

char test[4] = {'1','2','3','4'};
char get = '0';

int add = (int)&test;
Why are you casting to int? Naked casts are always a smell.
add++;

memcpy(&get, (void*)add, sizeof(char));
Again, why the casts? Also sizeof(char) is by definition 1.
printf("Second char in array is : %c\n", get);

My objective here was to read the '2' in the test array without using
test[1] as the starting point to read. Then I discovered that when using
&test+1 as the starting address I was in fact increasing it by 4 (maybe it
is basic stuff here but I don't know why, I'm kinda newb). Then I came up
with this technique. The final question is... Is it "correct" ?
Why didn't you just write

get = *(test+1);
 
J

John Harrison

Maxime said:
Hi there,

What I'm trying to do is quite simple and in fact I have found a way to
do it but I am not sure if it is OK. What I mean is sometime in c++ stuff
can "work" but not be "correct".
Exactly.

Here is my code maybe you'll see something
wrong :

char test[4] = {'1','2','3','4'};
char get = '0';

int add = (int)&test;

add++;

memcpy(&get, (void*)add, sizeof(char));

I think it is undefined behaviour to cast a pointer to an integer then
back to a pointer again.
printf("Second char in array is : %c\n", get);

My objective here was to read the '2' in the test array without using
test[1] as the starting point to read. Then I discovered that when using
&test+1 as the starting address I was in fact increasing it by 4 (maybe it
is basic stuff here but I don't know why, I'm kinda newb).

test + 1 would work.

&test + 1 increments the pointer by the size of the array (which is
four) because &test is a pointer to the array, but test is a pointer to
the first element of the array. It's daft but its the way C and C++ have
always worked.

Then I came up
with this technique. The final question is... Is it "correct" ?

Not really, use 'test + 1' and don't use casts.
Thanks for any info!
Max.

P.s. Sorry for any typo I'm french

john
 
A

Alf P. Steinbach

* John Harrison:
I think it is undefined behaviour to cast a pointer to an integer then
back to a pointer again.

Technically implementation defined.
 
M

Maxime Savard

Neat! Works just fine, and good to know why my pointer increased by 4.

Thanks for the info John,
Max.

John Harrison said:
Maxime said:
Hi there,

What I'm trying to do is quite simple and in fact I have found a way
to do it but I am not sure if it is OK. What I mean is sometime in c++
stuff can "work" but not be "correct".
Exactly.

Here is my code maybe you'll see something wrong :

char test[4] = {'1','2','3','4'};
char get = '0';

int add = (int)&test;

add++;

memcpy(&get, (void*)add, sizeof(char));

I think it is undefined behaviour to cast a pointer to an integer then
back to a pointer again.
printf("Second char in array is : %c\n", get);

My objective here was to read the '2' in the test array without using
test[1] as the starting point to read. Then I discovered that when using
&test+1 as the starting address I was in fact increasing it by 4 (maybe
it is basic stuff here but I don't know why, I'm kinda newb).

test + 1 would work.

&test + 1 increments the pointer by the size of the array (which is four)
because &test is a pointer to the array, but test is a pointer to the
first element of the array. It's daft but its the way C and C++ have
always worked.

Then I came up
with this technique. The final question is... Is it "correct" ?

Not really, use 'test + 1' and don't use casts.
Thanks for any info!
Max.

P.s. Sorry for any typo I'm french

john
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top