开发者

Not getting strcpy errors on Mac while I do on Windows?

开发者 https://www.devze.com 2022-12-13 10:59 出处:网络
-- All of the revised code still refuses to run well, please hel开发者_运维技巧p -- When I compile my code in Windows, I get memory errors. However on the Mac, where I initially coded this code, it w

-- All of the revised code still refuses to run well, please hel开发者_运维技巧p --

When I compile my code in Windows, I get memory errors. However on the Mac, where I initially coded this code, it works fine. I need to get this working on Windows.

It's something to do with the way I handle my char strings using strcpy that the Mac seems to be fine with (I guess it's related to gcc vs. Microsoft's way of doing things).


Here's the code for the complainers: main.cpp

#include "Cust.h"
using namespace std;

int main (int argc, char * const argv[]) {
    Cust customers[500];
    char tmpString[70] = " ";
    char * pch = new char[255];
    string tmpAcctFN = " ";
    string tmpAcctLN = " ";
    ifstream input("P3_custData.txt");
    for (int idx = 0; idx < 130; idx++){
        input.getline(tmpString, 70, '\n');
        strcpy(pch,strtok(tmpString," "),255);
        customers[idx].setAcctNum(pch);
        cout << pch << endl;
        strcpy(pch, strtok(NULL," "));;
        customers[idx].setAcctFN(pch);
        cout << pch << endl;
        strcpy(pch, strtok(NULL," "));;
        customers[idx].setAcctLN(pch);
        cout << pch << endl;
        strcpy(pch, strtok(NULL," "));;
        customers[idx].setCurrBalance(atol(pch));
        cout << pch << endl;
        strcpy(pch, strtok(NULL," "));;
        customers[idx].setPIN(atoi(pch));
        cout << pch << endl;
    }
    input.close();
    return 0;
}

Cust.h

/*
 *  Cust.h
 *  Project 3
 *
 *  Created by Anthony Glyadchenko on 11/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <iostream>
#include <string>

using namespace std;

#ifndef CUST_H
#define CUST_H

class Cust{
public:
    char * getAcctNum();
    void setAcctNum(char num[]);
    double getCurrBalance();
    void setCurrBalance(double balance);
    void addToCurrBalance(double amount);
    void subFromCurrBalance(double amount);
    void setAcctFN(char firstName[]);
    void setAcctLN(char lastName[]);
    char * getAcctFN();
    char * getAcctLN();
    void setPIN(int pin);
    int getPIN();

private:
    char acctNum[255];
    char acctFN[255];
    char acctLN[255];
    double currBalance;
    int pin;
    char fileName[255];
};
#endif

Cust.cpp

/*
 *  Cust.cpp
 *  Project 3
 *
 *  Created by Anthony Glyadchenko on 11/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <fstream>
#include <string>
#include <sstream>
#include "Cust.h"

using namespace std;

char * Cust::getAcctNum(){
    return acctNum;
}

void Cust::setAcctNum(char num[]){
    strcpy(acctNum,num);
}

double Cust::getCurrBalance(){
    return currBalance;
}

void Cust::setCurrBalance(double balance){
    currBalance = balance;
}

void Cust::addToCurrBalance(double amount){
    currBalance += amount;
}

void Cust::subFromCurrBalance(double amount){
    currBalance -= amount;
}

void Cust::setAcctFN(char firstName[]){
    strcpy(acctFN,firstName);
}

void Cust::setAcctLN(char lastName[]){
    strcpy(acctLN,lastName);

}

char * Cust::getAcctFN(){
    return acctFN;
}

char * Cust::getAcctLN(){
    return acctLN;
}

void Cust::setPIN(int pin){
    Cust::pin = pin;
}

int Cust::getPIN(){
    return pin;
}

Here is my stack trace:

 Index  Function
--------------------------------------------------------------------------------
 1      msvcr90d.dll!68d7f693() 
 2      [Frames below may be incorrect and/or missing, no symbols loaded for msvcr90d.dll]
*3      P3.exe!main(int argc=0, char * const * argv=0x0036fcd0) 
 4      P3.exe!_FreeLibrary@4() 
 5      P3.exe!@ILT+170(__except_handler4)() 
 6      kernel32.dll!75eb3677() 
 7      ntdll.dll!77b29d72() 
 8      ntdll.dll!77b29d45() 


A few things to check (sorry not going to download the code):

  1. does g++ *.c have warnings? If so fix them.
  2. does g++ -W have warnings? If so fix them.
  3. does g++ -W -Wall have warnings? If so fix them.
  4. does g++ -W -Wall -Wextra have warnings? If so fix them.
  5. does g++ -W -Wall -Wextra -ansi have warnings? If so fix them.
  6. does g++ -W -Wall -Wextra -ansi -pedantic have warnings? If so fix them.

On microsoft try adding /W4 to the command line to turn the warning up, again fix any issues.

Odds are you are doing something "silly" and chances are that the compiler can help you catch what it is.

Edit:

From compiling your code with the flags above you will see:

Cust.h:33: error: ISO C++ forbids zero-size array ‘acctNum’ Cust.h:34: error: ISO C++ forbids zero-size array ‘acctFN’ Cust.h:35: error: ISO C++ forbids zero-size array ‘acctLN’ Cust.h:38: error: ISO C++ forbids zero-size array ‘fileName’ Cust.h:33: error: ISO C++ forbids zero-size array ‘acctNum’ Cust.h:34: error: ISO C++ forbids zero-size array ‘acctFN’ Cust.h:35: error: ISO C++ forbids zero-size array ‘acctLN’ Cust.h:38: error: ISO C++ forbids zero-size array ‘fileName’

So your code is not valid C++. You are copying a name into an array that is too small - the array has 0 elements. What you really need to do is give the arrays a size when you declare them or declare them as pointers and then use "new" to allocate the right amount of memroy.


Passing invalid buffers, buffers that are too small, etc., to strcpy results in undefined behavior - just about anything can happen. On the Mac, the problems happen but aren't apparent, while on Windows it results in a crash.


char acctNum[];
char acctFN[];
char acctLN[];

There's your problem right there. You never seem to allocate any space for these strings anywhere. The strcpy() in setAcctNum() is overflowing the bounds of that unsized array, and overwriting something else. It's pretty amazing that this compiles at all, actually.

You probably ought to be using std::string, instead - that'll make the memory management easier, at least.


Probably is your implementation of the strcpy function, which could have differences between how it is coded on the mac and how it's coded on Windows.

0

精彩评论

暂无评论...
验证码 换一张
取 消