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

Question regarding malloc

121 100+
emmm, this is taken from the exam I just finished this morning:

Which one is correct:

Expand|Select|Wrap|Line Numbers
  1. A) double *ptr = (double *) malloc(100 * sizeof(double));
  2. B) double *ptr = (double *) malloc(100 * sizeof(double *));
  3. C) double ptr = (double) malloc(100 * size0f(double));
  4. D) double *ptr = (double) malloc(100*sizeof(double));
My choice is A.

But if you take a look at potion B, it says:
"allocate 100 times of the memory size for a pointer to double and then cast the void pointer to be of type double, finally assign it to a pointer to double called ptr."

I guess B is also correct on syntax, though not as that meaningful as A.

What's your opinion?

To prove this, here is my code fragment:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.    double *ptr = (double *) malloc(100*sizeof(double *));
  7.  
  8.    printf("Hello there!\n");
  9.    return 0;
  10. }
It was compiled successfully and generated the correct output...


BTW I found many funny questions in exam, such as this one:

Which one is NOT a reserved word in c?
A) int
B) for
C) sizeof
D) exit
Nov 20 '07 #1
6 1412
Shashi Sadasivan
1,435 Expert 1GB
Both A and B seem to be correct.
But A seems to be an answer which your examiners would be looking for.
Nov 20 '07 #2
gsi
51
Hi,

double *ptr = (double *) malloc(100 * sizeof(double *));

may be syntactically correct, but semantically wrong. It may be right , but it is thoroughly implementation defined. Because in this context if this is going to be right on a given implementation , then the size of a double is equal to the size of a pointer in that implementation.

int *ptr = (int*) malloc(100 * sizeof(int*)); --> this may hold good in many 32 bit unix implementation's but again undefined.



Thanks,
Gsi.
Nov 20 '07 #3
mattmao
121 100+
double *ptr = (double *) malloc(100 * sizeof(double *));

may be syntactically correct, but semantically wrong
Sorry, I don't quite follow you.

I already mentioned the meaning of option B, it makes fully sense to me. And if you want to allocate some memory space for the size of 100 pointer to double (s), then you should do it that way.

The original question is taken from a exam paper, so we shouldn't consider too much about the actual code implementation, anything that is legal should be recognized as a correct answer.

Since the cast is not critical to option B, we could replace it with:

void *ptr = malloc(100*sizeof(double *));

void ptr is only used for keeping the memory address of this allocated memory, what's going on is not for our consideration...
Nov 20 '07 #4
oler1s
671 Expert 512MB
None of those possible answers are correct. malloc gets you a pointer to a memory block. So if you allocate memory for ints, you get a pointer to an int array. If you allocate for doubles, you assign to a double pointer. If you allocate for double pointers, you assign to a pointer to a double pointer.

Furthermore, you should always be using the recommended malloc idiom. That is:

p = malloc(n* sizeof *p)

Therefore, you should have double** ptr = malloc(100 * sizeof *ptr);

--

Potentially you could assign to a void pointer. Effectively, it's a dumb idea. (No offense). There's several reasons, and if you can't figure them out, I'll walk through them.
Nov 20 '07 #5
mattmao
121 100+

Yes it compiles but it is also wrong.

sizeof(double *) will give you the size of memory for a pointer to a
double. This is usually 4 bytes on most machines. sizeof(double) will
give you the size of a double, which is usually 8 bytes.

Therefore A will set aside 800 bytes while B will set aside 400 bytes.

Under such conditions it's very easy to get segmentation faults when you
try and access some of the array, as not enough memory has been
allocated for it.
This is taken from my lecturer's reply...And I think that could end this discussion, it seems that he doesn't care too much about the meaning of allocating 100 double pointers...

Thank you for all of the helps. I found learning to program in c is pretty straight forward and interesting. Next semester I would continue to study in C++ :)
Nov 21 '07 #6
oler1s
671 Expert 512MB
Your lecturer's reply doesn't end the discussion. He doesn't actually answer the question. Furthermore, his implications are disturbing, because they smack of just bad programming.

Under such conditions it's very easy to get segmentation faults when you
try and access some of the array, as not enough memory has been
allocated for it.
And this means what? Obviously you'll get a segmentation fault if you allocate too little memory, and then correctly access parts of the array which you assume you have memory access to.

I have to question if that was really a lecturer's reply, because two possibilities arise: he needs to pick up K8R's book and learn C, or he is incapable of giving a technically coherent answer.
Nov 21 '07 #7

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

Similar topics

0
by: porschberg | last post by:
Hi, I have question regarding the pool library. In my application I read a lot of data records from a database into a small data class and therefor I thought object_pool could help me to reduce...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
19
by: amanayin | last post by:
Why does this program run in the ddd debugger but when i try to run the program in a konsole i get a segmentation fault. I just can't work it out the OS i use is suse 8.2 pro program below: /*...
36
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program...
19
by: lawtrevor | last post by:
I have a question regarding the use of free() based on some code I need to decipher: { struct A {<A fields>}; struct B {<A fields+ <Bfields>}; typedef struct A Aobj; typedef struct B Bobj;
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
10
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned...
7
by: somenath | last post by:
Hi All , As a process of my C language learning I was trying to learn how malloc can be implemented from K&R2 .But I am not able to understand few points . It would be very much helpful if...
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
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...

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.