473,396 Members | 1,894 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.

malloc for struct

My question is quite basic. I have a function "ds707_async_orig_hdlr"
which gets called by another module.

cm_cdma_orig_params_s_type : is a struct defined in teh same file
as this function.

Now, when the entire code (with all other files) is run, I get a lint
error that points to the possibility of assignment to a NULL pointer in
the lines assigning values to the pointer to struct -
cdma_orig_params_ptr.
The code compiles and runs fine. Only lint is giving trouble. (Error
613 in Gimpel)

Is it because I haven't done a malloc here for cdma_orig_params_ptr ?
What is the problem ?

void ds707_async_orig_hdlr
(
const byte *dial_string,
int dial_string_len,
cm_cdma_orig_params_s_type *cdma_orig_params_ptr
)
{
cdma_orig_params_ptr->srv_opt = uint16 orig_so;
cdma_orig_params_ptr->activate_code = CM_OTASP_ACT_CODE_NONE;
cdma_orig_params_ptr->drs_bit = 1; /* maybe NA in async
*/
cdma_orig_params_ptr->sr_id_included = FALSE;
cdma_orig_params_ptr->qos_parms_incl = FALSE;
cdma_orig_params_ptr->last_act_data_net = SYS_SYS_MODE_NO_SRV;
*cdma_params_changed = TRUE;
*cm_srv_type = CM_SRV_TYPE_CDMA_SPECIFIC;

}

Mar 20 '06 #1
7 2175
Madhu schrieb:
My question is quite basic. I have a function "ds707_async_orig_hdlr"
which gets called by another module.

cm_cdma_orig_params_s_type : is a struct defined in teh same file
as this function.

Now, when the entire code (with all other files) is run, I get a lint
error that points to the possibility of assignment to a NULL pointer in
the lines assigning values to the pointer to struct -
cdma_orig_params_ptr.
The code compiles and runs fine. Only lint is giving trouble. (Error
613 in Gimpel)
Note: The code compiles and _seems_ to run fine.
This is an important difference -- the code may "run fine"
for your tests and may run into an access violation/segmentation
fault at other times.
Is it because I haven't done a malloc here for cdma_orig_params_ptr ?
No. It is because you do not check for null pointers.
What is the problem ?

void ds707_async_orig_hdlr
(
const byte *dial_string,
int dial_string_len,
cm_cdma_orig_params_s_type *cdma_orig_params_ptr
)
{
if (NULL != cdma_orig_params_ptr) { cdma_orig_params_ptr->srv_opt = uint16 orig_so;
cdma_orig_params_ptr->activate_code = CM_OTASP_ACT_CODE_NONE;
cdma_orig_params_ptr->drs_bit = 1; /* maybe NA in async
*/
cdma_orig_params_ptr->sr_id_included = FALSE;
cdma_orig_params_ptr->qos_parms_incl = FALSE;
cdma_orig_params_ptr->last_act_data_net = SYS_SYS_MODE_NO_SRV;
*cdma_params_changed = TRUE;
*cm_srv_type = CM_SRV_TYPE_CDMA_SPECIFIC;
} else {
/* Deal with the error here */
}
}


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 20 '06 #2
Madhu opined:
My question is quite basic. I have a function "ds707_async_orig_hdlr"
which gets called by another module.

cm_cdma_orig_params_s_type : is a struct defined in teh same
file
as this function.

Now, when the entire code (with all other files) is run, I get a lint
error that points to the possibility of assignment to a NULL pointer
in the lines assigning values to the pointer to struct -
cdma_orig_params_ptr.
The code compiles and runs fine. Only lint is giving trouble. (Error
613 in Gimpel)

Is it because I haven't done a malloc here for cdma_orig_params_ptr
?
What is the problem ?

void ds707_async_orig_hdlr
(
const byte *dial_string,
int dial_string_len,
cm_cdma_orig_params_s_type *cdma_orig_params_ptr
)
{
cdma_orig_params_ptr->srv_opt = uint16 orig_so;
cdma_orig_params_ptr->activate_code = CM_OTASP_ACT_CODE_NONE;
cdma_orig_params_ptr->drs_bit = 1; /* maybe NA in async
*/
cdma_orig_params_ptr->sr_id_included = FALSE;
cdma_orig_params_ptr->qos_parms_incl = FALSE;
cdma_orig_params_ptr->last_act_data_net = SYS_SYS_MODE_NO_SRV;
*cdma_params_changed = TRUE;
*cm_srv_type = CM_SRV_TYPE_CDMA_SPECIFIC;

}


