Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
Do I need to memset a C struct in Swift?

AFAIK, In swift, calling the default initialiser of classes/structs will initialise everything to 0 , nil. In C (socket programming for example) sometimes memset is used to set everything to 0 before using the struct. Do I need to use memset in swift too or is fine the way I wrote it?

(BTW, In this case memset is used because hints should be set to 0 except below 2 parameters. Non 0 (garbage, etc) will have side effects on res when calling getaddrinfo.

C:

struct addrinfo hints, *res;
int status;

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

status = getaddrinfo(NULL, MYPORT, &hints, &res);

Swift:

var res = UnsafeMutablePointer<addrinfo>()
var hints = addrinfo()
hints.ai_family = AF_UNSPEC
hints.ai_socktype = SOCK_STREAM

let status = getaddrinfo(nil, MYPORT, &hints, &res)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From the Xcode 6.3 release notes:

Imported C structs now have a default initializer in Swift that initializes all of the struct's fields to zero.

which means that

var hints = addrinfo()

initializes all fields of struct addrinfo to zero and there is no need to call memset().

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda