473,385 Members | 1,645 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.

Structures and Pointers

I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?

I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}
Nov 14 '05 #1
13 1036
Sally <wo**************@yahoo.com> scribbled the following:
I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?
It works by accident. The code invokes undefined behaviour, but
undefined behaviour does not automatically mean segmentation
faults. If you try it in different circumstances, it might very well
crash.
I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?
The same reasoning applies.
struct myStruct {
char x[10];
char y[1000];
int z;
}; int main(..) { struct myStruct *ptr; ptr->z = 12;
printf("%d",prt->z); }


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It's not survival of the fattest, it's survival of the fittest."
- Ludvig von Drake
Nov 14 '05 #2
"Sally" <wo**************@yahoo.com> wrote in message
news:30**************************@posting.google.c om...
I am confused as to why the code below does not produce a segmenation
fault.
It's not required to.
It actually works
By (unfortunate) accident. This time.
and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?
You do if you want your program's behavior to be
well-defined and predictable, with guarantees from
the language standard about its behavior.
I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}


The above is an example of 'undefined behavior'. According to
the language standard, the compiler is allowed to produce
code that does *anything*. This can vary from a violent
crash, to 'seeming to work' (and theoretically such things
as the ubiquitous 'nasal demons'). In your case, it 'seems
to work.' Next time it might do something completely different.
Murphy's law dictates that this will happen when the application
is first demonstrated to a potential client.

Moral: Don't Do That.

You also have another case of undefined behavior:
invocation of 'printf()' with no prototype in scope.

And you need a return statement for main().

-Mike
Nov 14 '05 #3
wo**************@yahoo.com (Sally) wrote in
news:30**************************@posting.google.c om:
I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?
You got (un)lucky. You do need to use malloc() in this case. What you did
is wrong.
I noticed also if I do use malloc, then free(ptr), I can still use
ptr->z and assign to z successfully?
Again, do not do this. If it works as hoped it's just luck.
struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
Do *not* do this.
printf("%d",prt->z);

}


--
- Mark ->
--
Nov 14 '05 #4
Mike Wahler wrote:
"Sally" <wo**************@yahoo.com> wrote in message
news:30**************************@posting.google.c om...
I am confused as to why the code below does not produce
a segmenation fault.
It's not required to.
It actually works


By (unfortunate) accident. This time.
and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?


You do if you want your program's behavior to be
well-defined and predictable, with guarantees from
the language standard about its behavior.
I noticed also if I do use malloc, then free(ptr), I can
still use ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}


<snip> You also have another case of undefined behavior:
invocation of 'printf()' with no prototype in scope.


You need to include a prototype for each function that you use?

Nov 14 '05 #5
Joe Laughlin <Jo***************@boeing.com> scribbled the following:
Mike Wahler wrote:
"Sally" <wo**************@yahoo.com> wrote in message
news:30**************************@posting.google.c om...
I am confused as to why the code below does not produce
a segmenation fault.
It's not required to.
It actually works


By (unfortunate) accident. This time.
and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?


You do if you want your program's behavior to be
well-defined and predictable, with guarantees from
the language standard about its behavior.
I noticed also if I do use malloc, then free(ptr), I can
still use ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}

<snip>
You also have another case of undefined behavior:
invocation of 'printf()' with no prototype in scope.

You need to include a prototype for each function that you use?


Yes, you do. In C99 at least. A full definition constitutes a
prototype, however a mere declaration does not always do so.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
Nov 14 '05 #6
Joe Laughlin wrote:
Mike Wahler wrote:
You also have another case of undefined behavior:
invocation of 'printf()' with no prototype in scope.


You need to include a prototype for each function that you use?


No, but you do need to for all variadic functions. It's a good idea
regardless, however.

--
++acr@,ka"
Nov 14 '05 #7
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hz********@news.boeing.com...
Mike Wahler wrote:
"Sally" <wo**************@yahoo.com> wrote in message
news:30**************************@posting.google.c om...
I am confused as to why the code below does not produce
a segmenation fault.
It's not required to.
It actually works


By (unfortunate) accident. This time.
and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?


You do if you want your program's behavior to be
well-defined and predictable, with guarantees from
the language standard about its behavior.
I noticed also if I do use malloc, then free(ptr), I can
still use ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}


<snip>
You also have another case of undefined behavior:
invocation of 'printf()' with no prototype in scope.


You need to include a prototype for each function that you use?


The way you worded this question leads me to believe you may not be aware of
the fact that "#include <stdio.h>" brings a prototype for printf() into
scope. You do not need to manually prototype every standard library function
you call, just include the appropriate header.

Nov 14 '05 #8
Sally wrote:

I am confused as to why the code below does not produce a segmenation
fault. It actually works, and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?

You've learned a valuable lesson, don't rely on your implementation to
catch your mistakes in a predictable way.

Brian Rodenborn
Nov 14 '05 #9
Kieran Simkin wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in
message news:Hz********@news.boeing.com...
Mike Wahler wrote:
"Sally" <wo**************@yahoo.com> wrote in message
news:30**************************@posting.google.c om...
I am confused as to why the code below does not produce
a segmenation fault.

