NVIDIA Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

/* align_size has to be a power of two !! */
void *aligned_malloc(size_t size, size_t align_size) 
{
	char *ptr,*ptr2,*aligned_ptr;
	unsigned int align_mask = (unsigned int)align_size - 1;

	/* We need to use malloc provided by C. First we need to allocate memory
	of size bytes + alignment + sizeof(void *). We need 'bytes' because 
	user requested it. We need to add 'alignment' because malloc can give us 
	any address and we need to find multiple of 'alignment', so at maximum multiple
	of alignment will be 'alignment' bytes away from any location. We need 
	'sizeof(void *)' for implementing 'aligned_free', since we are returning modified 
	memory pointer, not given by malloc ,to the user, we must free the memory 
	allocated by malloc not anything else. So I am storing address given by malloc just above 
	pointer returning to user. Thats why I need extra space to store that address. 
	Then I am checking for error returned by malloc, if it returns NULL then 
	aligned_malloc will fail and return NULL.
	*/

	ptr = (char *)malloc(size + align_size + sizeof(void *));
	if(ptr == NULL) return(NULL);

	ptr2 = ptr + sizeof(void *);
	aligned_ptr = ptr2 + (align_size - ((unsigned int)ptr2 & align_mask));
	
	ptr2 = aligned_ptr - sizeof(void *);
	*((int *)ptr2)=(int)(ptr);

	return(aligned_ptr);
}


void aligned_free(void *ptr) 
{
	int *ptr2=(int *)ptr - 1;
	ptr = (*ptr2);
	free(ptr);
}

- sumit saxena October 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void *align_mem_alloc(size_t size, size_t alignment){
	void *pa, *ptr;
	pa=malloc(sizeof(size)+sizeof(void *)+sizeof(alignment-1));
	if(!pa)
		return NULL;
	ptr=(void*)(((size_t)pa+sizeof(void*)+alignment-1)&(~(alignment-1)));
	*((void**)ptr)=pa;
	return ptr;

}

void align_free(void *ptr){
	if(!ptr)
		return;
	free(*((void**)ptr-1));

}

- Yimin November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

*((void**)ptr)=pa in align mem alloc is correct?? isn't it *((void**)ptr-1)=pa??

- Hemanth November 07, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sizeof(size)+

- odie January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not sure why this is needed here

- odie January 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

any improvement on this?

- Gerald March 06, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More