473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading Memory

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
Feb 12 '07 #1
5 1646
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.
Feb 12 '07 #2
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
Feb 12 '07 #3
* 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?
Feb 12 '07 #4
Neat! Works just fine, and good to know why my pointer increased by 4.

Thanks for the info John,
Max.

"John Harrison" <jo*************@hotmail.comwrote in message
news:dt******************@newsfe6-gui.ntli.net...
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

Feb 12 '07 #5
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>
Feb 12 '07 #6

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

Similar topics

6
by: guillaume | last post by:
I have to read and process a large ASCII file containing a mesh : a list of points and triangles. The file is 100 MBytes. I first tried to do it in memory but I think I am running out of memory...
6
by: Kevin T. Ryan | last post by:
Hi All - I'm not sure, but I'm wondering if this is a bug, or maybe (more likely) I'm misunderstanding something...see below: >>> f = open('testfile', 'w') >>> f.write('kevin\n') >>>...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
4
by: Yodai | last post by:
Hi all.. I'm trying to program an application for an emmbedded sistem where an external processor introduces data into my RAM. The thing is some if the data I have to pick up is written in...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
3
by: vbnewbie | last post by:
I've been testing some of the example streamreads on 40 meg text file. I notice that it take awhile to read in debug.write and textbox1.text multi line. but if i use a textpad program, example...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
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...
0
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...
0
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...

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.