473,385 Members | 1,606 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,385 software developers and data experts.

memset & alignment

Please have a look at the following code snippet.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void* func ( int i )
{
void* ptr = malloc(sizeof(int));
memset(ptr, i, sizeof(int));
return ptr;
}

int main()
{
int i = 123;
int res = *((int*)func(i));
printf("i is %d\n", res);
return 0;
}

[l34@premier rnd]$ gcc p1.c
[l34@premier rnd]$ ./a.out
i is 2071690107

I don't understand what the problrm is, as I was expecting 123 to get
printed. Can somebody tell why this garbage is being printed.

Thanks.

Aug 8 '06 #1
3 4893
dragoncoder wrote:
memset(ptr, i, sizeof(int));
memcpy(ptr, &i, sizeof i);

--
pete
Aug 8 '06 #2
dragoncoder wrote:
Please have a look at the following code snippet.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void* func ( int i )
{
void* ptr = malloc(sizeof(int));
memset(ptr, i, sizeof(int));
You want to set each byte of the int-sized object at *ptr to
i? OK. Seems a bit strange, but you know best.
return ptr;
}

int main()
{
int i = 123;
int res = *((int*)func(i));
printf("i is %d\n", res);
return 0;
}

[l34@premier rnd]$ gcc p1.c
[l34@premier rnd]$ ./a.out
i is 2071690107

I don't understand what the problrm is, as I was expecting 123 to get
printed.
Bad expectation.

If `sizeof int` is 4 and there are 8 bits-per-byte (likely guesses), you've
set `res` to

(123 << 24) | (123 << 16) | (123 << 8) | 123

which by an amazing coincidence prints as 2071690107.

Surely surely surely you would have been better with

int *func( int i )
{
int *ptr = malloc( sizeof (*ptr) );
*ptr = i;
return ptr;
}
??

--
Chris "seeker" Dollin
"Who are you? What do you want?" /Babylon 5/

Aug 8 '06 #3

dragoncoder ha escrito:
Please have a look at the following code snippet.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void* func ( int i )
{
void* ptr = malloc(sizeof(int));
memset(ptr, i, sizeof(int));
return ptr;
}

int main()
{
int i = 123;
int res = *((int*)func(i));
printf("i is %d\n", res);
return 0;
}

[l34@premier rnd]$ gcc p1.c
[l34@premier rnd]$ ./a.out
i is 2071690107

I don't understand what the problrm is, as I was expecting 123 to get
printed. Can somebody tell why this garbage is being printed.

Thanks.
memset(ptr, i, n) sets each byte of n bytes pointed by ptr to i.
If n is 4, what you get pointed by ptr is:

01111011 01111011 01111011 01111011

123(decimal) = 01111011(binary)

which is 2071690107 in decimal.

Aug 8 '06 #4

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

Similar topics

53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
2
by: Stan Sainte-Rose | last post by:
Hello, I need to know how to format Integer and Real with the datagrid. In fact, I want to use the right alignment. When I use this option the header text comes with the right alignment too. :( ...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
21
by: Angus | last post by:
I need to convert a double to a 64 bit integer. I am using __int64 but could of course use a more portable type. To perform the correct casts: double d = 3.3; __int64 i64; i64 =...
18
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as...
5
by: mdh | last post by:
The 3rd paragraph says: "Alignment requirements can generally be satisfied easily, at the cost of some wasted space, by ensuring that the allocator always return a poiner that meets all (...
38
by: Bill Cunningham | last post by:
When I want to clear memory space this is what I typically do myself, char a; int i; for (i=0;i != 100;++i) a='\0'; Now with the function memset I could do the same thing and it would be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.