Thread: malloc/realloc

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28

    malloc/realloc

    Everytime i think i get this i mess it up.
    Code:
    #include<stdio.h>
    #include<math.h>
    typedef int boolean;
    #define true 1
    #define false 0
    char Shift(char Achar,int Shifts)
    {
        boolean Shiftleft =
        Shifts <= 0;
        if (Shiftleft)
         Shifts=sqrt(pow(Shifts,2)); //i wonder if -1*val would be faster
        int Lowerbound = 33;
        int Remainder;
        int Upperbound = 126;
        Remainder =
    Shifts%(Upperbound-Lowerbound);
        if (Shiftleft)
        {
            if (Remainder>
            (int)Achar-Lowerbound)
            {
                return (char)(Upperbound+(((int)Achar-Lowerbound)-Remainder)+1);
            }
            else return (char)((int)Achar-Remainder);
    
        }
        else
        {
            if (Remainder>
            Upperbound-(int)Achar)
             {
                return (char)(Lowerbound+(Remainder-(Upperbound-(int)Achar))-1);
            }
            else return (char)((int)Achar+Remainder);
        }
    }
    int Encryption(char *AString,char *Buffer,int *NewLength)
    {
    int i = 0 ;
    *NewLength = strlen(AString)+1;
    Buffer = malloc(*NewLength);
    printf("Func Length : %i\n",*NewLength);
    for (i;i!=*NewLength;i++)
    {
     Buffer[i]=AString[i];
    }
    Buffer[i+1]='\0';
    printf("In Func: %s\n",Buffer);
    return 1;
    }
    
    #include<stdio.h>
    #include<stdlib.h>
    
    int Changec(char *Arr)
    {
        Arr[1]='G';
        return 1;
    }
    void Changei(int *Pointer)
    {
        *Pointer=3;
    }
    
    
    int main(void)
    {
        int strLength;
        char NewStr[0];
        Encryption("a",NewStr,&strLength);
        printf("Encrypted Str : %s\n",NewStr);
        printf("New Length : %i\n",strLength);
    
        return 0;
    }
    The encrypted string comes back as a smiley face. A very...very...friendly smiley.
    Any Help?

    If someone could explain how to use malloc and realloc properly that'd be nice XC

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Same as this (well same code)
    Strings :0
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using malloc & realloc
    By shruthi in forum C Programming
    Replies: 7
    Last Post: 09-12-2012, 03:04 AM
  2. Use of malloc and realloc
    By mrbains in forum C Programming
    Replies: 5
    Last Post: 11-11-2010, 02:53 AM
  3. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM

Tags for this Thread