Your code is quite a mouthful, but I guess Lint is, quite rightly IMHO,
complaining about you not checking whether `cdma_orig_params_ptr` is
non-NULL before trying to dereference it. Try:

if (cdma_orig_params_ptr)
{
/* your code here */
}
--
BR, Vladimir

No one may kill a man. Not for any purpose. It cannot be condoned.
-- Kirk, "Spock's Brain", stardate 5431.6

Mar 20 '06 #3
Thanks Vladimir and Michael. That was the fix.
may run into an access violation/segmentation
fault at other times

Totally. esp. in the app I'm programming for, it could be critical.

Its been a long while since I walked in pointer territory. If you can
clarify this, that would be great:
int *a; *a = 100; => error because pointer was not assigned an address
int *a; a = &b; *a = 100; => will work

In the case of ptr to struct why isn't there a need to malloc?

Mar 20 '06 #4
Oh. Come to think of it, I have a guess.
there is no need for address assignment because this code is in a
function definition? i.e. Has the requisite space and addresses been
allotted already since the function parameters specify their type and
when the function is called, the address space is made ready?

Mar 20 '06 #5
Madhu opined:
Thanks Vladimir and Michael. That was the fix.
may run into an access violation/segmentation
fault at other times Totally. esp. in the app I'm programming for, it could be critical.

Its been a long while since I walked in pointer territory. If you can
clarify this, that would be great:
int *a; *a = 100; => error because pointer was not assigned an
address
int *a; a = &b; *a = 100; => will work


Yes. The first line has `a` uninitialised, so dereferencing it is not
on. In the second line, `a` is initialised to the address of a
variable `b`, so 100 gets stored there. Whether storing an `int` in
`b` is OK, depends on what `b` is (which you didn't show).
In the case of ptr to struct why isn't there a need to malloc?


I'm not sure I understand this.

--
BR, Vladimir

A bug in the hand is better than one as yet undetected.

Mar 20 '06 #6
> In the case of ptr to struct why isn't there a need to malloc?
I'm not sure I understand this.


That was just a weird question that came up in my head once you guys
told me the fix. I was wondering why I did not need to explicitly allot
space for the struct "cdma_orig_params_ptr" in my code and how I could
directly derefernce its members as in
cdma_orig_params_ptr->sr_id_included = FALSE;

while in the int example (with a and b) a first had to be assigned an
address.

I then figured it must be because all the coding in this particular
case is in a function where the pointer to the structure is a parameter
passed to the function; that the compiler takes care of alloting
requisite space since teh function defn is known previously.
Is that right?

Does my question make sense now? o/w forget it. its no big deal.

Thanks,
Madhu

Mar 20 '06 #7
Madhu schrieb:
In the case of ptr to struct why isn't there a need to malloc?
I'm not sure I understand this.


You messed up the quoting levels and snipped the attributions;
this makes it hard to understand that the upper is by you and
the lower has been written afterwards.

With Vladimir S. Oka wrote
Madhu wrote
In the case of ptr to struct why isn't there a need to malloc?
I'm not sure I understand this.

this is much clearer.

That was just a weird question that came up in my head once you guys
told me the fix. I was wondering why I did not need to explicitly allot
space for the struct "cdma_orig_params_ptr" in my code and how I could
directly derefernce its members as in
cdma_orig_params_ptr->sr_id_included = FALSE;

while in the int example (with a and b) a first had to be assigned an
address.

I then figured it must be because all the coding in this particular
case is in a function where the pointer to the structure is a parameter
passed to the function; that the compiler takes care of alloting
requisite space since teh function defn is known previously.
Is that right?

Does my question make sense now? o/w forget it. its no big deal.


Yes. Someone has to provide the memory the pointer points to,
either the library or you. Make sure that you understand whether
this storage should be provided directly or indirectly by you.

If the pointer has a random value, i.e. points randomly somewhere
in memory, the whole thing still can seemingly work for a
time and then run amok, overwriting essential data elsewhere.

To conclude: A structure type is not handled differently from
a simple int in this respect.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 21 '06 #8

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

Similar topics

59
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>...
34
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...
18
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that...
5
by: Grant Austin | last post by:
What would be the correct syntax for setting up a dynamic array of structs? Suppose you have a struct declared: struct relation { FILE * binFile; unsigned int numAttrs; struct attrList *...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
7
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...
40
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
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
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. ...
3
by: palidhjede | last post by:
Hi I'm having difficulties trying to malloc a struct this is what it looks like and how I'm approaching the problem. struct Records { char * fname; char * lname; }; Records** g_Recs;...
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
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
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...
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,...

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.