Sponsored Content
Top Forums Programming Segmentation fault when debugging in C Post 302706365 by bakunin on Wednesday 26th of September 2012 02:51:37 PM
Old 09-26-2012
To elaborate what DonCragun explained:

A pointer is a memory address where a certain object is stored. Suppose the line:

Code:
x = "abcd"

Most compilers (not only C-compilers) will do something similar to this: first, allocate 4 bytes to hold "abcd", then label this 4-byte-part of the memory with "x". Subsequent usages of "x" will be dereferenced to this location.

Now suppose you want to pass this variable to a function, but you want the function to modify it somehow. If you just pass the variable as an argument the compiler creates a copy of the data and the function will use this copy. Changes made to this copy are naturally lost once the function ends. Therefore C can pass a pointer instead of the variable itself. By passing the pointer the function gains access not to the copy, but the original and can modify it lastingly.

It is also possible to find out the memory address of an object (the "&" operator DonCragun told you about) and it is possible to get the variable from a pointer address: the "*" operator.

Now, all pointers are of the same format - addresses in memory - and this begs the question why C pointers have types. Regardless of the types all pointers would look the same, no? Yes and no: yes, they look the same. No, they need to be typed because pointers in C are "intelligent": as they know which data they point to, it is possible to use pointer arithmetics.

Suppose you have an array of 32-bit-values. When the program runs this is a series of 4-byte long memory-locations. Now suppose you have a pointer to the first element. If you create a pointer in C and tell it the correct data type the pointer will "know" that the data "unit" it points to is 4 bytes long. an operation like ++ on the pointer will then not increment the address it points to by one (which would be the address of the second byte of the same unit - rather senseless) but by 4, so that the pointer now points to the next units first byte. This way it is easy to go from one to the next element of the array. This would not be possible if your pointer wouldn't know which size the data it points to is.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Segmentation Fault

hello all, I tried a program on an array to intialise array elements from the standard input device.it is an integer array of 5 elements.but after entering the 4th element it throws a message called "Segmentation Fault" and returns to the command prompt without asking for the 5th element. ... (3 Replies)
Discussion started by: compbug
3 Replies

2. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

3. Programming

segmentation fault

ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do??? thank... (8 Replies)
Discussion started by: rockgal
8 Replies

4. Programming

Why not a segmentation fault??

Hi, Why I don't receive a segmentation fault in the following sample. int main(void) { char buff; sprintf(buff,"Hello world"); printf("%s\n",buff); } If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev... (22 Replies)
Discussion started by: lagigliaivan
22 Replies

5. Programming

segmentation fault

What is segmentation fault(core dumped) (1 Reply)
Discussion started by: gokult
1 Replies

6. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

7. Homework & Coursework Questions

Segmentation Fault

this is a network programming code to run a rock paper scissors in a client and server. I completed it and it was working without any error. After I added the findWinner function to the server code it starts giving me segmentation fault. -the segmentation fault is fixed Current problem -Also... (3 Replies)
Discussion started by: femchi
3 Replies

8. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

9. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
2 Replies

10. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
QGuardedPtr(3qt)														  QGuardedPtr(3qt)

NAME
QGuardedPtr - Template class that provides guarded pointers to QObjects SYNOPSIS
#include <qguardedptr.h> Public Members QGuardedPtr () QGuardedPtr ( T * p ) QGuardedPtr ( const QGuardedPtr<T> & p ) ~QGuardedPtr () QGuardedPtr<T> & operator= ( const QGuardedPtr<T> & p ) QGuardedPtr<T> & operator= ( T * p ) bool operator== ( const QGuardedPtr<T> & p ) const bool operator!= ( const QGuardedPtr<T> & p ) const bool isNull () const T * operator-> () const T & operator* () const operator T * () const DESCRIPTION
The QGuardedPtr class is a template class that provides guarded pointers to QObjects. A guarded pointer, QGuardedPtr<X>, behaves like a normal C++ pointer X*, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which become "dangling pointers" in such cases). X must be a subclass of QObject. Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else and therefore might be destroyed while you still hold a reference to it. You can safely test the pointer for validity. Example: QGuardedPtr<QLabel> label = new QLabel( 0, "label" ); label->setText( "I like guarded pointers" ); delete (QLabel*) label; // simulate somebody destroying the label if ( label) label->show(); else qDebug("The label has been destroyed"); The program will output The label has been destroyed rather than dereferencing an invalid address in label->show(). The functions and operators available with a QGuardedPtr are the same as those available with a normal unguarded pointer, except the pointer arithmetic operators (++, --, -, and +), which are normally used only with arrays of objects. Use them like normal pointers and you will not need to read this class documentation. For creating guarded pointers, you can construct or assign to them from an X* or from another guarded pointer of the same type. You can compare them with each other using operator==() and operator!=(), or test for 0 with isNull(). And you can dereference them using either the *x or the x->member notation. A guarded pointer will automatically cast to an X*, so you can freely mix guarded and unguarded pointers. This means that if you have a QGuardedPtr<QWidget>, you can pass it to a function that requires a QWidget*. For this reason, it is of little value to declare functions to take a QGuardedPtr as a parameter; just use normal pointers. Use a QGuardedPtr when you are storing a pointer over time. Note again that class X must inherit QObject, or a compilation or link error will result. See also Object Model. MEMBER FUNCTION DOCUMENTATION
QGuardedPtr::QGuardedPtr () Constructs a 0 guarded pointer. See also isNull(). QGuardedPtr::QGuardedPtr ( T * p ) Constructs a guarded pointer that points to same object as p points to. QGuardedPtr::QGuardedPtr ( const QGuardedPtr<T> & p ) Copy one guarded pointer from another. The constructed guarded pointer points to the same object that p points to (which may be 0). QGuardedPtr::~QGuardedPtr () Destroys the guarded pointer. Just like a normal pointer, destroying a guarded pointer does not destroy the object being pointed to. bool QGuardedPtr::isNull () const Returns TRUE if the referenced object has been destroyed or if there is no referenced object; otherwise returns FALSE. QGuardedPtr::operator T * () const Cast operator; implements pointer semantics. Because of this function you can pass a QGuardedPtr<X> to a function where an X* is required. bool QGuardedPtr::operator!= ( const QGuardedPtr<T> & p ) const Inequality operator; implements pointer semantics, the negation of operator==(). Returns TRUE if p and this guarded pointer are not pointing to the same object; otherwise returns FALSE. T &; QGuardedPtr::operator* () const Dereference operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer. T * QGuardedPtr::operator-> () const Overloaded arrow operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer. QGuardedPtr<;T> & QGuardedPtr::operator= ( const QGuardedPtr<T> & p ) Assignment operator. This guarded pointer then points to the same object as p points to. QGuardedPtr<;T> & QGuardedPtr::operator= ( T * p ) This is an overloaded member function, provided for convenience. It behaves essentially like the above function. Assignment operator. This guarded pointer then points to the same object as p points to. bool QGuardedPtr::operator== ( const QGuardedPtr<T> & p ) const Equality operator; implements traditional pointer semantics. Returns TRUE if both p and this guarded pointer are 0, or if both p and this pointer point to the same object; otherwise returns FALSE. See also operator!=(). SEE ALSO
http://doc.trolltech.com/qguardedptr.html http://www.trolltech.com/faq/tech.html COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. AUTHOR
Generated automatically from the source code. BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qguardedptr.3qt) and the Qt version (3.3.8). Trolltech AS 2 February 2007 QGuardedPtr(3qt)
All times are GMT -4. The time now is 09:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy