problem with sizeof

This is a discussion on problem with sizeof within the C++ Programming forums, part of the General Programming Boards category; I am working on making a simple program which allows rot13 encryption and decryption of files. I am starting small ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    problem with sizeof

    I am working on making a simple program which allows rot13 encryption and decryption of files. I am starting small and I am working on making code to perform rot13 to and from on a char*. For some reason I can't have anything over size 4 put in the returned char. I think I may be misusing sizeof. Here is my code with the output it produces.

    Code:
    using std::string;
    
    char* encrypt_string(const char *input){
        size_t size = (sizeof(input) / sizeof(*input));
        char *ret = new char[size + 1];
        for(size_t i = 0; i < size + 1; ++i){
            *(ret + i) = (*(input + i) + 13);
        }
        return ret;
    }
    
    char* decrypt_string(const char *out){
        size_t size = (sizeof(out) / sizeof(*out));
        char *ret = new char[size + 1];
        for(size_t i = 0; i < size + 1; ++i){
            *(ret + i) = (*(out + i) - 13);
        }
        return ret;
    }
    
    int main(){
        char *orig = "goodbye";
        size_t t = (sizeof(orig) / sizeof(*orig));
        std::cout << t << std::endl;
        char *s = encrypt_string(orig);
        std::cout << s << std::endl;
        size_t size = sizeof(s) / sizeof(*s);
        std::cout << size << std::endl;
        char *s1 = decrypt_string(s);
        std::cout << s1 << std::endl;
    }
    output is

    Code:
    4
    t||qo
    4
    goodb
    Perhaps it's something small that I am missing in my tired state, but help would be greatly appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Use strlen() on char * pointers, pointing at \0 terminated arrays of characters.

    The sizeof()/sizeof() approach only works on TRUE arrays which are in scope at the time you do it.
    If all you have is a pointer, it will not work as you hope.

    Though this being C++, why aren't you using std::string (and all it's methods) for everything?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    I suppose I could probably do that, was just reading about pointers so I figured this would be something to practice it a bit. Thanks for the advice, I got it working. As I said, my main goal is really to learn to use pointers. Most of my experience is with java atm and strings would be too easy. Any advice on good ways to practice using pointers?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bob887 View Post
    Any advice on good ways to practice using pointers?
    Write a simple linked list class (with add, insert, delete and find methods). Of course, there are already things like that in the C++ STL, but it is a great way to get comfy with pointers.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    I'm not sure C++ is a particularly good language to be using to learn about pointers.

    It's just that you can get a lot done in C++ without ever going near a pointer at all. I would suggest that you keep "this is how to use pointers" and "this is how to write C++" as separate ideas in your mind.

    MK27's suggestion is good practice non the less.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Alright, thank you all, I got that part of my code working(I also created a string version of the encrypt and encrypt functions).

    Hopefully I can get the i/o stuff done without having much trouble.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Your code is leaking memory, due to no delete statements. That is why raw pointers are dangerous. Consider reading about smart pointers, too. You can practice them a little, too, when writing your linked list.
    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.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Ah, I was wondering if that happened. When can I delete the pointer? Is it possible to delete space allocated in a method from main? I ended up changing the way I did it and used strings instead. I am wondering, this is the correct way to read a file, right?
    Code:
    std::string s;
    std::ifstream in("test.txt");
    while(getline(in, s)){
      std::cout << s << std::endl;
    }

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    You can call delete anywhere so long as you have the original pointer.
    Just remember that after delete, you cannot use that memory anymore.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    edit: solved it, thanks very much for all the help
    Last edited by bob887; 05-08-2011 at 08:33 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Looking at your old code, you're not still trying to delete those C-style strings you put on the stack, are you?
    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.

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Quote Originally Posted by Elysia View Post
    Looking at your old code, you're not still trying to delete those C-style strings you put on the stack, are you?
    In my final code I switched to using strings instead of c-style strings.

    Code:
    string encrypt_string(string& input){
        for(string::iterator i = input.begin(); i != input.end(); ++i){
            *i = (*i + 13);
        }
        return input;
    }
    I don't know if it's perfect but I have it so I can successfully encrypt a file by passing
    Code:
    -e filename
    to the cmd line, and -d instead for decrypting. I am considering it may be smarter to allow the actual encryption/decryption of files function to take an encryption function as an argument to add easy ability to change what is used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with sizeof operator
    By slash_axl in forum C Programming
    Replies: 9
    Last Post: 09-28-2010, 07:00 AM
  2. sizeof an array problem
    By jeanluca in forum C Programming
    Replies: 8
    Last Post: 12-24-2009, 01:11 AM
  3. sizeof(): why the problem?
    By pantera in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2009, 12:36 AM
  4. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  5. sizeof(struct.member) problem
    By huh in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2003, 04:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21