It's not required to.

It actually works

By (unfortunate) accident. This time.

and I get 12 outputted on my screen. I
would have thought I needed to get memory using malloc?

You do if you want your program's behavior to be
well-defined and predictable, with guarantees from
the language standard about its behavior.

I noticed also if I do use malloc, then free(ptr), I
can still use ptr->z and assign to z successfully?

struct myStruct {
char x[10];
char y[1000];
int z;
};

int main(..) {

struct myStruct *ptr;

ptr->z = 12;
printf("%d",prt->z);

}

<snip>
You also have another case of undefined behavior:
invocation of 'printf()' with no prototype in scope.


You need to include a prototype for each function that
you use?


The way you worded this question leads me to believe you
may not be aware of the fact that "#include <stdio.h>"
brings a prototype for printf() into scope. You do not
need to manually prototype every standard library
function you call, just include the appropriate header.


Oh, yeah. Nevermind. :)
Nov 14 '05 #10
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Joe Laughlin <Jo***************@boeing.com> scribbled the following:
You need to include a prototype for each function that you use?


Yes, you do. In C99 at least. A full definition constitutes a
prototype, however a mere declaration does not always do so.


You are confused. C99 requires called functions to be declared.
It does not require prototypes for called functions. A full
definition may not include a prototype, because it can be a K&R
style definition[1], but it is always a declaration.

[1] C99 has still not outlawed these (which it calls "function
declarators with empty parentheses (not prototype-format
parameter type declarators)"), although they have been obsolete
since C89, and it seems likely that they will be in C0x too.
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Nov 14 '05 #11
On Thu, 10 Jun 2004 18:14:43 -0700, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Joe Laughlin <Jo***************@boeing.com> scribbled the following:
You need to include a prototype for each function that you use?
Yes, you do. In C99 at least. A full definition constitutes a
prototype, however a mere declaration does not always do so.


You are confused. C99 requires called functions to be declared.
It does not require prototypes for called functions. A full
definition may not include a prototype, because it can be a K&R
style definition[1], but it is always a declaration.


ISTR that C99 is a little stricter about when a function call has invoked
UB than C89 was. However I'm not entirely clear about where that is in the
standard.
[1] C99 has still not outlawed these


But its made it pretty darn clear you should not use them (6.11.6 and
6.11.7).
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #12
Mark McIntyre <ma**********@spamcop.net> writes:
On Thu, 10 Jun 2004 18:14:43 -0700, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> writes:
Joe Laughlin <Jo***************@boeing.com> scribbled the following:
You need to include a prototype for each function that you use?

Yes, you do. In C99 at least. A full definition constitutes a
prototype, however a mere declaration does not always do so.


You are confused. C99 requires called functions to be declared.
It does not require prototypes for called functions. A full
definition may not include a prototype, because it can be a K&R
style definition[1], but it is always a declaration.


ISTR that C99 is a little stricter about when a function call has invoked
UB than C89 was. However I'm not entirely clear about where that is in the
standard.


You'll have to be specific. I'm not aware of the changes you're
describing, nor do I even vaguely remember something like that.

Both C89 and C99 do require prototypes for variadic functions.
[1] C99 has still not outlawed these


But its made it pretty darn clear you should not use them (6.11.6 and
6.11.7).


That wording is unchanged from C89.
--
"Given that computing power increases exponentially with time,
algorithms with exponential or better O-notations
are actually linear with a large constant."
--Mike Lee
Nov 14 '05 #13
On Sat, 12 Jun 2004 11:03:47 -0700, in comp.lang.c , Ben Pfaff
<bl*@cs.stanford.edu> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:

ISTR that C99 is a little stricter about when a function call has invoked
UB than C89 was. However I'm not entirely clear about where that is in the
standard.


You'll have to be specific. I'm not aware of the changes you're
describing, nor do I even vaguely remember something like that.


*grin*. I can't be more specific, because I can't remember where it is. I
was kinda hoping someone else would remember. Or disremember and correct
me. Either is acceptable.

(of K&R style)
[1] C99 has still not outlawed these


But its made it pretty darn clear you should not use them (6.11.6 and
6.11.7).


That wording is unchanged from C89.


Sure, and I think that C89 made it perfectly clear that one should not
write that style. Repeating the lesson a decade later reinforces the point,
just don't do it.....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #14

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

Similar topics

14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
11
by: Fred Bennett | last post by:
I have a simulation project in which data can naturally be held in structures for processing. There are calls to multiple functions involved. Execution speed is an issue. Do I take a big hit for...
1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
8
by: michi | last post by:
Hello everybody, I have following problem: I have an array of pointers to structures: table* tab = new table; and structure table is like this: struct table{ CSLL::node* chain;
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Mr John FO Evans | last post by:
Am looking at adding structures to an embedded C emulator but am wondering what the standard is for copying structures(ie structa=structb) which contain pointers to memory and which are therefore...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will 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...
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...

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.