Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:28+00:00 2026-05-26T22:56:28+00:00

I got 2 functions: – stringCopy() which copy the parameter strToCopy into another string

  • 0

I got 2 functions:
– stringCopy() which copy the parameter strToCopy into another string dynamically allocated applying a sanitize (see 2nd function)
– _sanitized() which returns a dynamically allocated uppercased version of the parameter and removing non-letters char (such as numeric values & spaces).

Considering the following, I got an EXC_BAD_ACCESS because of k growing too much.

char* _sanitized(const char* str)
{
    char* uppercasedStr = malloc(sizeof str);

    int k = 0; // Index de parcours de la chaîne originale
    int i = k; // Index dans la nouvelle chaîne
    char evaluatedChar;
    while ( (evaluatedChar = str[k]) != '\0') 
    {
        if ('A' <= evaluatedChar && evaluatedChar <= 'Z') 
        {
            uppercasedStr[i] = evaluatedChar;
            i++;
        }
        else if ('a' <= evaluatedChar && evaluatedChar <= 'z') 
        {
            uppercasedStr[i] = evaluatedChar-32;
            i++;
        }

        k++;
    }
    i++;
    uppercasedStr[i] = '\0';

    return uppercasedStr;
}


char* stringCopy(char* strToCopy)
{
    char* uppercaseStr = _sanitized(strToCopy);

    char* copiedStr = malloc(sizeof uppercaseStr);

    int k = 0;
    while (uppercaseStr[k] != '\0') 
    {
        copiedStr[k] = uppercaseStr[k];
        k++;
    }
    k++;
    copiedStr[k] = '\0';

    free(uppercaseStr);

    return copiedStr;
}

I also noticed that when I copy char from uppercaseStr into copiedStr it modifies uppercaseStr in the same time which cause the overflow…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:56:29+00:00Added an answer on May 26, 2026 at 10:56 pm

    The error I see is here:

    char* uppercasedStr = malloc(sizeof str);
    

    You can’t use sizeof() to get the length of a string. You need to use strlen():

    char* uppercasedStr = malloc(strlen(str) + 1);  //  Need +1 for terminating null
    

    Here’s the other occurrence of the same mistake:

    char* copiedStr = malloc(sizeof uppercaseStr);
    

    should be:

    char* copiedStr = malloc(strlen(uppercaseStr) + 1);
    

    sizeof(str) only gives you the size of a char pointer, not the length of the entire c-string.

    Also note that I omitted the sizeof(char). This is because sizeof(char) is defined to be 1 in C. Therefore it is not needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a python script that calls a bunch of functions, each of which
I am learning cakePHP 1.26. I got a Controller which got two functions. I
i've got a lot of functions i create or copy from the web. i
I've got a lot of c++ code which contains a lot of functions and
I recently got into closures and anonymous functions, and I'm wondering if my code
if i've got string functions i use a lot, should i put them in
I’ve got three functions, foo , bar and baz , which, from my point
I have got a few jquery functions which I notice I'm repeating quite a
I've got a class with some functions which realistically are just 'helper' methods that
I've got this ToggleButton which is supposed to change the drawables (and functions) of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.