Sponsored Content
Full Discussion: Segment Violation
Top Forums Programming Segment Violation Post 302425556 by Corona688 on Friday 28th of May 2010 12:03:49 PM
Old 05-28-2010
The value of pList in main is undefined and probably pointing off into hyperspace somewhere. The instant you try to use it you're trying to access memory you never allocated.

Your logic is also a touch incorrect. An empty list has no memory allocated to it at all in this scheme, hence can't be modified without modifying the base pointer itself, which your function cannot do, being passed only a copy of it.

Pass a pointer to the pointer so it can modify it, check for the special case of creating the first node, and make sure you set the value of the empty list to NULL.

Code:
void insertAtEnd(NodeType **ppList) {
    NodeType *plist=(*ppList);
    char element;

    printf("Introduce a element: \n");
    setbuf(stdin, NULL);
    scanf("%c", &element);

    //Makes a new node.
    NodeType *newNode = (NodeType *) malloc(sizeof(NodeType));
    newNode->caracter = element;
    newNode->nextNode = NULL;

    // Empty list.
    if(plist == NULL)
    {
          // Set the base of the list to the new node
          (*ppList)=plist;
          return;
    }


    //Find the end of the list;
    while (pList->nextNode != NULL) {
        pList = pList->nextNode;
    }

    //Links the new node.
    pList->nextNode = newNode;
}

int main(int argc, char argv[]) {

    NodeTipe *pList=NULL;
    insertAtEnd(&pList); 
}

 

9 More Discussions You Might Find Interesting

1. Programming

Segment Fault

When run it, segment fault. What is wrong? #include <stdio.h> #include <stdlib.h> const int max =20; //**************************************************** // Input Matrix //**************************************************** void inMatrixAA(int *AA, int row, int col)... (9 Replies)
Discussion started by: zhshqzyc
9 Replies

2. Shell Programming and Scripting

extract segment

Hey all, could someone please direct me on how to extract a segment from a file between two tags? Thanks! (1 Reply)
Discussion started by: mpang_
1 Replies

3. Shell Programming and Scripting

Cutting segment of a string

Hi, I am using bash. My question concerns cutting out segments of a string. Given the following filename: S2002254132542.L1A_MLAC.x.hdf I have been able to successfully separate the string at the periods (.): $ L1A_FILE=S2002254132542.L1A_MLAC.x.hdf $ BASE=$(echo $L1A_FILE | awk -F.... (5 Replies)
Discussion started by: msb65
5 Replies

4. UNIX for Dummies Questions & Answers

code segment

how do i close a do code segment? od? (1 Reply)
Discussion started by: trob
1 Replies

5. Shell Programming and Scripting

How to change a segment in a particular position

I need help in removing a leading zero in a particular position. For eg.: XYZ*04567472*0099*020091231*0123*0.12 In the above line, I want to replace "*0123" with "123" and "0.12" with ".12". I want to remove the leading zero only in position number 4 and 5 (the bolded segments) I was able... (10 Replies)
Discussion started by: ananthmm
10 Replies

6. Programming

How can I know where the segment of memory is all Zero?

I mean, I malloc a segment of memory, maybe 1k maybe 20bytes.. assume the pointer is pMem How can I know the content pMem refered is all Zero or \0 . I know memcmp but the second parameter should another memory address... thanx (4 Replies)
Discussion started by: macroideal
4 Replies

7. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

8. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

9. Shell Programming and Scripting

[Solved] Trimming out segment

I have a file with a combination of binary characters and words, and need to trim out the segment i.e. SleeperThreadAborting {{username::RAB2002}} {{scriptname::scs_get_pending_by_loc}} and put the lists into a text file. Luckily, the segment I am looking for i.e. has readable words ... (8 Replies)
Discussion started by: Daniel Gate
8 Replies
ADDPHSEG(3)						      MBK PHYSICAL FUNCTIONS						       ADDPHSEG(3)

NAME
addphseg - create a physical segment ORIGIN
This software belongs to the ALLIANCE CAD SYSTEM developed by the ASIM team at LIP6 laboratory of Universite Pierre et Marie CURIE, in Paris, France. Web : http://asim.lip6.fr/recherche/alliance/ E-mail : alliance-users@asim.lip6.fr SYNOPSYS
#include "mph.h" phseg_list *addphseg(ptfig, layer, width, x1, y1, x2, y2, nodename) phfig_list *ptfig; char layer; long width; long x1, y1, x2, y2; char *nodename; PARAMETERS
ptfig Pointer to the figure in which the segment should be added layer Segment symbolic layer width Segment symbolic width x1, y1, x2, y2 Segment endpoints coordinates nodename Name of the segment DESCRIPTION
addphseg creates a new segment at the given coordinates, possibly called segname, and adds it to the list of segments pointed to by ptfig->PHSEG. The new segment is added in front of the list, and becomes itself the list head. The segment name is an optional information, and does not allow the segment identification. When not needed, this parameter should be set to NULL. The parameters nodename, layer and width fill respectivly the NAME, LAYER and WIDTH fields of the phseg structure. x1, y1, x2, y2 are sorted to warranty that the fields X1 and Y1 contain the minimum of, respectivly, x1, x2 and y1, y2, and the X2, Y2 fields the maximum. The TYPE field is computed by the addphseg function. It will be either VER if x1 equals x2, or HOR if y1 equals y2. The width is a layer extension that takes place in perpendicular to the segment TYPE. For a list of valid layers, and details on the structure, see phseg(3). RETURN VALUE
addphseg returns a pointer to the newly created segment. ERRORS
"*** mbk error *** illegal addphseg wrong layer code layer in x1, y1, x2, y2" The layer parameter is out of range, and does not represent a legal symbolic layer. See phseg(3) for a complete list of layers. "*** mbk error *** illegal addphseg : x1, y1, x2, y2" The segment coordinates are such that the segment is neither vertical nor horizontal. So it's not a legal symbolic segment. EXAMPLE
#include "mph.h" phseg_list *du_seg(pfd, pfs) phfig_list *pfd, *pfs; { phseg_list *ps; /* names don't matter */ for (ps = pfs->PHSEG; ps != NULL; ps = ps->NEXT) addphseg(pfd, ps->LAYER, ps->WIDTH, ps->X1, ps->Y1, ps->X2, ps->Y2, NULL); } SEE ALSO
mbk(1), phfig(3), phseg(3), delphseg(3). BUG REPORT
This tool is under development at the ASIM department of the LIP6 laboratory. We need your feedback to improve documentation and tools. ASIM
/LIP6 October 1, 1997 ADDPHSEG(3)
All times are GMT -4. The time now is 09:08 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy