473,513 Members | 13,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc for struct

hi
I am a newbie to c coding and going thru K&R ...
i have a structure to represent an image as

struct image{
char* imagename;
int imagewidth;
int imageheight;
long* pixels;
};

typedef struct image img;
typedef img* imgptr;

here long * pixels shd point to an array of long values representing
pixels of the image.Arraysize (ie number of pixels )will be known only
at runtime.i want to read values into this struct and return a pointer
so i can use this later in another fn

so i wrote
imgptr readimage(char* filename){
/* read image with some library and set wifth,height and get
pixel data array (obtained like long* pixeldata)*/
....
/* allocate memory for structure */
imgptr thisimageptr=malloc(sizeof(thisimageptr));
if(thisimageptr==NULL) exit(EXIT_FAILURE);
int numberofpixels=width * height;
/*allocate memory for the long * pixels */
thisimageptr->pixels=malloc(sizeof(long*) * numberofpixels);
if(thisimageptr->pixels==NULL) exit(EXIT_FAILURE);
/* set fields of struct */
thisimageptr->imagename=filename;
thisimageptr->imagewidth=width;
thisimageptr->imageheight=height;

for(i=0;i<numberofpixels;i++){
thisimageptr->pixels[i]=pixeldata[i];;
}

return thisimageptr;
}
if another fn gets this pointer it should be able to retrieve the
fields and should be able to iterate over the pixelvalues

i am a little doubtful about the malloc part..is the above mallocs
correct ? or is there some hidden risk?
Feb 8 '08 #1
7 2788
va********@gmail.com wrote:
>
hi
I am a newbie to c coding and going thru K&R ...
i have a structure to represent an image as

struct image{
char* imagename;
int imagewidth;
int imageheight;
long* pixels;
};

typedef struct image img;
typedef img* imgptr;
imgptr thisimageptr=malloc(sizeof(thisimageptr));
i am a little doubtful about the malloc part..is the above mallocs
correct ?
Unlikely.
or is there some hidden risk?
This is more likely to be correct:

imgptr thisimageptr = malloc(sizeof *thisimageptr);

This is more likely to be correct and better style:

img *thisimageptr = malloc(sizeof *thisimageptr);

--
pete
Feb 8 '08 #2
>
This is more likely to be correct:

imgptr thisimageptr = malloc(sizeof *thisimageptr);

This is more likely to be correct and better style:

img *thisimageptr = malloc(sizeof *thisimageptr);

pete
thanx for the reply
what i was worried about was
when i allocate for that pixels
do i have to allocate memory for a size of (a long pointer)*(number of
items in the array) ?
only then can it be big enough to hold all those long pointers

eric
Feb 8 '08 #3
va********@gmail.com wrote:
thisimageptr->pixels=malloc(sizeof(long*) * numberofpixels);
i am a little doubtful about the malloc part..is the above mallocs
correct ? or is there some hidden risk?
That way is OK, but this way is better style:

thisimageptr->pixels
= malloc(sizeof *thisimageptr->pixels * numberofpixels);

http://c-faq.com/malloc/noscale.html

--
pete
Feb 8 '08 #4
va********@gmail.com wrote:
>

This is more likely to be correct:

imgptr thisimageptr = malloc(sizeof *thisimageptr);

This is more likely to be correct and better style:

img *thisimageptr = malloc(sizeof *thisimageptr);

pete
thanx for the reply
what i was worried about was
when i allocate for that pixels
do i have to allocate memory for a size of (a long pointer)*(number of
items in the array) ?
only then can it be big enough to hold all those long pointers
That's right.
An array of pointers to arrays is commonly used that way.

--
pete
Feb 8 '08 #5
do i have to allocate memory for a size of (a long pointer)*(number of
items in the array) ?
only then can it be big enough to hold all those long pointers

That's right.
An array of pointers to arrays is commonly used that way.
pardon me if i am not following this correctly..
the long pointer
thisimageptr->pixels points to a chunk that shd be big enough to
hold (numberofpixels) values which are 'long's not 'long*'s
so the allocation should be to a size equal to numberofpixels*
sizeof(long)

i believe that is what happens in
thisimageptr->pixels
= malloc(sizeof *thisimageptr->pixels * numberofpixels);

on the other hand ,in
thisimageptr->pixels
= malloc(sizeof(long*) * numberofpixels);
allocation happens to multiple of sizeof(long*).
that will give correct result only if
sizeof(long*) ==sizeof(long) in a machine

am i correct..or is my understanding wrong?
jim

Feb 8 '08 #6
jimgardener wrote:
>
do i have to allocate memory for a size of
(a long pointer)*(number of
items in the array) ?
only then can it be big enough to hold all those long pointers
That's right.
An array of pointers to arrays is commonly used that way.

pardon me if i am not following this correctly..
the long pointer
thisimageptr->pixels points to a chunk that shd be big enough to
hold (numberofpixels) values which are 'long's not 'long*'s
so the allocation should be to a size equal to numberofpixels*
sizeof(long)

i believe that is what happens in
thisimageptr->pixels
= malloc(sizeof *thisimageptr->pixels * numberofpixels);

on the other hand ,in
thisimageptr->pixels
= malloc(sizeof(long*) * numberofpixels);
allocation happens to multiple of sizeof(long*).
that will give correct result only if
sizeof(long*) ==sizeof(long) in a machine

am i correct..or is my understanding wrong?
jim
You are correct in what you have said about allocation
for each pointer in the array of pointers.
But I believe that the topic being addressed by my reply to OP
was about the allocation of the array of pointers.
After which each pointer in the array would
then be pointed to allocated memory.

--
pete
Feb 8 '08 #7

<va********@gmail.comwrote in message news
>
>>
This is more likely to be correct:

imgptr thisimageptr = malloc(sizeof *thisimageptr);

This is more likely to be correct and better style:

img *thisimageptr = malloc(sizeof *thisimageptr);


pete
thanx for the reply
what i was worried about was
when i allocate for that pixels
do i have to allocate memory for a size of (a long pointer)*(number of
items in the array) ?
only then can it be big enough to hold all those long pointers
struct image *answer;
answer = malloc(sizeof(struct image));
if(!answer)
return 0;
/* get width and height somehow */
answer->pixels = malloc(sizeof(long) * width * height);
if(!answer->pixels)
{
free(answer);
return 0;
}
/* fill answer->pixels with raster data, somehow */
return answer;

Some advanced programmers will say malloc(sizeof *answer->pixels * width *
height) instead, so that if you change the pixel representation the code
will update. However in my opinion this is hard to read, and has proved a
source of confusion to you.
sizeof() may take either a type name or a variable.

In this case, the allocation is likely to be of a meduim to large size,
depending on the image and your system. So it is forseeable that it might
fail, and you should be prepared to handler failure conditions in the
caller.
It is most unlikely that the trivial allocation for the structure itself
will fail, but since we are already reporting out of memory conditions, we
might as well report it as well.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 8 '08 #8

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

Similar topics

59
5116
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h>...
13
83877
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
34
6395
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
33
2662
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
7
2189
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
58
3866
by: lasek | last post by:
Hi all..i'm here another time..for a simple question: #include <stdlib.h> #include <stdio.h> int main(void) { /* ** I know that someone else use different malloc ** instruction
68
15624
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
2525
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
23
2690
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
25
3339
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
7254
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
7153
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
7373
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
7432
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...
1
7094
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
7519
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...
1
5079
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.