Thread: Weird error, possible related to strings, pointers, stupidity, all of the above

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Question Weird error, possible related to strings, pointers, stupidity, all of the above

    Hey, I'm working on this program and I have two modules. One is a job class which contains a name. The other is a scheduling class.

    I'm having this problem where I put a job through my job_copy method and the name comes out scrambled-- but it doesn't happen within the job_copy method itself.

    Here is my output:
    Before job_copy-- name: name2
    In job_copy-- name: name2
    After job_copy-- name: �D(�����h(��Z���/(��

    Here is the relevant code in the Scheduler class:
    Code:
    Job* SC_removeJob(Scheduler* sc){
    
    
    	/* WHAT IS GOING ON?????? */
    	Job* j;
    
    	printf("Before job_copy-- name: %s\n", sc->job->name);
    	j = job_copy(sc->job);
    
    	printf("After job_copy-- name: %s\n", j->name);
    and here is the job_copy method:
    Code:
    Job* job_copy(Job *j2){
    
    	Job* j1 = job_alloc();
    	char new[5]; /*fix this?? */
    	j1->name = strcpy( new, j2->name);
    	j1->startTime = j2->startTime;
    	j1->CPUTime = j2->CPUTime;
    	j1->CPUTimeLeft = j2->CPUTimeLeft;
    	j1->IOOperations = j2->IOOperations;
    	j1->priority = j2->priority;
    
    	j1->timeSlice = j2->timeSlice;
    
    	j1->opsPerMs = j2->opsPerMs;
    
    	printf("In job_copy-- name: %s\n", j1->name);
    	return j1;
    }
    I'm very confused as to where the error is occurring.
    Any help = 5 Schrute bucks.

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most likely because you're copying the new char array (which is full of jibberish) into j1's name member and most likely causing a buffer overrun in the process, as well.
    What do you think it does? If you want duplicate the job, then copy j2's name into j1'2 name.

    This is assuming the name member is an array or a pointer to allocated memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, new isn't full of jibberish when it's been written by j2->name, but "new" is not going to be there any longer when you leave the function [well, the physical memory in the machine is still there, but it has been made available for the compiler to store other things in, so it will get overwritten with "gibberish" if you ever call some other function - such as printf()].

    You need to allocate memory for the name, and then use strcpy() to copy the original name into the new variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oops, mixed up source and destination for strcpy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    What pray is job_alloc() ?
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Got it.
    Thanks matsp.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Weird fatal error, header file related
    By alkis_y3k in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2002, 09:54 AM
  4. weird strings
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-06-2002, 01:42 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM