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 6921461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:16:48+00:00 2026-05-27T10:16:48+00:00

Having a struct defined in a such way, I need to allocate memory typedef

  • 0

Having a struct defined in a such way, I need to allocate memory

typedef struct string_collection {
    char **c;
    size_t current, allocated;
} TSC, *ASC;

So I came with this code, is it right or I missed something? First allocating struct descriptor and then enough space for d pointers to string

ASC AlocSC(size_t d)
{
    ASC sc;

    sc = (TSC*) malloc(sizeof(TSC));
    if (!sc) return NULL;

    sc->c = calloc(d, sizeof(char *));

    if (!sc->c) {
        free(sc);
        return NULL;
    }

    sc->current = 0;
    sc->allocated = d;

    return sc;
}
  • 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-27T10:16:48+00:00Added an answer on May 27, 2026 at 10:16 am

    The code as edited is essentially correct, though I have several stylistic differences with you (such as not doing a typedef to hide the “pointerness” of an object, not using the size of the allocated object in the malloc/calloc call, and a few other things).

    Your code, “cleaned up” a bit:

    TSC *AlocSC(size_t d)
    {
        TSC *sc = malloc(sizeof *sc);
        if (!sc) return NULL;
    
        sc->c = calloc(d, sizeof *sc->c);
        if (!sc->c) {
            free(sc);
            return NULL;
        }
    
        sc->current = 0;
        sc->allocated = d;
    
        return sc;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble with some struct typedef declarations in a header file not
Implement an array of stacks where stacks are defined : typedef struct StackNode {
I am having a struct defined in C++ which contains int and std::string arrays
I'm having difficulties understanding the way sigaction() works. In <signal.h> , sigaction is defined
I am having a struct with three fields defined as follows: struct tmp {
Having code: struct B { int* a; B(int value):a(new int(value)) { } B():a(nullptr){} B(const
I am relatively new to C++ and am having problems understanding struct. I have
Having construction in a form: struct Node { Node():left_(nullptr), right_(nullptr) { } int id_;
I'm having a very big struct in an existing program. This struct includes a
I am having a VC++ stucture like struct VideoInputV20 { int m_nBrightness; int m_nSharpness;

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.