473,396 Members | 2,052 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.

Pointer issue on IA-32 and IA-64

I've seen a puzzle while surfing. It is given below.
The following C program segfaults of IA-64, but works fine on IA-32.
Expand|Select|Wrap|Line Numbers
  1.   int main()
  2.   {
  3.       int* p;
  4.       p = (int*)malloc(sizeof(int));
  5.       *p = 10;
  6.       return 0;
  7.   }
  8.  
Why does it happen so?
I think, the problem is related to pointer type conversion. I don't have a 64-bit processor. I was unable to fully elaborate it.
Please explain why does this program behaves differently on different architectures.
Thanks.
May 11 '09 #1
9 2873
JosAH
11,448 Expert 8TB
@Aftabpasha
If that code snippet results in a seg-fault the malloc implementation is broken.

kind regards,

Jos
May 11 '09 #2
But the malloc() just allocates a BLOCK OF MEMORY of specified size and returns a void pointer to it's starting address. That's why I thought the problem is some where in pointer conversion process. Just don't know where exactly it is.
The following is the link to the puzzle
http://www.gowrikumar.com/c/index.html

I just saw an old discussion on the same puzzle, but I ended up with confusions. Here is the link to it
http://www.velocityreviews.com/forum...uzzlequot.html

Please explain it.
Thanks.
May 11 '09 #3
JosAH
11,448 Expert 8TB
@Aftabpasha
Not just any pointer value: malloc returns a pointer value that is suitably aligned for any data type (usually a multiple of 8 or 16).

kind regards,

Jos

ps. found it: malloc() isn't prototyped so the compiler assumes an int return value. If a pointer happens to be larger than an int disaster will happen.
May 11 '09 #4
I don't think protottyping is a reason. If proper header file for malloc() is not included, then compiler would show an error message rather than trying to return an integer (default return type).

I think Jos is right. I guess it is faulty malloc implementation that is triggering this problem. I think the things are going as described below.

According to the C standard, malloc() always returns a suitably aligned address. It is essential because the address (of type void *) can to be further converted to other other types (e.g. int *).

I think, in the above problem, probably the malloc() (or faulty implementation of malloc) is not giving a suitably aligned address on IA-64. Eventually this resulted in improper pointer conversion from void * to int *. On an IA-64 (x86) machine, which is of type Little Endian, this conversion might have pointed pointer p to some restricted address. Hence generating a segmentation fault.

I'm not sure about the above explanation. But thats what I can guess.
Please have some comment on it.

Thanks and kind regards.

-Aftab
May 12 '09 #5
newb16
687 512MB
C ( not C++) compiler may issue a warning but not an error. More surfing proves that the root cause is indeed missing prototype.
May 12 '09 #6
Your problem is here:
p = (int*)malloc(sizeof(int));

You sizeof(int) is 4 bytes in most compilers. What you need is sizeof(int*).
May 12 '09 #7
Banfa
9,065 Expert Mod 8TB
That is completely wrong. sizeof(int) is correct as the code is trying to allocate memory to hold an int not enough memory to hold an int *.

On a 64bit system it is reasonable to assume that the point will be 64bits. If the prototype isn't there then the compiler will assume that malloc returns int which is normally 32bits on modern 64bit systems so only 1/2 the bits of the pointer get copied and the error occurs. This doesn't happen on a 32bit system because generally both int and pointer have 32bits so assuming malloc returns int is not so disastrous.
May 12 '09 #8
I completely missed the 'only C' perspective of the problem. Now all doubts are cleared.
Thank you all for their quick attention and helpful comments.

Regards,

Aftab.
May 13 '09 #9
In C, if the prototype for a function is not provided, the compiler
assumes that the function takes an integer and returns an integer. As
a result malloc would be treated as returning an integer instead of a
pointer.
On 32 -bit systems, both integer and pointer are 32-bits, hence the
program works fine.
(the model is called ILP-32 : Integer, Long, Pointer are 32-bits)
But on Linux/IA-64 with gcc, the default model is LP-64, where int is
32 bits and long,pointer are 64 bits. As a result of it the pointer
gets truncated to 32-bits and hence causes the segfault.
Feb 24 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jacek Dziedzic | last post by:
Hi! Say a have a class called black_box which, among other things, contains an array of cells like this: class black_box { private: // ... public: black_box_details_struct details;
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
2
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded...
13
by: Moes | last post by:
struct Base{ }; struct Derived: public Base{ }; void f( Base *pBase ) { Derived *pDerived = (Derived*)pBase; }
4
by: Joe H | last post by:
Hi i converted a program to a lib. The parameters of main are maincommand(int argc, char **argv) I want to use main a s function that takes a string how can i convert a char * to a char ** or even...
204
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 =...
26
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...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
20
by: prashant.khade1623 | last post by:
I am not getting the exact idea. Can you please explain me with an example. Thanks
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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,...
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.