Reading Memory

Discussion in 'C++' started by Maxime Savard, Feb 12, 2007.

  1. 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
    Maxime Savard, Feb 12, 2007
    #1
    1. Advertising

  2. Maxime Savard

    Ian Collins Guest

    Maxime Savard wrote:
    > 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);

    --
    Ian Collins.
    Ian Collins, Feb 12, 2007
    #2
    1. Advertising

  3. Maxime Savard wrote:
    > 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
    John Harrison, Feb 12, 2007
    #3
  4. * John Harrison:
    >
    > I think it is undefined behaviour to cast a pointer to an integer then
    > back to a pointer again.


    Technically implementation defined.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Feb 12, 2007
    #4
  5. Neat! Works just fine, and good to know why my pointer increased by 4.

    Thanks for the info John,
    Max.

    "John Harrison" <> wrote in message
    news:dt4Ah.17455$...
    > Maxime Savard wrote:
    >> 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
    Maxime Savard, Feb 12, 2007
    #5
  6. Maxime Savard

    Default User Guest

    Re: Reading Memory - TPA

    Maxime Savard wrote:

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



    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
    Default User, Feb 12, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    669
    Kevin Spencer
    Nov 11, 2004
  2. Wael Soliman

    ASP.NET Reading problem (reading .xls)

    Wael Soliman, Jan 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,788
    =?Utf-8?B?dmluYXk=?=
    Jan 3, 2005
  3. Replies:
    0
    Views:
    778
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    365
    Hrvoje Niksic
    May 25, 2007
  5. rahulthathoo

    Huge Memory Load for reading into memory

    rahulthathoo, Nov 7, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    115
    Ted Zlatanov
    Nov 10, 2006
Loading...

Share This Page